Path: blob/main/crates/polars-core/src/series/arithmetic/bitops.rs
6940 views
use std::borrow::Cow;12use polars_error::PolarsResult;34use super::{BooleanChunked, ChunkedArray, DataType, IntoSeries, Series, polars_bail};56macro_rules! impl_bitop {7($(($trait:ident, $f:ident))+) => {8$(9impl std::ops::$trait for &Series {10type Output = PolarsResult<Series>;11#[inline(never)]12fn $f(self, rhs: Self) -> Self::Output {13use DataType as DT;14match self.dtype() {15DT::Boolean => {16let lhs: &BooleanChunked = self.as_ref().as_ref().as_ref();17let rhs = lhs.unpack_series_matching_type(rhs)?;18Ok(lhs.$f(rhs).into_series())19},20dt if dt.is_integer() => {21let rhs = if rhs.len() == 1 {22Cow::Owned(rhs.cast(self.dtype())?)23} else {24Cow::Borrowed(rhs)25};2627with_match_physical_integer_polars_type!(dt, |$T| {28let lhs: &ChunkedArray<$T> = self.as_ref().as_ref().as_ref();29let rhs = lhs.unpack_series_matching_type(&rhs)?;30Ok(lhs.$f(&rhs).into_series())31})32},33_ => polars_bail!(opq = $f, self.dtype()),34}35}36}37impl std::ops::$trait for Series {38type Output = PolarsResult<Series>;39#[inline(always)]40fn $f(self, rhs: Self) -> Self::Output {41<&Series as std::ops::$trait>::$f(&self, &rhs)42}43}44impl std::ops::$trait<&Series> for Series {45type Output = PolarsResult<Series>;46#[inline(always)]47fn $f(self, rhs: &Series) -> Self::Output {48<&Series as std::ops::$trait>::$f(&self, rhs)49}50}51impl std::ops::$trait<Series> for &Series {52type Output = PolarsResult<Series>;53#[inline(always)]54fn $f(self, rhs: Series) -> Self::Output {55<&Series as std::ops::$trait>::$f(self, &rhs)56}57}58)+59};60}6162impl_bitop! {63(BitAnd, bitand)64(BitOr, bitor)65(BitXor, bitxor)66}676869