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>>::try_from(&s1)?;
assert_eq!(n1, NotNull(1));
// NA integer - error.
let sna = r!(NA_INTEGER);
assert_eq!(<Nullable<i32>>::try_from(&sna).is_err(), true);
// NA integer - option gives none.
assert_eq!(<Nullable<Option<i32>>>::try_from(&sna)?, NotNull(None));
// NULL object.
let snull = r!(NULL);
let nnull = <Nullable<i32>>::try_from(&snull)?;
assert_eq!(nnull, Null);
assert_eq!(r!(Nullable::<i32>::Null), r!(NULL));
assert_eq!(r!(Nullable::<i32>::NotNull(1)), r!(1));
}
Variants§
Implementations§
source§impl<T> Nullable<T>
impl<T> Nullable<T>
sourcepub fn into_option(self) -> Option<T>
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>
impl<T> Nullable<T>
sourcepub fn map<F, U>(self, f: F) -> Nullable<U>where
F: FnOnce(T) -> U,
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> From<Option<T>> for Nullable<T>
impl<T> From<Option<T>> for Nullable<T>
source§fn from(value: Option<T>) -> Self
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> TryFrom<&'a Robj> for Nullable<T>
impl<'a, T> TryFrom<&'a Robj> for Nullable<T>
source§fn try_from(robj: &'a Robj) -> Result<Self, Self::Error>
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);
}
source§impl<T> TryFrom<Robj> for Nullable<T>
impl<T> TryFrom<Robj> for Nullable<T>
source§fn try_from(robj: Robj) -> Result<Self, Self::Error>
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);
}
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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