Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add robj clone #348

Merged
merged 6 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ Collate:
'translation.R'
'vctrs.R'
'zzz.R'
Config/rextendr/version: 0.3.1
Config/rextendr/version: 0.3.1.9000
VignetteBuilder: knitr
6 changes: 4 additions & 2 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ coalesce_exprs <- function(exprs) .Call(wrap__coalesce_exprs, exprs)

sum_exprs <- function(exprs) .Call(wrap__sum_exprs, exprs)

mem_address <- function(robj) .Call(wrap__mem_address, robj)

concat_list <- function(exprs) .Call(wrap__concat_list, exprs)

r_date_range <- function(start, stop, every, closed, name, tu, tz) .Call(wrap__r_date_range, start, stop, every, closed, name, tu, tz)
Expand All @@ -51,6 +49,10 @@ arrow_stream_to_rust <- function(rbr) invisible(.Call(wrap__arrow_stream_to_rust

dtype_str_repr <- function(dtype) .Call(wrap__dtype_str_repr, dtype)

mem_address <- function(robj) .Call(wrap__mem_address, robj)

clone_robj <- function(robj) .Call(wrap__clone_robj, robj)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests for them?

test_robj_to_usize <- function(robj) .Call(wrap__test_robj_to_usize, robj)

test_robj_to_i64 <- function(robj) .Call(wrap__test_robj_to_i64, robj)
Expand Down
8 changes: 4 additions & 4 deletions man/nanoarrow.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 17 additions & 39 deletions src/rust/src/rlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ pub fn hor_concat_df(dfs: &VecDataFrame) -> List {
r_result_list(df.map_err(|err| format!("{:?}", err)))
}

#[extendr]
pub fn mem_address(robj: Robj) -> String {
let ptr_raw = unsafe { robj.external_ptr_addr::<usize>() };
let ptr_val = ptr_raw as usize;
format!("{:#012x}", ptr_val)
}

#[extendr]
fn min_exprs(exprs: &ProtoExprArray) -> Expr {
let exprs = exprs.to_vec("select");
Expand Down Expand Up @@ -225,38 +218,20 @@ pub fn dtype_str_repr(dtype: Robj) -> RResult<String> {
Ok(dtype.to_string())
}

// pub fn series_from_arrow(name: &str, array: Robj) -> Result<Series, String> {
// use polars::prelude::IntoSeries;
// let arr = crate::arrow_interop::to_rust::arrow_array_to_rust(array)?;

// match arr.data_type() {
// pl::ArrowDataType::LargeList(_) => {
// let array = arr.as_any().downcast_ref::<pl::LargeListArray>().unwrap();

// let mut previous = 0;
// let mut fast_explode = true;
// for &o in array.offsets().as_slice()[1..].iter() {
// if o == previous {
// fast_explode = false;
// break;
// }
// previous = o;
// }
// let mut out = unsafe { pl::ListChunked::from_chunks(name, vec![arr]) };
// if fast_explode {
// out.set_fast_explode()
// }
// Ok(Series(out.into_series()))
// }
// _ => {
// let res_series: pl::PolarsResult<pl::Series> =
// std::convert::TryFrom::try_from((name, arr));
// let series = res_series.map_err(|err| err.to_string())?;
// Ok(Series(series))
// }
// }
// }
// -- Meta Robj functions
#[extendr]
pub fn mem_address(robj: Robj) -> String {
let ptr_raw = unsafe { robj.external_ptr_addr::<usize>() };
let ptr_val = ptr_raw as usize;
format!("{:#012x}", ptr_val)
}

#[extendr] //could be used to check copying/cloning behavior of R objects
pub fn clone_robj(robj: Robj) -> Robj {
robj.clone()
}

// -- Special functions just for unit testing
#[extendr]
fn test_robj_to_usize(robj: Robj) -> RResult<String> {
robj_to!(usize, robj).map(rdbg)
Expand Down Expand Up @@ -285,7 +260,7 @@ extendr_module! {
fn max_exprs;
fn coalesce_exprs;
fn sum_exprs;
fn mem_address;

fn concat_list;
fn r_date_range;
fn r_date_range_lazy;
Expand All @@ -298,6 +273,9 @@ extendr_module! {
fn arrow_stream_to_rust;
fn dtype_str_repr;

fn mem_address;
fn clone_robj;

fn test_robj_to_usize;
fn test_robj_to_i64;
fn test_robj_to_u32;
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-extendr-meta.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test_that("clone_robj + mem_adress", {

# clone mutable
env = new.env(parent = emptyenv())
env2 = clone_robj(env)
env$foo = 42
expect_identical(env,env2)
expect_identical(pl$mem_address(env), pl$mem_address(env2))

# clone immutable, not the same
l = list()
l2 = clone_robj(l)
l$foo = 42
expect_identical(l,list(foo = 42))
expect_identical(l2,list())
expect_false(pl$mem_address(l) == pl$mem_address(l2))

})