Enum optional_field::Field [−][src]
pub enum Field<T> {
Missing,
Present(Option<T>),
}Variants
Present(Option<T>)Implementations
Is the value missing?
Examples
assert!(Missing::<u8>.is_missing()); assert!(!Present::<u8>(None).is_missing()); assert!(!Present(Some(1)).is_missing());
Is the value present?
Examples
assert!(!Missing::<u8>.is_present()); assert!(Present::<u8>(None).is_present()); assert!(Present(Some(1)).is_present());
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());
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));
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);
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)));
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)));
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));
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);
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);
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);
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);
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");
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`
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`
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));
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())));
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for Field<T> where
T: RefUnwindSafe, impl<T> UnwindSafe for Field<T> where
T: UnwindSafe, Blanket Implementations
Mutably borrows from an owned value. Read more