Enum optional_field::Field
source · pub enum Field<T> {
Missing,
Present(Option<T>),
}
Variants§
Implementations§
source§impl<T> Field<T>
impl<T> Field<T>
sourcepub fn is_missing(&self) -> bool
pub fn is_missing(&self) -> bool
Is the value missing?
Examples
assert!(Missing::<u8>.is_missing());
assert!(!Present::<u8>(None).is_missing());
assert!(!Present(Some(1)).is_missing());
sourcepub fn is_present(&self) -> bool
pub fn is_present(&self) -> bool
Is the value present?
Examples
assert!(!Missing::<u8>.is_present());
assert!(Present::<u8>(None).is_present());
assert!(Present(Some(1)).is_present());
sourcepub fn has_value(&self) -> bool
pub fn has_value(&self) -> bool
Is present and the value is not None?
Examples
assert!(!Missing::<u8>.has_value());
assert!(!Present::<u8>(None).has_value());
assert!(Present(Some(1)).has_value());
sourcepub fn contains<U>(&self, x: &U) -> boolwhere
U: PartialEq<T>,
pub fn contains<U>(&self, x: &U) -> boolwhere U: PartialEq<T>,
Does the value contain the given value?
Examples
let x = 1;
assert!(!Missing::<u8>.contains(&x));
assert!(!Present::<u8>(None).contains(&x));
assert!(Present(Some(1)).contains(&x));
sourcepub fn as_ref(&self) -> Field<&T>
pub fn as_ref(&self) -> Field<&T>
Converts from &Field<T>
to Field<&T>
.
Examples
Converts a Field<
String
>
into an Field<
usize
>
, preserving the original.
The map
method takes the self
argument by value, consuming the original,
so this technique uses as_ref
to first take an Field
to a reference
to the value inside the original.
let text: Field<String> = Present(Some("Hello, world!".to_string()));
// First, cast `Field<String>` to `Field<&String>` with `as_ref`,
// then consume *that* with `map`, leaving `text` on the stack.
let text_length: Field<usize> = text.as_ref().map(|s| s.len());
println!("still can print text: {:?}", text);
sourcepub fn as_mut(&mut self) -> Field<&mut T>
pub fn as_mut(&mut self) -> Field<&mut T>
Converts from &mut Field<T>
to Field<&mut T>
.
Examples
let mut x = Present(Some(2));
match x.as_mut() {
Present(Some(v)) => *v = 42,
_ => {},
}
assert_eq!(x, Present(Some(42)));
sourcepub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Field<U>
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Field<U>
Maps a Field<T>
to Field<U>
by applying a function to the value contained in
the inner Option
.
Examples
Converts a Field<
String
>
into an Field<
usize
>
, consuming the original:
let maybe_some_string = Present(Some(String::from("Hello, World!")));
// `Field::map` takes self *by value*, consuming `maybe_some_string`
let maybe_some_len = maybe_some_string.map(|s| s.len());
assert_eq!(maybe_some_len, Present(Some(13)));
sourcepub fn map_present<U, F: FnOnce(Option<T>) -> Option<U>>(self, f: F) -> Field<U>
pub fn map_present<U, F: FnOnce(Option<T>) -> Option<U>>(self, f: F) -> Field<U>
Maps a Field<T>
to Field<U>
by applying a function to the option contained in Present
.
Examples
Converts a Field<
String
>
into an Field<
usize
>
, consuming the original:
let maybe_some_string = Present(Some(String::from("Hello, World!")));
// `Field::map_present` takes self *by value*, consuming `maybe_some_string`
let maybe_some_len = maybe_some_string.map_present(|s| None);
assert_eq!(maybe_some_len, Present::<usize>(None));
sourcepub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U
pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U
Applies a function to the value contained in the inner Option
(if any),
or returns the provided default (if not).
Arguments passed to map_or
are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use map_or_else
,
which is lazily evaluated.
Examples
let x = Present(Some("foo"));
assert_eq!(x.map_or(42, |v| v.len()), 3);
let x: Field<&str> = Missing;
assert_eq!(x.map_or(42, |v| v.len()), 42);
sourcepub fn map_present_or<U, F: FnOnce(Option<T>) -> Option<U>>(
self,
default: Option<U>,
f: F
) -> Option<U>
pub fn map_present_or<U, F: FnOnce(Option<T>) -> Option<U>>( self, default: Option<U>, f: F ) -> Option<U>
Applies a function to the value contained in Present
(if any),
or returns the provided default (if not).
Examples
let x = Present(Some("foo"));
assert_eq!(x.map_or(42, |v| v.len()), 3);
let x: Field<&str> = Missing;
assert_eq!(x.map_or(42, |v| v.len()), 42);
sourcepub fn map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>(
self,
default: D,
f: F
) -> U
pub fn map_or_else<U, D: FnOnce() -> U, F: FnOnce(T) -> U>( self, default: D, f: F ) -> U
Computes a default function result if the field is Missing or Present(None), or applies a different function to the contained value (if any).
Examples
let k = 21;
let x = Present(Some("foo"));
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3);
let x: Option<&str> = None;
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);
sourcepub fn map_present_or_else<U, D: FnOnce() -> Option<U>, F: FnOnce(Option<T>) -> Option<U>>(
self,
default: D,
f: F
) -> Option<U>
pub fn map_present_or_else<U, D: FnOnce() -> Option<U>, F: FnOnce(Option<T>) -> Option<U>>( self, default: D, f: F ) -> Option<U>
Computes a default function result (if missing), or applies a different function to the contained value (if any).
Examples
let k = 21;
let x = Present(Some("foo"));
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3);
let x: Option<&str> = None;
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);
sourcepub fn unwrap_present(self) -> Option<T>
pub fn unwrap_present(self) -> Option<T>
sourcepub fn unwrap_present_ref(&self) -> &Option<T>
pub fn unwrap_present_ref(&self) -> &Option<T>
sourcepub fn unwrap_present_mut(&mut self) -> &mut Option<T>
pub fn unwrap_present_mut(&mut self) -> &mut Option<T>
sourcepub fn unwrap_or(self, default: T) -> T
pub fn unwrap_or(self, default: T) -> T
Returns the contained Some
value or a provided default.
Arguments passed to unwrap_or
are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use unwrap_or_else
,
which is lazily evaluated.
Examples
assert_eq!(Present(Some("car")).unwrap_or("bike"), "car");
assert_eq!(Present(None).unwrap_or("bike"), "bike");
assert_eq!(Missing.unwrap_or("bike"), "bike");
sourcepub fn unwrap_present_or(self, default: Option<T>) -> Option<T>
pub fn unwrap_present_or(self, default: Option<T>) -> Option<T>
sourcepub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T
pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T
sourcepub fn unwrap_present_or_else<F: FnOnce() -> Option<T>>(self, f: F) -> Option<T>
pub fn unwrap_present_or_else<F: FnOnce() -> Option<T>>(self, f: F) -> Option<T>
sourcepub fn expect(self, msg: &str) -> T
pub fn expect(self, msg: &str) -> T
Returns the contained Some
value from within Present
, consuming the self
value.
Panics
Panics if the value is a Missing
or [‘None’] with a custom panic message provided by
msg
.
Examples
let x = Present(Some("value"));
assert_eq!(x.expect("fruits are healthy"), "value");
let x: Field<Option<&str>> = Missing;
x.expect("fruits are healthy"); // panics with `fruits are healthy`
sourcepub fn expect_present(self, msg: &str) -> Option<T>
pub fn expect_present(self, msg: &str) -> Option<T>
Returns the contained Option
in the Present
, consuming the self
value.
Panics
Panics if the value is a Missing
with a custom panic message provided by
msg
.
Examples
let x: Field<Option<&str>> = Present(None);
assert_eq!(x.expect_present("fruits are healthy"), None);
let x: Field<Option<&str>> = Missing;
x.expect("fruits are healthy"); // panics with `fruits are healthy`
pub fn ok_or<E>(self, err: E) -> Result<T, E>
pub fn ok_present_or<E>(self, err: E) -> Result<Option<T>, E>
pub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E>
pub fn ok_present_or_else<E, F: FnOnce() -> E>( self, err: F ) -> Result<Option<T>, E>
sourcepub fn and<U>(self, fieldb: Field<U>) -> Field<U>
pub fn and<U>(self, fieldb: Field<U>) -> Field<U>
Returns None
if the option is None
, otherwise returns fieldb
.
Examples
let x = Present(Some(2));
let y: Field<&str> = Present(None);
assert_eq!(x.and(y), Present(None));
let x: Field<u32> = Present(None);
let y = Present(Some("foo"));
assert_eq!(x.and(y), Present(None));
let x = Present(Some(2));
let y = Present(Some("foo"));
assert_eq!(x.and(y), Present(Some("foo")));
let x: Field<u32> = Present(None);
let y: Field<&str> = Present(None);
assert_eq!(x.and(y), Present(None));
let x: Field<u32> = Missing;
let y: Field<&str> = Missing;
assert_eq!(x.and(y), Missing);
sourcepub fn and_present<U>(self, fieldb: Field<U>) -> Field<U>
pub fn and_present<U>(self, fieldb: Field<U>) -> Field<U>
Returns None
if the option is None
, otherwise returns fieldb
.
Examples
let x = Present(Some(2));
let y: Field<&str> = Present(None);
assert_eq!(x.and(y), Present(None));
let x: Field<u32> = Present(None);
let y = Present(Some("foo"));
assert_eq!(x.and(y), Present(None));
let x = Present(Some(2));
let y = Present(Some("foo"));
assert_eq!(x.and(y), Present(Some("foo")));
let x: Field<u32> = Present(None);
let y: Field<&str> = Present(None);
assert_eq!(x.and(y), Present(None));
let x: Field<u32> = Missing;
let y: Field<&str> = Missing;
assert_eq!(x.and(y), Missing);
sourcepub fn and_then<U, F>(self, f: F) -> Field<U>where
F: FnOnce(T) -> Field<U>,
pub fn and_then<U, F>(self, f: F) -> Field<U>where F: FnOnce(T) -> Field<U>,
Returns Missing
if the field is Missing
, Present(None) if the field
is Present(None), otherwise calls f
with the wrapped value and returns the result.
let x: Field<&str> = Present(Some("foo"));
assert_eq!(x.clone().and_then(|_s| Present(Some(1)) ), Present(Some(1)));
assert_eq!(x.clone().and_then(|_s| Missing::<u8> ), Missing::<u8>);
assert_eq!(x.and_then(|_s| Present::<u8>(None) ), Present::<u8>(None));
sourcepub fn and_then_present<U, F>(self, f: F) -> Field<U>where
F: FnOnce(Option<T>) -> Field<U>,
pub fn and_then_present<U, F>(self, f: F) -> Field<U>where F: FnOnce(Option<T>) -> Field<U>,
Returns Missing
if the field is Missing
, otherwise calls f
with the wrapped value and returns the result.
let x: Field<&str> = Present(Some("foo"));
assert_eq!(x.clone().and_then_present(|_s| Present(Some(1)) ), Present(Some(1)));
assert_eq!(x.clone().and_then_present(|_s| Missing::<u8> ), Missing::<u8>);
assert_eq!(x.and_then_present(|_s| Present::<u8>(None) ), Present::<u8>(None));
sourcepub fn get_or_insert(&mut self, value: T) -> &mut T
pub fn get_or_insert(&mut self, value: T) -> &mut T
sourcepub fn get_or_insert_present(&mut self, value: Option<T>) -> &mut Option<T>
pub fn get_or_insert_present(&mut self, value: Option<T>) -> &mut Option<T>
sourcepub fn get_or_insert_with<F>(&mut self, f: F) -> &mut Twhere
F: FnOnce() -> T,
pub fn get_or_insert_with<F>(&mut self, f: F) -> &mut Twhere F: FnOnce() -> T,
sourcepub fn get_or_insert_with_present<F>(&mut self, f: F) -> &mut Option<T>where
F: FnOnce() -> Option<T>,
pub fn get_or_insert_with_present<F>(&mut self, f: F) -> &mut Option<T>where F: FnOnce() -> Option<T>,
source§impl<T: Default> Field<T>
impl<T: Default> Field<T>
pub fn unwrap_or_default(self) -> T
pub fn unwrap_present_or_default(self) -> Option<T>
source§impl<T: Deref> Field<T>
impl<T: Deref> Field<T>
sourcepub fn as_deref(&self) -> Field<&T::Target>
pub fn as_deref(&self) -> Field<&T::Target>
Converts from Field<T>
(or &Field<T>
) to Field<&T::Target>
.
Leaves the original Field in-place, creating a new one with a reference
to the original one, additionally coercing the contents via Deref
.
Examples
let x: Field<String> = Present(Some("hey".to_owned()));
assert_eq!(x.as_deref(), Present(Some("hey")));
let x: Field<String> = Present(None);
assert_eq!(x.as_deref(), Present(None));
source§impl<T: DerefMut> Field<T>
impl<T: DerefMut> Field<T>
sourcepub fn as_deref_mut(&mut self) -> Field<&mut T::Target>
pub fn as_deref_mut(&mut self) -> Field<&mut T::Target>
Converts from Field<T>
(or &mut Field<T>
) to Field<&mut T::Target>
.
Leaves the original Field
in-place, creating a new one containing a mutable reference to
the inner type’s Deref::Target
type.
Examples
let mut x: Field<String> = Present(Some("hey".to_owned()));
assert_eq!(x.as_deref_mut().map(|x| {
x.make_ascii_uppercase();
x
}), Present(Some("HEY".to_owned().as_mut_str())));
source§impl<T> Field<T>where
T: Clone + PartialEq,
impl<T> Field<T>where T: Clone + PartialEq,
sourcepub fn delta(&self, other: &Field<T>) -> Field<T>
pub fn delta(&self, other: &Field<T>) -> Field<T>
Returns a new FieldSelf
and other
.
Assumes Self
is the current value and other
is the “new” value,
it evaluates if the value has changed and if so returns a field
with the new value.
Examples
let old = Present(Some("oh hai"));
// Values are the same
assert_eq!(Missing, old.delta(&Present(Some("oh hai"))));
// There is no new value to compare
assert_eq!(Missing, old.delta(&Missing));
// The value has changed
assert_eq!(Present(Some("new")), old.delta(&Present(Some("new"))));