Enum extendr_api::wrapper::nullable::Nullable

source ·
pub enum Nullable<T> {
    NotNull(T),
    Null,
}
Expand description

Wrapper for handling potentially NULL values.

use extendr_api::prelude::*;
test! {
    use extendr_api::wrapper::Nullable::*;

    // Plain integer.
    let s1 = r!(1);
    let n1 = <Nullable<i32>>::from_robj(&s1)?;
    assert_eq!(n1, NotNull(1));

    // NA integer - error.
    let sna = r!(NA_INTEGER);
    assert_eq!(<Nullable<i32>>::from_robj(&sna).is_err(), true);

    // NA integer - option gives none.
    assert_eq!(<Nullable<Option<i32>>>::from_robj(&sna)?, NotNull(None));

    // NULL object.
    let snull = r!(NULL);
    let nnull = <Nullable<i32>>::from_robj(&snull)?;
    assert_eq!(nnull, Null);

    assert_eq!(r!(Nullable::<i32>::Null), r!(NULL));
    assert_eq!(r!(Nullable::<i32>::NotNull(1)), r!(1));
}

Variants§

§

NotNull(T)

§

Null

Implementations§

source§

impl<T> Nullable<T>
where T: TryFrom<Robj, Error = Error>,

source

pub fn into_option(self) -> Option<T>

Convert Nullable R object into Option

use extendr_api::prelude::*;
test! {
    assert_eq!(Nullable::<Rint>::Null.into_option(), None);
    assert_eq!(Nullable::<Rint>::NotNull(Rint::from(42)).into_option(), Some(Rint::from(42)));
}
source§

impl<T> Nullable<T>

source

pub fn map<F, U>(self, f: F) -> Nullable<U>
where F: FnOnce(T) -> U,

Map Nullable<T> into Nullable<U>

use extendr_api::prelude::*;
test! {
    assert_eq!(Nullable::<Rfloat>::Null.map(|x| x.abs()), Nullable::<Rfloat>::Null);
    assert_eq!(Nullable::<Rfloat>::NotNull(Rfloat::from(42.0)).map(|x| x.abs()), Nullable::<Rfloat>::NotNull(Rfloat::from(42.0)));
}

Trait Implementations§

source§

impl<T: Clone> Clone for Nullable<T>

source§

fn clone(&self) -> Nullable<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Nullable<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, T> From<&'a Nullable<T>> for Option<&'a T>
where T: TryFrom<Robj, Error = Error>,

source§

fn from(value: &'a Nullable<T>) -> Self

Convert a Nullable reference type into Option containing reference

use extendr_api::prelude::*;
test! {
    assert_eq!(<Option<&i32>>::from(&Nullable::Null), None);
    assert_eq!(<Option<&i32>>::from(&Nullable::NotNull(42)), Some(&42));
}
source§

impl<T> From<Nullable<T>> for Option<T>
where T: TryFrom<Robj, Error = Error>,

source§

fn from(value: Nullable<T>) -> Self

Convert a Nullable type into Option

use extendr_api::prelude::*;
test! {
    assert_eq!(<Option<i32>>::from(Nullable::Null), None);
    assert_eq!(<Option<i32>>::from(Nullable::NotNull(42)), Some(42));
}
source§

impl<T> From<Nullable<T>> for Robj
where T: Into<Robj>,

source§

fn from(val: Nullable<T>) -> Self

Convert a rust object to NULL or another type.

use extendr_api::prelude::*;
test! {
    assert_eq!(r!(Nullable::<i32>::Null), r!(NULL));
    assert_eq!(r!(Nullable::<i32>::NotNull(1)), r!(1));
}
source§

impl<T> From<Option<T>> for Nullable<T>
where T: Into<Robj>,

source§

fn from(value: Option<T>) -> Self

Convert an Option into Nullable type

use extendr_api::prelude::*;
test! {
    let x : Nullable<_> = From::<Option<i32>>::from(None);
    assert_eq!(x, Nullable::<i32>::Null);
    let x : Nullable<_> = From::<Option<i32>>::from(Some(42));
    assert_eq!(x, Nullable::<i32>::NotNull(42));
}
source§

impl<'a, T> FromRobj<'a> for Nullable<T>
where T: FromRobj<'a>,

source§

fn from_robj(robj: &'a Robj) -> Result<Self, &'static str>

Convert an object that may be null to a rust type.

use extendr_api::prelude::*;
test! {
    let s1 = r!(1);
    let n1 = <Nullable<i32>>::from_robj(&s1)?;
    assert_eq!(n1, Nullable::NotNull(1));
    let snull = r!(NULL);
    let nnull = <Nullable<i32>>::from_robj(&snull)?;
    assert_eq!(nnull, Nullable::Null);
}
source§

impl<T: PartialEq> PartialEq for Nullable<T>

source§

fn eq(&self, other: &Nullable<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, T> TryFrom<&'a Robj> for Nullable<T>
where T: TryFrom<&'a Robj, Error = Error>,

source§

fn try_from(robj: &'a Robj) -> Result<Self, Self::Error>

Convert an object that may be null to a rust type.

use extendr_api::prelude::*;
test! {
    let s1 = r!(1);
    let n1 = <Nullable<i32>>::try_from(&s1)?;
    assert_eq!(n1, Nullable::NotNull(1));
    let snull = r!(NULL);
    let nnull = <Nullable<i32>>::try_from(&snull)?;
    assert_eq!(nnull, Nullable::Null);
}
§

type Error = Error

The type returned in the event of a conversion error.
source§

impl<T> TryFrom<Robj> for Nullable<T>
where T: TryFrom<Robj, Error = Error>,

source§

fn try_from(robj: Robj) -> Result<Self, Self::Error>

Convert an object that may be null to a rust type.

use extendr_api::prelude::*;
test! {
    let s1 = r!(1);
    let n1 = <Nullable<i32>>::try_from(s1)?;
    assert_eq!(n1, Nullable::NotNull(1));
    let snull = r!(NULL);
    let nnull = <Nullable<i32>>::try_from(snull)?;
    assert_eq!(nnull, Nullable::Null);
}
§

type Error = Error

The type returned in the event of a conversion error.
source§

impl<T> StructuralPartialEq for Nullable<T>

Auto Trait Implementations§

§

impl<T> Freeze for Nullable<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Nullable<T>
where T: RefUnwindSafe,

§

impl<T> Send for Nullable<T>
where T: Send,

§

impl<T> Sync for Nullable<T>
where T: Sync,

§

impl<T> Unpin for Nullable<T>
where T: Unpin,

§

impl<T> UnwindSafe for Nullable<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> IntoRobj for T
where Robj: From<T>,

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.