diff --git a/R/extendr-wrappers.R b/R/extendr-wrappers.R index 4917583c0..2a467ec64 100644 --- a/R/extendr-wrappers.R +++ b/R/extendr-wrappers.R @@ -311,6 +311,8 @@ RPolarsRField$set_datatype_mut <- function(datatype) invisible(.Call(wrap__RPola RPolarsErr <- new.env(parent = emptyenv()) +RPolarsErr$default <- function() .Call(wrap__RPolarsErr__default) + RPolarsErr$new <- function() .Call(wrap__RPolarsErr__new) RPolarsErr$contexts <- function() .Call(wrap__RPolarsErr__contexts, self) @@ -1271,7 +1273,7 @@ RPolarsStringCacheHolder <- new.env(parent = emptyenv()) RPolarsStringCacheHolder$hold <- function() .Call(wrap__RPolarsStringCacheHolder__hold) -RPolarsStringCacheHolder$release <- function() .Call(wrap__RPolarsStringCacheHolder__release, self) +RPolarsStringCacheHolder$release <- function() invisible(.Call(wrap__RPolarsStringCacheHolder__release, self)) #' @export `$.RPolarsStringCacheHolder` <- function (self, name) { func <- RPolarsStringCacheHolder[[name]]; environment(func) <- environment(); func } diff --git a/src/rust/src/arrow_interop/to_rust.rs b/src/rust/src/arrow_interop/to_rust.rs index 186d2a0df..55bec01c1 100644 --- a/src/rust/src/arrow_interop/to_rust.rs +++ b/src/rust/src/arrow_interop/to_rust.rs @@ -163,8 +163,8 @@ fn consume_arrow_stream_to_series(boxed_stream: Box) -> R let array = array_res?; let series_res: pl::PolarsResult = std::convert::TryFrom::try_from(("df", array)); - let series = series_res.map_err(polars_to_rpolars_err)?; - series + + series_res.map_err(polars_to_rpolars_err)? } else { rerr() .plain("Arrow array stream was empty") diff --git a/src/rust/src/concat.rs b/src/rust/src/concat.rs index b2c34d765..9dd2feca8 100644 --- a/src/rust/src/concat.rs +++ b/src/rust/src/concat.rs @@ -72,7 +72,7 @@ pub fn concat_series(l: Robj, rechunk: Robj, to_supertypes: Robj) -> RResult Ok(Some(x)), // first fold, acc is None, just us x, Ok(Some(acc)) => polars_core::utils::get_supertype(&acc, &x) .ok_or(RPolarsErr::new().plain("Series' have no common supertype".to_string())) - .map(|dt| Some(dt)), + .map(Some), }); let shared_supertype = shared_supertype?.expect("cannot be None, unless empty s_vec"); diff --git a/src/rust/src/concurrent.rs b/src/rust/src/concurrent.rs index 92e195d16..92b25cae8 100644 --- a/src/rust/src/concurrent.rs +++ b/src/rust/src/concurrent.rs @@ -97,7 +97,10 @@ pub fn collect_with_r_func_support(lazy_df: pl::LazyFrame) -> RResult RResult RPolarsLazyFrame { RPolarsLazyFrame( - pl::LazyFrame::from(self.lgb.logical_plan.clone()) - .with_optimizations(self.opt_state.clone()), + pl::LazyFrame::from(self.lgb.logical_plan.clone()).with_optimizations(self.opt_state), ) } diff --git a/src/rust/src/lazy/dsl.rs b/src/rust/src/lazy/dsl.rs index 98754df7c..134f0248f 100644 --- a/src/rust/src/lazy/dsl.rs +++ b/src/rust/src/lazy/dsl.rs @@ -718,7 +718,7 @@ impl RPolarsExpr { .map(|rank_method| { let options = pl::RankOptions { method: rank_method, - descending: descending, + descending, }; RPolarsExpr(self.0.clone().rank(options, Some(0u64))) }) @@ -1007,8 +1007,8 @@ impl RPolarsExpr { .replace( robj_to!(PLExpr, old)?, robj_to!(PLExpr, new)?, - robj_to!(Option, PLExpr, default)?.map(|e| e), - robj_to!(Option, PLPolarsDataType, return_dtype)?.map(|dt| dt), + robj_to!(Option, PLExpr, default)?, + robj_to!(Option, PLPolarsDataType, return_dtype)?, ) .into()) } @@ -1052,7 +1052,7 @@ impl RPolarsExpr { .clone() .list() .sort(SortOptions { - descending: descending, + descending, ..Default::default() }) .with_fmt("list.sort") @@ -1349,13 +1349,10 @@ impl RPolarsExpr { } pub fn dt_replace_time_zone(&self, tz: Nullable, ambiguous: Robj) -> RResult { - Ok(RPolarsExpr( - self.0 - .clone() - .dt() - .replace_time_zone(tz.into_option(), robj_to!(PLExpr, ambiguous)?) - .into(), - )) + Ok(RPolarsExpr(self.0.clone().dt().replace_time_zone( + tz.into_option(), + robj_to!(PLExpr, ambiguous)?, + ))) } pub fn dt_total_days(&self) -> RResult { @@ -1877,7 +1874,7 @@ impl RPolarsExpr { } pub fn str_to_titlecase(&self) -> RResult { - f_str_to_titlecase(&self) + f_str_to_titlecase(self) } pub fn str_strip_chars(&self, matches: Robj) -> RResult { @@ -2633,7 +2630,7 @@ pub fn robj_to_col(name: Robj, dotdotdot: Robj) -> RResult { match () { _ if name.is_string() && name.len() == 1 && dotdotdot.len() == 0 => { - Ok(RPolarsExpr::col(name.as_str().unwrap_or(&""))) + Ok(RPolarsExpr::col(name.as_str().unwrap_or(""))) } _ if name.inherits("RPolarsDataType") //or if name is a list and first element is RPolarsDataType diff --git a/src/rust/src/rbackground.rs b/src/rust/src/rbackground.rs index 5166c8baf..fe7a6581c 100644 --- a/src/rust/src/rbackground.rs +++ b/src/rust/src/rbackground.rs @@ -112,7 +112,7 @@ pub fn deserialize_dataframe(bits: &[u8]) -> RResult } pub fn serialize_series(series: PSeries) -> RResult> { - serialize_dataframe(&mut std::iter::once(series).into_iter().collect()) + serialize_dataframe(&mut std::iter::once(series).collect()) } pub fn deserialize_series(bits: &[u8]) -> RResult { @@ -371,7 +371,7 @@ impl InnerRBackgroundPool { pub fn resize(&mut self, new_cap: usize) { self.cap = new_cap; - while (self.active > self.cap) & (self.pool.len() > 0) { + while (self.active > self.cap) & (!self.pool.is_empty()) { let handle = self .pool .pop_front() @@ -426,10 +426,10 @@ impl RBackgroundPool { drop(pool_guard); // avoid deadlock #[cfg(feature = "rpolars_debug_print")] println!("wait for freed handler"); - let ok = Ok(rx.recv()?); + #[cfg(feature = "rpolars_debug_print")] println!("thread was awoken queue and passed a handler"); - ok + Ok(rx.recv()?) } } .when("trying to rent a R process from the global R process pool") @@ -454,11 +454,11 @@ impl RBackgroundPool { Ok(()) } - pub fn reval<'t>( - &'t self, + pub fn reval( + &self, raw_func: Vec, raw_arg: Vec, - ) -> RResult RResult> + 't> { + ) -> RResult RResult> + '_> { #[cfg(feature = "rpolars_debug_print")] dbg!("reval"); let handler = self.lease()?; @@ -477,11 +477,11 @@ impl RBackgroundPool { }) } - pub fn rmap_series<'t>( - &'t self, + pub fn rmap_series( + &self, raw_func: Vec, series: PSeries, - ) -> RResult RResult + 't> { + ) -> RResult RResult + '_> { #[cfg(feature = "rpolars_debug_print")] dbg!("rmap_series"); let handler = self.lease()?; diff --git a/src/rust/src/rdataframe/mod.rs b/src/rust/src/rdataframe/mod.rs index 3d2add57d..75c28a533 100644 --- a/src/rust/src/rdataframe/mod.rs +++ b/src/rust/src/rdataframe/mod.rs @@ -286,12 +286,12 @@ impl RPolarsDataFrame { } pub fn select_at_idx(&self, idx: i32) -> List { - let expr_result = || -> Result { + let expr_result = { self.0 .select_at_idx(idx as usize) .map(|s| RPolarsSeries(s.clone())) .ok_or_else(|| format!("select_at_idx: no series found at idx {:?}", idx)) - }(); + }; r_result_list(expr_result) } @@ -441,7 +441,7 @@ impl RPolarsDataFrame { pub fn transpose(&self, keep_names_as: Robj, new_col_names: Robj) -> RResult { let opt_s = robj_to!(Option, str, keep_names_as)?; let opt_vec_s = robj_to!(Option, Vec, String, new_col_names)?; - let opt_either_vec_s = opt_vec_s.map(|vec_s| Either::Right(vec_s)); + let opt_either_vec_s = opt_vec_s.map(Either::Right); self.0 .transpose(opt_s, opt_either_vec_s) .map_err(polars_to_rpolars_err) diff --git a/src/rust/src/rdatatype.rs b/src/rust/src/rdatatype.rs index b4a7f91d8..d6784f8f6 100644 --- a/src/rust/src/rdatatype.rs +++ b/src/rust/src/rdatatype.rs @@ -104,7 +104,7 @@ impl RPolarsDataType { } pub fn new_struct(l: Robj) -> List { - let res = || -> std::result::Result { + let res = { let len = l.len(); //iterate over R list and collect Fields and place in a Struct-datatype @@ -127,7 +127,7 @@ impl RPolarsDataType { .and_then(|iter| collect_hinted_result(len, iter)) .map_err(|err| format!("in pl$Struct: {}", err)) .map(|v_field| RPolarsDataType(pl::DataType::Struct(v_field))) - }(); + }; r_result_list(res) } diff --git a/src/rust/src/rpolarserr.rs b/src/rust/src/rpolarserr.rs index 4e2575e2d..27b31c2be 100644 --- a/src/rust/src/rpolarserr.rs +++ b/src/rust/src/rpolarserr.rs @@ -114,6 +114,10 @@ impl> WithRctx for core::result::Result { #[extendr] impl RPolarsErr { + fn default() -> Self { + Self::new() + } + pub fn new() -> Self { RPolarsErr::new_from_ctxs(VecDeque::new()) } @@ -238,12 +242,11 @@ impl std::fmt::Display for RPolarsErr { if let Some(c) = &self.rcall { writeln!(indented(f).ind(0), "During function call [{}]", c)? } - Ok(self - .contexts + self.contexts .iter() .rev() .enumerate() - .try_for_each(|(idx, ctx)| writeln!(indented(f).ind(idx + 1), "{}", ctx))?) + .try_for_each(|(idx, ctx)| writeln!(indented(f).ind(idx + 1), "{}", ctx)) } } diff --git a/src/rust/src/rstringcache.rs b/src/rust/src/rstringcache.rs index 6aedc4815..8d8fe21cc 100644 --- a/src/rust/src/rstringcache.rs +++ b/src/rust/src/rstringcache.rs @@ -12,7 +12,7 @@ impl RPolarsStringCacheHolder { // R Garbage collection is not deterministic. Deleting all references to RPolarsStringCacheHolder // will cause it to be dropped eventually. Calling release ensures immediate drop, leave back a None. - fn release(&mut self) -> () { + fn release(&mut self) { let _opt_sch = self.0.take(); } } diff --git a/src/rust/src/series.rs b/src/rust/src/series.rs index 66c054d5d..31f25b8f9 100644 --- a/src/rust/src/series.rs +++ b/src/rust/src/series.rs @@ -388,7 +388,7 @@ impl RPolarsSeries { }; //handle any return type from R and collect into Series - let s: extendr_api::Result = || -> extendr_api::Result { + let s: extendr_api::Result = { match out_type { Float64 => apply_output!(r_iter, strict, allow_fail_eval, Doubles, Float64Chunked), Int32 => apply_output!(r_iter, strict, allow_fail_eval, Integers, Int32Chunked), @@ -425,7 +425,7 @@ impl RPolarsSeries { _ => todo!("this output type is not implemented"), } - }(); + }; let s = s.map(move |mut x| { x.rename_mut(&format!("{}_apply", &self.name())); diff --git a/src/rust/src/utils/mod.rs b/src/rust/src/utils/mod.rs index f77cac379..8be415f40 100644 --- a/src/rust/src/utils/mod.rs +++ b/src/rust/src/utils/mod.rs @@ -445,11 +445,11 @@ pub fn inner_unpack_r_result_list(robj: extendr_api::Robj) -> Result let l = robj.as_list().expect("extendr_result is a list"); let ok = l.elt(0).expect("extendr_result has a 1st element"); let err = l.elt(1).expect("extendr_result has a 2nd element"); - let inner_res = match err.rtype() { + + match err.rtype() { Rtype::Null => Ok(ok), _ => Err(err), - }; - inner_res + } } else { Ok(robj) } @@ -475,7 +475,8 @@ pub fn unpack_r_result_list(robj: extendr_api::Robj) -> RResult { }); // 3 - Convert any Robj-err to a Robj-RPolarsErr - let res = res.map_err(|err| { + + res.map_err(|err| { if err.inherits("RPolarsErr") { //robj was already an external ptr to RPolarsErr, use as is unsafe { &mut *err.external_ptr_addr::() }.clone() @@ -483,9 +484,7 @@ pub fn unpack_r_result_list(robj: extendr_api::Robj) -> RResult { //robj was still some other error, upcast err to a string err and wrap in RPolarsErr RPolarsErr::new().plain(rdbg(err)) } - }); - - res + }) } //None if not real or Na. @@ -547,7 +546,7 @@ pub fn robj_to_rchoice(robj: extendr_api::Robj) -> RResult { let robj = unpack_r_result_list(robj)?; let robj_clone = robj.clone(); let s_res: EResult = robj.try_into(); - let opt_str = s_res.map(|s| s.iter().next().map(|rstr| rstr.clone())); + let opt_str = s_res.map(|s| s.iter().next().cloned()); match opt_str { // NA_CHARACTER not allowed as first element return error Ok(Some(rstr)) if rstr.is_na() => { @@ -609,7 +608,7 @@ pub fn robj_to_i64(robj: extendr_api::Robj) -> RResult { let robj = unpack_r_result_list(robj)?; use extendr_api::*; - return match (robj.rtype(), robj.len()) { + match (robj.rtype(), robj.len()) { (_, 0 | 2..) => Some(err_no_scalar()), (Rtype::Strings, 1) => Some(robj_parse_str_to_t(robj.clone())), (Rtype::Doubles, 1) if robj.inherits("integer64") => { @@ -625,14 +624,14 @@ pub fn robj_to_i64(robj: extendr_api::Robj) -> RResult { .unwrap_or_else(err_no_nan) .bad_robj(&robj) .mistyped(tn::()) - .when("converting into type"); + .when("converting into type") } pub fn robj_to_i32(robj: extendr_api::Robj) -> RResult { let robj = unpack_r_result_list(robj)?; use extendr_api::*; - return match (robj.rtype(), robj.len()) { + match (robj.rtype(), robj.len()) { (_, 0 | 2..) => Some(err_no_scalar()), (Rtype::Strings, 1) => Some(robj_parse_str_to_t(robj.clone())), (Rtype::Doubles, 1) if robj.inherits("integer64") => { @@ -647,14 +646,14 @@ pub fn robj_to_i32(robj: extendr_api::Robj) -> RResult { .unwrap_or_else(err_no_nan) .bad_robj(&robj) .mistyped(tn::()) - .when("converting into type"); + .when("converting into type") } pub fn robj_to_f64(robj: extendr_api::Robj) -> RResult { let robj = unpack_r_result_list(robj)?; use extendr_api::*; - return match (robj.rtype(), robj.len()) { + match (robj.rtype(), robj.len()) { (_, 0 | 2..) => Some(err_no_scalar()), (Rtype::Strings, 1) => Some(robj_parse_str_to_t(robj.clone())), (Rtype::Doubles, 1) if robj.inherits("integer64") => { @@ -669,7 +668,7 @@ pub fn robj_to_f64(robj: extendr_api::Robj) -> RResult { .unwrap_or_else(err_no_nan) .bad_robj(&robj) .mistyped(tn::()) - .when("converting into type"); + .when("converting into type") } pub fn robj_to_u64(robj: extendr_api::Robj) -> RResult { @@ -869,7 +868,7 @@ pub fn list_expr_to_vec_pl_expr( robj_to_rexpr(robj.clone(), str_to_lit) .when(format!("converting element {} into an Expr", i + 1)) .map(|e| { - if name != "" && named { + if !name.is_empty() && named { e.0.alias(name) } else { e.0