Skip to content

Commit

Permalink
extendr-enum: added return-type conversion
Browse files Browse the repository at this point in the history
for `use_try_from`
  • Loading branch information
CGMossa committed Feb 16, 2024
1 parent a3f07c0 commit f088997
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions extendr-api/tests/extendr_enum_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn tst_enum_wrapper(_x: Robj, enum_fct: Model) {
}

#[extendr(use_try_from = true)]
// #[extendr]
fn my_enum(e: &str) -> Model {
match e.to_uppercase().as_str() {
"A" => Model::MeanModel,
Expand Down
2 changes: 2 additions & 0 deletions tests/extendrtests/R/extendr-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ tst_altinteger <- function() .Call(wrap__tst_altinteger)

tst_enum_wrapper <- function(`_x`, enum_fct) invisible(.Call(wrap__tst_enum_wrapper, `_x`, enum_fct))

my_enum <- function(e) .Call(wrap__my_enum, e)

#' Class for testing (exported)
#' @examples
#' x <- MyClass$new()
Expand Down
11 changes: 11 additions & 0 deletions tests/extendrtests/src/rust/src/enum_as_factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ fn tst_enum_wrapper(_x: Robj, enum_fct: Model) {
};
}

#[extendr(use_try_from = true)]
fn my_enum(e: &str) -> Model {
match e.to_uppercase().as_str() {
"A" => Model::A,
"B" => Model::B,
"C" => Model::C,
_ => unimplemented!(),
}
}

extendr_module! {
mod enum_as_factor;
fn tst_enum_wrapper;
fn my_enum;
}

0 comments on commit f088997

Please sign in to comment.