Skip to content

Commit

Permalink
updated extendr to 0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
CGMossa committed Jul 1, 2024
1 parent 2096838 commit 80d65e6
Show file tree
Hide file tree
Showing 15 changed files with 364 additions and 350 deletions.
615 changes: 309 additions & 306 deletions src/rust/Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ opt-level = 3 # was 1 to support 1.66, but since 1.70 is needed anyway it does n
opt-level = 3

[dependencies]
extendr-api = { git = "https://github.com/extendr/extendr", rev = "1895bfc8ee22347665900caa0e48da4f0b13232f", default-features = false, features = [
extendr-api = { version = "0.7.0", default-features = false, features = [
"result_list",
"serde",
] }
Expand Down
10 changes: 7 additions & 3 deletions src/rust/src/arrow_interop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ pub enum RArrowArrayClass {
NanoArrowArray,
}

impl<'a> FromRobj<'a> for RArrowArrayClass {
fn from_robj(robj: &Robj) -> std::result::Result<Self, &'static str> {
impl TryFrom<&Robj> for RArrowArrayClass {
type Error = extendr_api::Error;

fn try_from(robj: &Robj) -> extendr_api::Result<Self> {
if robj.inherits("nanoarrow_array") {
Ok(RArrowArrayClass::NanoArrowArray)
} else if robj.inherits("Array") {
Ok(RArrowArrayClass::ArrowArray)
} else {
Err("Robj does not inherit from Array or nanoarrow_array")
Err(Error::Other(
"Robj does not inherit from Array or nanoarrow_array".into(),
))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rust/src/arrow_interop/to_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn arrow_array_to_rust(arrow_array: Robj) -> Result<ArrayRef, String> {
)
};

RArrowArrayClass::from_robj(&arrow_array)?
RArrowArrayClass::try_from(&arrow_array)?
.get_package()
.get_export_array_func()?
.call(pairlist!(&arrow_array, ext_a, ext_s))?;
Expand Down
1 change: 0 additions & 1 deletion src/rust/src/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use extendr_api::prelude::*;
use polars::lazy::dsl;
use polars::prelude as pl;
use polars_core::functions as pl_functions;
use std::result::Result;

#[extendr]
fn concat_lf(
Expand Down
4 changes: 2 additions & 2 deletions src/rust/src/conversion_r_to_s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ fn recursive_robjname2series_tree(x: &Robj, name: &str) -> pl::PolarsResult<Seri

Rtype::Raw => {
let rpolars_raw_list = list!(x)
.set_class(["rpolars_raw_list", "list"])
.set_class(["rpolars_raw_list", "list"]).cloned()
.map_err(|err| pl::polars_err!(ComputeError: err.to_string()))?;
recursive_robjname2series_tree(&rpolars_raw_list, name)
recursive_robjname2series_tree(&rpolars_raw_list.into_robj(), name)
}

Rtype::List if x.inherits("rpolars_raw_list") => {
Expand Down
8 changes: 7 additions & 1 deletion src/rust/src/conversion_s_to_r.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub fn pl_series_to_list(
})
.collect_robj()
.set_class(&["integer64"])
.cloned()
.expect("internal error could not set class label 'integer64'")
}),
_ => Err(pl::PolarsError::InvalidOperation(
Expand Down Expand Up @@ -108,6 +109,7 @@ pub fn pl_series_to_list(
extendr_api::List::from_values(x)
.into_robj()
.set_class(["rpolars_raw_list", "list"])
.cloned()
.expect("this class label is always valid")
}),
Enum(_, _) => s
Expand Down Expand Up @@ -186,6 +188,7 @@ pub fn pl_series_to_list(
.into_iter()
.collect_robj()
.set_class(&["Date"])
.cloned()
.expect("internal error: class label Date failed")),
Null => Ok((extendr_api::NULL).into_robj()),
Time => s
Expand All @@ -200,8 +203,9 @@ pub fn pl_series_to_list(
.map(|mut robj| {
robj.set_class(&["PTime"])
.expect("internal error: class label PTime failed")
.clone()
})
.map(|mut robj| robj.set_attrib("tu", "ns"))
.map(|mut robj| robj.set_attrib("tu", "ns").cloned())
.expect("internal error: attr tu failed")
.map_err(|err| {
pl_error::ComputeError(
Expand Down Expand Up @@ -253,9 +257,11 @@ pub fn pl_series_to_list(
.map(|mut robj| {
robj.set_class(&["POSIXct", "POSIXt"])
.expect("internal error: class POSIXct label failed")
.clone()
})
.map(|mut robj| {
robj.set_attrib("tzone", opt_tz.as_ref().map(|s| s.as_str()).unwrap_or(""))
.cloned()
})
.expect("internal error: attr tzone failed")
.map_err(|err| {
Expand Down
6 changes: 2 additions & 4 deletions src/rust/src/lazy/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ use polars::prelude as pl;
use polars::prelude::{JoinCoalesce, SerializeOptions};
use polars_lazy::prelude::CsvWriterOptions;

#[allow(unused_imports)]
use std::result::Result;

#[derive(Clone)]
pub struct RPolarsLazyFrame(pub pl::LazyFrame);

Expand All @@ -37,6 +34,7 @@ impl From<pl::LazyFrame> for RPolarsLazyFrame {
}
}

use extendr_api::Result;
#[extendr]
impl RPolarsLazyFrame {
fn print(&self) -> RResult<Self> {
Expand All @@ -52,7 +50,7 @@ impl RPolarsLazyFrame {
}

//low level version of describe_plan, mainly for arg testing
pub fn debug_plan(&self) -> Result<String, String> {
pub fn debug_plan(&self) -> std::result::Result<String, String> {
use polars_core::export::serde::Serialize;
use serde_json::value::Serializer;
Serialize::serialize(&self.0.logical_plan.clone(), Serializer)
Expand Down
10 changes: 5 additions & 5 deletions src/rust/src/lazy/dsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use polars::prelude as pl;
use polars::prelude::{ExprEvalExtension, NestedType, SortOptions};
use std::any::Any;
use std::ops::{Add, Div, Mul, Rem, Sub};
use std::result::Result;
pub type NameGenerator = pl::Arc<dyn Fn(usize) -> String + Send + Sync>;
use crate::rdatatype::robjs_to_ewm_options;
use crate::utils::r_expr_to_rust_expr;
Expand Down Expand Up @@ -56,6 +55,7 @@ impl From<pl::Expr> for RPolarsExpr {
}
}

use extendr_api::Result;
#[extendr]
impl RPolarsExpr {
//constructors
Expand Down Expand Up @@ -2494,7 +2494,7 @@ impl RPolarsExpr {
.into())
}

pub fn bin_starts_with(&self, sub: Robj) -> Result<Self, String> {
pub fn bin_starts_with(&self, sub: Robj) -> std::result::Result<Self, String> {
Ok(self
.0
.clone()
Expand All @@ -2503,7 +2503,7 @@ impl RPolarsExpr {
.into())
}

pub fn bin_ends_with(&self, sub: Robj) -> Result<Self, String> {
pub fn bin_ends_with(&self, sub: Robj) -> std::result::Result<Self, String> {
Ok(self
.0
.clone()
Expand Down Expand Up @@ -2560,7 +2560,7 @@ impl RPolarsExpr {
List::from_values(exprs.iter().map(|e| RPolarsExpr(e.clone())))
}

fn meta_eq(&self, other: Robj) -> Result<bool, String> {
fn meta_eq(&self, other: Robj) -> std::result::Result<bool, String> {
let other = robj_to!(Expr, other)?;
Ok(self.0 == other.0)
}
Expand All @@ -2575,7 +2575,7 @@ impl RPolarsExpr {
.collect()
}

fn meta_output_name(&self) -> Result<String, String> {
fn meta_output_name(&self) -> std::result::Result<String, String> {
let name = self
.0
.clone()
Expand Down
3 changes: 3 additions & 0 deletions src/rust/src/rbackground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ impl<T: Send + Sync + 'static> RPolarsRThreadHandle<T> {
}
}

use extendr_api::Error;
use extendr_api::ExternalPtr;
use extendr_api::Result;
#[extendr]
impl RPolarsRThreadHandle<RResult<RPolarsDataFrame>> {
fn join(&mut self) -> RResult<RPolarsDataFrame> {
Expand Down
22 changes: 11 additions & 11 deletions src/rust/src/rdataframe/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use extendr_api::{extendr, prelude::*, rprintln};
use polars::prelude::{self as pl, IntoLazy, SerWriter};
use std::result::Result;
pub mod read_csv;
pub mod read_ipc;
pub mod read_ndjson;
Expand Down Expand Up @@ -52,7 +51,7 @@ impl OwnedDataFrameIterator {
}

impl Iterator for OwnedDataFrameIterator {
type Item = Result<Box<dyn arrow::array::Array>, PolarsError>;
type Item = std::result::Result<Box<dyn arrow::array::Array>, PolarsError>;

fn next(&mut self) -> Option<Self::Item> {
if self.idx >= self.n_chunks {
Expand Down Expand Up @@ -86,6 +85,7 @@ impl From<pl::DataFrame> for RPolarsDataFrame {
}
}

use extendr_api::Result;
#[extendr]
impl RPolarsDataFrame {
pub fn shape(&self) -> Robj {
Expand Down Expand Up @@ -143,14 +143,14 @@ impl RPolarsDataFrame {
}

//internal use
pub fn set_column_from_robj(&mut self, robj: Robj, name: &str) -> Result<(), String> {
pub fn set_column_from_robj(&mut self, robj: Robj, name: &str) -> std::result::Result<(), String> {
robjname2series(robj, name)
.and_then(|s| self.0.with_column(s).map(|_| ()))
.map_err(|err| format!("in set_column_from_robj: {:?}", err))
}

//internal use
pub fn set_column_from_series(&mut self, x: &RPolarsSeries) -> Result<(), String> {
pub fn set_column_from_series(&mut self, x: &RPolarsSeries) -> std::result::Result<(), String> {
let s: pl::Series = x.into(); //implicit clone, cannot move R objects
self.0
.with_column(s)
Expand Down Expand Up @@ -183,7 +183,7 @@ impl RPolarsDataFrame {
.collect()
}

pub fn set_column_names_mut(&mut self, names: Vec<String>) -> Result<(), String> {
pub fn set_column_names_mut(&mut self, names: Vec<String>) -> std::result::Result<(), String> {
self.0
.set_column_names(&names[..])
.map(|_| ())
Expand Down Expand Up @@ -229,7 +229,7 @@ impl RPolarsDataFrame {
// }

pub fn to_list(&self, int64_conversion: &str) -> List {
let robj_vec_res: Result<Vec<Robj>, _> = collect_hinted_result(
let robj_vec_res: std::result::Result<Vec<Robj>, _> = collect_hinted_result(
self.0.width(),
self.0
.iter()
Expand All @@ -249,7 +249,7 @@ impl RPolarsDataFrame {

//this methods should only be used for benchmarking
pub fn to_list_unwind(&self, int64_conversion: &str) -> Robj {
let robj_vec_res: Result<Vec<Robj>, _> = collect_hinted_result(
let robj_vec_res: std::result::Result<Vec<Robj>, _> = collect_hinted_result(
self.0.width(),
self.0
.iter()
Expand All @@ -271,7 +271,7 @@ impl RPolarsDataFrame {
// does not expose this arg in to_list as it is quite niche and might be deprecated later
pub fn to_list_tag_structs(&self, int64_conversion: &str) -> List {
//convert DataFrame to Result of to R vectors, error if DataType is not supported
let robj_vec_res: Result<Vec<Robj>, _> = collect_hinted_result(
let robj_vec_res: std::result::Result<Vec<Robj>, _> = collect_hinted_result(
self.0.width(),
self.0
.iter()
Expand Down Expand Up @@ -368,7 +368,7 @@ impl RPolarsDataFrame {
}
}

pub fn from_arrow_record_batches(rbr: Robj) -> Result<RPolarsDataFrame, String> {
pub fn from_arrow_record_batches(rbr: Robj) -> std::result::Result<RPolarsDataFrame, String> {
Ok(RPolarsDataFrame(unsafe {
crate::arrow_interop::to_rust::to_rust_df(rbr)
}?))
Expand Down Expand Up @@ -607,9 +607,9 @@ impl RPolarsDataFrame {
}

impl RPolarsDataFrame {
pub fn to_list_result(&self, int64_conversion: &str) -> Result<Robj, pl::PolarsError> {
pub fn to_list_result(&self, int64_conversion: &str) -> std::result::Result<Robj, pl::PolarsError> {
//convert DataFrame to Result of to R vectors, error if DataType is not supported
let robj_vec_res: Result<Vec<Robj>, _> = self
let robj_vec_res: std::result::Result<Vec<Robj>, _> = self
.0
.iter()
.map(|s| pl_series_to_list(s, true, int64_conversion))
Expand Down
1 change: 0 additions & 1 deletion src/rust/src/rdataframe/read_csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use polars::io::RowIndex;
use crate::utils::wrappers::{null_to_opt, Wrap};
use extendr_api::{extendr, prelude::*};
use polars::prelude as pl;
use std::result::Result;

//see param, null_values
#[derive(Clone, Debug)]
Expand Down
1 change: 0 additions & 1 deletion src/rust/src/rdataframe/read_ndjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use polars::io::RowIndex;
use extendr_api::{extendr, prelude::*};
use polars::prelude as pl;
use polars::prelude::LazyFileListReader;
use std::result::Result;

#[allow(clippy::too_many_arguments)]
#[extendr]
Expand Down
5 changes: 4 additions & 1 deletion src/rust/src/rpolarserr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::VecDeque;

use extendr_api::{
call, eval_string, eval_string_with_params, extendr, extendr_module, symbol::class_symbol,
Attributes, Nullable, Operators, Pairlist, Rinternals, Robj, Types, R,
Attributes, Nullable, Operators, Pairlist, Robj, Types, R,
};

#[derive(Clone, Debug, thiserror::Error, serde::Deserialize, serde::Serialize)]
Expand Down Expand Up @@ -118,6 +118,9 @@ impl Default for RPolarsErr {
}
}

use extendr_api::Result;
use extendr_api::Error;
use extendr_api::ExternalPtr;
#[extendr]
impl RPolarsErr {
fn default() -> Self {
Expand Down
Loading

0 comments on commit 80d65e6

Please sign in to comment.