Module extendr_api::optional::either

source ·
Expand description

Enables support for the either crate, to allow accepting and returning Either<L, R> values if both L and R are convertible to/from Robj.

either crate support is currently available in the dev version of extendr-api and requires enabling either feature:

[dependencies]
extendr-api = { git = "https://github.com/extendr/extendr" , features = ["either"] }

Accepting Either<L, R> as an input requires enabling the extendr option use_try_from = TRUE:

use extendr_api::prelude::*;

#[extendr(use_try_from = true)]
fn accept_numeric(input : Either<Integers, Doubles>) {}

Here is an example of either usage – a type-aware sum:

use extendr_api::prelude::*;

#[extendr(use_try_from = true)]
fn type_aware_sum(input : Either<Integers, Doubles>) -> Either<Rint, Rfloat> {
    match input {
        Left(ints) => Left(ints.iter().sum::<Rint>()),
        Right(dbls) => Right(dbls.iter().sum::<Rfloat>()),
    }
}