Enum abi_stable::std_types::ROption
source · #[repr(u8)]pub enum ROption<T> {
RSome(T),
RNone,
}
Expand description
Ffi-safe equivalent of the std::option::Option
type.
Option
is also ffi-safe for NonNull/NonZero types, and references.
Variants§
Implementations§
source§impl<T> ROption<T>
impl<T> ROption<T>
sourcepub const fn as_ref(&self) -> ROption<&T>
pub const fn as_ref(&self) -> ROption<&T>
Converts from ROption<T>
to ROption<&T>
.
§Example
assert_eq!(RSome(10).as_ref(), RSome(&10));
assert_eq!(RNone::<u32>.as_ref(), RNone);
sourcepub fn as_mut(&mut self) -> ROption<&mut T>
pub fn as_mut(&mut self) -> ROption<&mut T>
Converts from ROption<T>
to ROption<&mut T>
.
§Example
assert_eq!(RSome(10).as_mut(), RSome(&mut 10));
assert_eq!(RNone::<u32>.as_mut(), RNone);
sourcepub const fn is_rsome(&self) -> bool
pub const fn is_rsome(&self) -> bool
Returns whether self
is an RSome
§Example
assert_eq!(RSome(10).is_rsome(), true);
assert_eq!(RNone::<u32>.is_rsome(), false);
sourcepub const fn is_rnone(&self) -> bool
pub const fn is_rnone(&self) -> bool
Returns whether self
is an RNone
§Example
assert_eq!(RSome(10).is_rnone(), false);
assert_eq!(RNone::<u32>.is_rnone(), true);
sourcepub const fn is_some(&self) -> bool
pub const fn is_some(&self) -> bool
Returns whether self
is an RSome
§Example
assert_eq!(RSome(10).is_some(), true);
assert_eq!(RNone::<u32>.is_some(), false);
sourcepub const fn is_none(&self) -> bool
pub const fn is_none(&self) -> bool
Returns whether self
is an RNone
§Example
assert_eq!(RSome(10).is_none(), false);
assert_eq!(RNone::<u32>.is_none(), true);
sourcepub fn into_option(self) -> Option<T>
pub fn into_option(self) -> Option<T>
Converts from ROption<T>
to Option<T>
.
§Example
assert_eq!(RSome(10).into_option(), Some(10));
assert_eq!(RNone::<u32>.into_option(), None);
sourcepub fn unwrap_or(self, def: T) -> T
pub fn unwrap_or(self, def: T) -> T
Returns the value in the ROption<T>
, or def
if self
is RNone
.
§Example
assert_eq!(RSome(10).unwrap_or(99), 10);
assert_eq!(RNone::<u32>.unwrap_or(99), 99);
sourcepub fn unwrap_or_default(self) -> Twhere
T: Default,
pub fn unwrap_or_default(self) -> Twhere
T: Default,
Returns the value in the ROption<T>
, or T::default()
if self
is RNone
.
§Example
assert_eq!(RSome(10).unwrap_or_default(), 10);
assert_eq!(RNone::<u32>.unwrap_or_default(), 0);
sourcepub fn unwrap_or_else<F>(self, f: F) -> Twhere
F: FnOnce() -> T,
pub fn unwrap_or_else<F>(self, f: F) -> Twhere
F: FnOnce() -> T,
Returns the value in the ROption<T>
,
or the return value of calling f
if self
is RNone
.
§Example
assert_eq!(RSome(10).unwrap_or_else(|| 77), 10);
assert_eq!(RNone::<u32>.unwrap_or_else(|| 77), 77);
sourcepub fn map<U, F>(self, f: F) -> ROption<U>where
F: FnOnce(T) -> U,
pub fn map<U, F>(self, f: F) -> ROption<U>where
F: FnOnce(T) -> U,
Converts the ROption<T>
to a ROption<U>
,
transforming the contained value with the f
closure.
§Example
assert_eq!(RSome(10).map(|x| x * 2), RSome(20));
assert_eq!(RNone::<u32>.map(|x| x * 2), RNone);
sourcepub fn map_or<U, F>(self, default: U, f: F) -> Uwhere
F: FnOnce(T) -> U,
pub fn map_or<U, F>(self, default: U, f: F) -> Uwhere
F: FnOnce(T) -> U,
Transforms (and returns) the contained value with the f
closure,
or returns default
if self
is RNone
.
§Example
assert_eq!(RSome(10).map_or(77, |x| x * 2), 20);
assert_eq!(RNone::<u32>.map_or(77, |x| x * 2), 77);
sourcepub fn map_or_else<U, D, F>(self, otherwise: D, f: F) -> U
pub fn map_or_else<U, D, F>(self, otherwise: D, f: F) -> U
Transforms (and returns) the contained value with the f
closure,
or returns otherwise()
if self
is RNone
..
§Example
assert_eq!(RSome(10).map_or_else(|| 77, |x| x * 2), 20);
assert_eq!(RNone::<u32>.map_or_else(|| 77, |x| x * 2), 77);
sourcepub fn ok_or<E>(self, err: E) -> RResult<T, E>
pub fn ok_or<E>(self, err: E) -> RResult<T, E>
Transforms the ROption<T>
into a RResult<T, E>
, mapping RSome(v)
to ROk(v)
and RNone
to RErr(err)
.
Arguments passed to ok_or
are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use ok_or_else
, which is
lazily evaluated.
§Examples
let x = RSome("foo");
assert_eq!(x.ok_or(0), ROk("foo"));
let x: ROption<&str> = RNone;
assert_eq!(x.ok_or(0), RErr(0));
sourcepub fn ok_or_else<E, F>(self, err: F) -> RResult<T, E>where
F: FnOnce() -> E,
pub fn ok_or_else<E, F>(self, err: F) -> RResult<T, E>where
F: FnOnce() -> E,
Transforms the ROption<T>
into a RResult<T, E>
, mapping RSome(v)
to
ROk(v)
and RNone
to RErr(err())
.
§Examples
let x = RSome("foo");
assert_eq!(x.ok_or_else(|| 0), ROk("foo"));
let x: ROption<&str> = RNone;
assert_eq!(x.ok_or_else(|| 0), RErr(0));
sourcepub fn filter<P>(self, predicate: P) -> Self
pub fn filter<P>(self, predicate: P) -> Self
Returns self
if predicate(&self)
is true, otherwise returns RNone
.
§Example
assert_eq!(RSome(10).filter(|x| (x % 2) == 0), RSome(10));
assert_eq!(RSome(10).filter(|x| (x % 2) == 1), RNone);
assert_eq!(RNone::<u32>.filter(|_| true), RNone);
assert_eq!(RNone::<u32>.filter(|_| false), RNone);
sourcepub fn and(self, optb: ROption<T>) -> ROption<T>
pub fn and(self, optb: ROption<T>) -> ROption<T>
Returns self
if it is RNone
, otherwise returns optb
.
§Example
assert_eq!(RSome(10).and(RSome(20)), RSome(20));
assert_eq!(RSome(10).and(RNone), RNone);
assert_eq!(RNone::<u32>.and(RSome(20)), RNone);
assert_eq!(RNone::<u32>.and(RNone), RNone);
sourcepub fn and_then<F, U>(self, f: F) -> ROption<U>
pub fn and_then<F, U>(self, f: F) -> ROption<U>
Returns self
if it is RNone
,
otherwise returns the result of calling f
with the value in RSome
.
§Example
assert_eq!(RSome(10).and_then(|x| RSome(x * 2)), RSome(20));
assert_eq!(RSome(10).and_then(|_| RNone::<u32>), RNone);
assert_eq!(RNone::<u32>.and_then(|x| RSome(x * 2)), RNone);
assert_eq!(RNone::<u32>.and_then(|_| RNone::<u32>), RNone);
sourcepub fn or(self, optb: ROption<T>) -> ROption<T>
pub fn or(self, optb: ROption<T>) -> ROption<T>
Returns self
if it contains a value, otherwise returns optb
.
§Example
assert_eq!(RSome(10).or(RSome(20)), RSome(10));
assert_eq!(RSome(10).or(RNone ), RSome(10));
assert_eq!(RNone::<u32>.or(RSome(20)), RSome(20));
assert_eq!(RNone::<u32>.or(RNone ), RNone);
sourcepub fn or_else<F>(self, f: F) -> ROption<T>
pub fn or_else<F>(self, f: F) -> ROption<T>
Returns self
if it contains a value,
otherwise calls optb
and returns the value it evaluates to.
§Example
assert_eq!(RSome(10).or_else(|| RSome(20)), RSome(10));
assert_eq!(RSome(10).or_else(|| RNone), RSome(10));
assert_eq!(RNone::<u32>.or_else(|| RSome(20)), RSome(20));
assert_eq!(RNone::<u32>.or_else(|| RNone), RNone);
sourcepub fn xor(self, optb: ROption<T>) -> ROption<T>
pub fn xor(self, optb: ROption<T>) -> ROption<T>
Returns RNone
if both values are RNone
or RSome
,
otherwise returns the value that is anRSome
.
§Example
assert_eq!(RSome(10).xor(RSome(20)), RNone);
assert_eq!(RSome(10).xor(RNone), RSome(10));
assert_eq!(RNone::<u32>.xor(RSome(20)), RSome(20));
assert_eq!(RNone::<u32>.xor(RNone), RNone);
sourcepub fn get_or_insert(&mut self, value: T) -> &mut T
pub fn get_or_insert(&mut self, value: T) -> &mut T
Sets this ROption to RSome(value)
if it was RNone
.
Returns a mutable reference to the inserted/pre-existing RSome
.
§Example
assert_eq!(RSome(10).get_or_insert(40), &mut 10);
assert_eq!(RSome(20).get_or_insert(55), &mut 20);
assert_eq!(RNone::<u32>.get_or_insert(77), &mut 77);
sourcepub fn get_or_insert_with<F>(&mut self, func: F) -> &mut Twhere
F: FnOnce() -> T,
pub fn get_or_insert_with<F>(&mut self, func: F) -> &mut Twhere
F: FnOnce() -> T,
Sets this ROption
to RSome(func())
if it was RNone
.
Returns a mutable reference to the inserted/pre-existing RSome
.
§Example
assert_eq!(RSome(10).get_or_insert_with(|| 40), &mut 10);
assert_eq!(RSome(20).get_or_insert_with(|| 55), &mut 20);
assert_eq!(RNone::<u32>.get_or_insert_with(|| 77), &mut 77);
sourcepub fn take(&mut self) -> ROption<T>
pub fn take(&mut self) -> ROption<T>
Takes the value of self
, replacing it with RNone
§Example
let mut opt0 = RSome(10);
assert_eq!(opt0.take(), RSome(10));
assert_eq!(opt0, RNone);
let mut opt1 = RSome(20);
assert_eq!(opt1.take(), RSome(20));
assert_eq!(opt1, RNone);
let mut opt2 = RNone::<u32>;
assert_eq!(opt2.take(), RNone);
assert_eq!(opt2, RNone);
sourcepub fn replace(&mut self, value: T) -> ROption<T>
pub fn replace(&mut self, value: T) -> ROption<T>
Replaces the value of self
with RSome(value)
.
§Example
let mut opt0 = RSome(10);
assert_eq!(opt0.replace(55), RSome(10));
assert_eq!(opt0, RSome(55));
let mut opt1 = RSome(20);
assert_eq!(opt1.replace(88), RSome(20));
assert_eq!(opt1, RSome(88));
let mut opt2 = RNone::<u32>;
assert_eq!(opt2.replace(33), RNone);
assert_eq!(opt2, RSome(33));
source§impl<T> ROption<&T>
impl<T> ROption<&T>
source§impl<T> ROption<&mut T>
impl<T> ROption<&mut T>
source§impl<T: Deref> ROption<T>
impl<T: Deref> ROption<T>
sourcepub fn as_deref(&self) -> ROption<&T::Target>
pub fn as_deref(&self) -> ROption<&T::Target>
Converts from ROption<T>
(or &ROption<T>
) to ROption<&T::Target>
.
Leaves the original ROption in-place, creating a new one with a
reference to the original one, additionally coercing the contents via
Deref
.
§Examples
let x: ROption<RString> = RSome(RString::from("hey"));
assert_eq!(x.as_deref(), RSome("hey"));
let x: ROption<RString> = RNone;
assert_eq!(x.as_deref(), RNone);
Trait Implementations§
source§impl<'de, T> Deserialize<'de> for ROption<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for ROption<T>where
T: Deserialize<'de>,
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl<T> GetStaticEquivalent_ for ROption<T>where
T: __StableAbi,
impl<T> GetStaticEquivalent_ for ROption<T>where
T: __StableAbi,
source§type StaticEquivalent = _static_ROption<<T as GetStaticEquivalent_>::StaticEquivalent>
type StaticEquivalent = _static_ROption<<T as GetStaticEquivalent_>::StaticEquivalent>
'static
equivalent of Self
source§impl<T> IntoReprRust for ROption<T>
impl<T> IntoReprRust for ROption<T>
source§impl<T: Ord> Ord for ROption<T>
impl<T: Ord> Ord for ROption<T>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<T: PartialOrd> PartialOrd for ROption<T>
impl<T: PartialOrd> PartialOrd for ROption<T>
source§impl<T> StableAbi for ROption<T>where
T: __StableAbi,
impl<T> StableAbi for ROption<T>where
T: __StableAbi,
source§type IsNonZeroType = False
type IsNonZeroType = False
source§const LAYOUT: &'static TypeLayout = _
const LAYOUT: &'static TypeLayout = _
source§const ABI_CONSTS: AbiConsts = _
const ABI_CONSTS: AbiConsts = _
const
-equivalents of the associated types.impl<T: Copy> Copy for ROption<T>
impl<T: Eq> Eq for ROption<T>
impl<T> StructuralPartialEq for ROption<T>
Auto Trait Implementations§
impl<T> Freeze for ROption<T>where
T: Freeze,
impl<T> RefUnwindSafe for ROption<T>where
T: RefUnwindSafe,
impl<T> Send for ROption<T>where
T: Send,
impl<T> Sync for ROption<T>where
T: Sync,
impl<T> Unpin for ROption<T>where
T: Unpin,
impl<T> UnwindSafe for ROption<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> AlignerFor<1> for T
impl<T> AlignerFor<1> for T
source§impl<T> AlignerFor<1024> for T
impl<T> AlignerFor<1024> for T
source§type Aligner = AlignTo1024<T>
type Aligner = AlignTo1024<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<128> for T
impl<T> AlignerFor<128> for T
source§type Aligner = AlignTo128<T>
type Aligner = AlignTo128<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<16> for T
impl<T> AlignerFor<16> for T
source§impl<T> AlignerFor<16384> for T
impl<T> AlignerFor<16384> for T
source§type Aligner = AlignTo16384<T>
type Aligner = AlignTo16384<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<2> for T
impl<T> AlignerFor<2> for T
source§impl<T> AlignerFor<2048> for T
impl<T> AlignerFor<2048> for T
source§type Aligner = AlignTo2048<T>
type Aligner = AlignTo2048<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<256> for T
impl<T> AlignerFor<256> for T
source§type Aligner = AlignTo256<T>
type Aligner = AlignTo256<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<32> for T
impl<T> AlignerFor<32> for T
source§impl<T> AlignerFor<32768> for T
impl<T> AlignerFor<32768> for T
source§type Aligner = AlignTo32768<T>
type Aligner = AlignTo32768<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<4> for T
impl<T> AlignerFor<4> for T
source§impl<T> AlignerFor<4096> for T
impl<T> AlignerFor<4096> for T
source§type Aligner = AlignTo4096<T>
type Aligner = AlignTo4096<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<512> for T
impl<T> AlignerFor<512> for T
source§type Aligner = AlignTo512<T>
type Aligner = AlignTo512<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<64> for T
impl<T> AlignerFor<64> for T
source§impl<T> AlignerFor<8> for T
impl<T> AlignerFor<8> for T
source§impl<T> AlignerFor<8192> for T
impl<T> AlignerFor<8192> for T
source§type Aligner = AlignTo8192<T>
type Aligner = AlignTo8192<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.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
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)
clone_to_uninit
)source§impl<'a, T> RCowCompatibleRef<'a> for Twhere
T: Clone + 'a,
impl<'a, T> RCowCompatibleRef<'a> for Twhere
T: Clone + 'a,
source§fn as_c_ref(from: &'a T) -> <T as RCowCompatibleRef<'a>>::RefC
fn as_c_ref(from: &'a T) -> <T as RCowCompatibleRef<'a>>::RefC
source§fn as_rust_ref(from: <T as RCowCompatibleRef<'a>>::RefC) -> &'a T
fn as_rust_ref(from: <T as RCowCompatibleRef<'a>>::RefC) -> &'a T
source§impl<S> ROExtAcc for S
impl<S> ROExtAcc for S
source§fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
offset
. Read moresource§fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
offset
. Read moresource§fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
offset
. Read moresource§fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
offset
. Read moresource§impl<S> ROExtOps<Aligned> for S
impl<S> ROExtOps<Aligned> for S
source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read moresource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
source§impl<S> ROExtOps<Unaligned> for S
impl<S> ROExtOps<Unaligned> for S
source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read moresource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
source§impl<T> SelfOps for Twhere
T: ?Sized,
impl<T> SelfOps for Twhere
T: ?Sized,
source§fn piped<F, U>(self, f: F) -> U
fn piped<F, U>(self, f: F) -> U
source§fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
piped
except that the function takes &Self
Useful for functions that take &Self
instead of Self
. Read moresource§fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
piped
, except that the function takes &mut Self
.
Useful for functions that take &mut Self
instead of Self
.source§fn mutated<F>(self, f: F) -> Self
fn mutated<F>(self, f: F) -> Self
source§fn observe<F>(self, f: F) -> Self
fn observe<F>(self, f: F) -> Self
source§fn as_ref_<T>(&self) -> &T
fn as_ref_<T>(&self) -> &T
AsRef
,
using the turbofish .as_ref_::<_>()
syntax. Read more