Module 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"] }
use extendr_api::prelude::*;

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

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

use extendr_api::prelude::*;

#[extendr]
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>()),
    }
}