Skip to content

Commit

Permalink
Improve inspect() to show if the object is materialized (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation committed May 2, 2024
1 parent 7593d5c commit f8e0366
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
10 changes: 7 additions & 3 deletions src/altrep/altinteger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ pub trait AltInteger: Sized + IntoExtPtrSexp {
}

/// What gets printed when `.Internal(inspect(x))` is used.
fn inspect(&mut self) {
crate::io::r_print(&format!("({})", Self::CLASS_NAME), false);
fn inspect(&mut self, is_materialized: bool) {
crate::io::r_print(
&format!("{} (materialized: {is_materialized})", Self::CLASS_NAME),
false,
);
}

/// Converts the struct into an ALTREP object
Expand Down Expand Up @@ -179,9 +182,10 @@ pub fn register_altinteger_class<T: AltInteger>(
_: c_int,
_: Option<unsafe extern "C" fn(SEXP, c_int, c_int, c_int)>,
) -> Rboolean {
let is_materialized = unsafe { R_altrep_data2(x) != R_NilValue };
match super::extract_mut_from_altrep::<T>(&mut x) {
Ok(self_) => {
self_.inspect();
self_.inspect(is_materialized);
Rboolean_TRUE
}
Err(_) => Rboolean_FALSE,
Expand Down
10 changes: 7 additions & 3 deletions src/altrep/altlogical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ pub trait AltLogical: Sized + IntoExtPtrSexp {
}

/// What gets printed when `.Internal(inspect(x))` is used.
fn inspect(&mut self) {
crate::io::r_print(&format!("({})", Self::CLASS_NAME), false);
fn inspect(&mut self, is_materialized: bool) {
crate::io::r_print(
&format!("{} (materialized: {is_materialized})", Self::CLASS_NAME),
false,
);
}

/// Converts the struct into an ALTREP object
Expand Down Expand Up @@ -179,9 +182,10 @@ pub fn register_altlogical_class<T: AltLogical>(
_: c_int,
_: Option<unsafe extern "C" fn(SEXP, c_int, c_int, c_int)>,
) -> Rboolean {
let is_materialized = unsafe { R_altrep_data2(x) != R_NilValue };
match super::extract_mut_from_altrep::<T>(&mut x) {
Ok(self_) => {
self_.inspect();
self_.inspect(is_materialized);
Rboolean_TRUE
}
Err(_) => Rboolean_FALSE,
Expand Down
10 changes: 7 additions & 3 deletions src/altrep/altreal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ pub trait AltReal: Sized + IntoExtPtrSexp {
}

/// What gets printed when `.Internal(inspect(x))` is used.
fn inspect(&mut self) {
crate::io::r_print(&format!("({})", Self::CLASS_NAME), false);
fn inspect(&mut self, is_materialized: bool) {
crate::io::r_print(
&format!("{} (materialized: {is_materialized})", Self::CLASS_NAME),
false,
);
}

/// Converts the struct into an ALTREP object
Expand Down Expand Up @@ -165,9 +168,10 @@ pub fn register_altreal_class<T: AltReal>(
_: c_int,
_: Option<unsafe extern "C" fn(SEXP, c_int, c_int, c_int)>,
) -> Rboolean {
let is_materialized = unsafe { R_altrep_data2(x) != R_NilValue };
match super::extract_mut_from_altrep::<T>(&mut x) {
Ok(self_) => {
self_.inspect();
self_.inspect(is_materialized);
Rboolean_TRUE
}
Err(_) => Rboolean_FALSE,
Expand Down
22 changes: 13 additions & 9 deletions src/altrep/altstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ pub trait AltString: Sized + IntoExtPtrSexp {
fn elt(&mut self, i: usize) -> &str;

/// What gets printed when `.Internal(inspect(x))` is used.
fn inspect(&mut self) {
crate::io::r_print(&format!("({})", Self::CLASS_NAME), false);
fn inspect(&mut self, is_materialized: bool) {
crate::io::r_print(
&format!("{} (materialized: {is_materialized})", Self::CLASS_NAME),
false,
);
}

/// Converts the struct into an ALTREP object
Expand Down Expand Up @@ -160,13 +163,14 @@ pub fn register_altstring_class<T: AltString>(
_: c_int,
_: Option<unsafe extern "C" fn(SEXP, c_int, c_int, c_int)>,
) -> Rboolean {
let self_: &mut T = match super::extract_mut_from_altrep(&mut x) {
Ok(self_) => self_,
Err(_) => return Rboolean_FALSE,
};
self_.inspect();

Rboolean_TRUE
let is_materialized = unsafe { R_altrep_data2(x) != R_NilValue };
match super::extract_mut_from_altrep::<T>(&mut x) {
Ok(self_) => {
self_.inspect(is_materialized);
Rboolean_TRUE
}
Err(_) => Rboolean_FALSE,
}
}

unsafe extern "C" fn altstring_elt<T: AltString>(mut x: SEXP, i: R_xlen_t) -> SEXP {
Expand Down

0 comments on commit f8e0366

Please sign in to comment.