Skip to content

Commit

Permalink
extendr-api: #[extendr]-enum now returns factor,
Browse files Browse the repository at this point in the history
with levels and tehe right class
  • Loading branch information
CGMossa committed Feb 16, 2024
1 parent f088997 commit 9b7c2e5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions extendr-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ pub const NA_STRING: Option<&str> = None;
/// NA value for logical. `r!(NA_LOGICAL)`
pub const NA_LOGICAL: Rbool = Rbool::na_value();

thread_local! {
pub static R_FACTOR_STRING: once_cell::unsync::Lazy<Robj> = once_cell::unsync::Lazy::new(||{
"factor".into_robj()
});
}

#[doc(hidden)]
pub use std::collections::HashMap;

Expand Down
13 changes: 12 additions & 1 deletion extendr-macros/src/extendr_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,18 @@ pub(crate) fn extendr_enum(item_enum: syn::ItemEnum) -> proc_macro::TokenStream
type Error = extendr_api::Error;
fn try_from(value: #enum_name) -> Result<Self> {
let rint: Rint = value.into();
Ok(rint.try_into()?)
let robj: Robj = rint.try_into()?;
unsafe {
#enum_levels_name_strings.with(|strings_enum|{
let strings_enum = once_cell::unsync::Lazy::force(strings_enum);
libR_sys::Rf_setAttrib(robj.get(), libR_sys::R_LevelsSymbol, strings_enum.get());
});
extendr_api::R_FACTOR_STRING.with(|factor_class| {
let factor_class = once_cell::unsync::Lazy::force(factor_class);
libR_sys::Rf_setAttrib(robj.get(), libR_sys::R_ClassSymbol, factor_class.get());
});
}
Ok(robj)
}
}

Expand Down

0 comments on commit 9b7c2e5

Please sign in to comment.