pub enum Field<T> {
    Missing,
    Present(Option<T>),
}

Variants§

§

Missing

§

Present(Option<T>)

Implementations§

source§

impl<T> Field<T>

source

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());
source

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());
source

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());
source

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));
source

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);
source

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)));
source

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)));
source

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));
source

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);
source

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);
source

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);
source

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);
source

pub fn unwrap(self) -> T

Returns the contained Some value, consuming the self value.

Panics

Panics if the self value equals Missing or Present(None).

Examples
let x = Present(Some("air"));
assert_eq!(x.unwrap(), "air");
let x: Field<&str> = Present(None);
assert_eq!(x.unwrap(), "air"); // fails
source

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

Returns the contained option, consuming the self value.

Panics

Panics if the self value equals Missing.

Examples
let x = Present(Some("air"));
assert_eq!(x.unwrap_present(), Some("air"));
let x: Field<&str> = Missing;
assert_eq!(x.unwrap_present(), Some("air")); // fails
source

pub fn unwrap_present_ref(&self) -> &Option<T>

Returns a reference to the contained option.

Panics

Panics if the self value equals Missing.

Examples
let x = Present(Some("air"));
assert_eq!(x.unwrap_present_ref(), &Some("air"));
let x: Field<&str> = Missing;
assert_eq!(x.unwrap_present_ref(), &Some("air")); // fails
source

pub fn unwrap_present_mut(&mut self) -> &mut Option<T>

Returns a mutable reference to the contained option.

Panics

Panics if the self value equals Missing.

Examples
let mut x = Present(Some("air"));
assert_eq!(x.unwrap_present_mut(), &mut Some("air"));
let mut x: Field<&str> = Missing;
assert_eq!(x.unwrap_present_mut(), &mut Some("air")); // fails
source

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");
source

pub fn unwrap_present_or(self, default: Option<T>) -> Option<T>

Returns the contained Present value or a provided default.

Examples
assert_eq!(Present(Some("car")).unwrap_present_or(Some("bike")), Some("car"));
assert_eq!(Present(None).unwrap_present_or(Some("bike")), None);
assert_eq!(Missing.unwrap_present_or(Some("bike")), Some("bike"));
source

pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T

Returns the contained Some value or computes it from a closure.

Examples
let k = 10;
assert_eq!(Present(Some(4)).unwrap_or_else(|| 2 * k), 4);
assert_eq!(Present(None).unwrap_or_else(|| 2 * k), 20);
assert_eq!(Missing.unwrap_or_else(|| 2 * k), 20);
source

pub fn unwrap_present_or_else<F: FnOnce() -> Option<T>>(self, f: F) -> Option<T>

Returns the contained Present value or computes it from a closure.

Examples
assert_eq!(Present(Some(4)).unwrap_present_or_else(|| Some(10)), Some(4));
assert_eq!(Present(None).unwrap_present_or_else(|| Some(20)), None);
assert_eq!(Missing.unwrap_present_or_else(|| Some(30)), Some(30));
source

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`
source

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`
source

pub fn ok_or<E>(self, err: E) -> Result<T, E>

source

pub fn ok_present_or<E>(self, err: E) -> Result<Option<T>, E>

source

pub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E>

source

pub fn ok_present_or_else<E, F: FnOnce() -> E>( self, err: F ) -> Result<Option<T>, E>

source

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);
source

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);
source

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));
source

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));
source

pub fn get_or_insert(&mut self, value: T) -> &mut T

Inserts value into the option if it is Missing or [Present(None)], then returns a mutable reference to the contained value.

Examples
let mut x = Missing;

{
    let y: &mut u32 = x.get_or_insert(5);
    assert_eq!(y, &5);

    *y = 7;
}

assert_eq!(x, Present(Some(7)));
source

pub fn get_or_insert_present(&mut self, value: Option<T>) -> &mut Option<T>

Inserts value into the option if it is Missing or [Present(None)], then returns a mutable reference to the contained value.

Examples
let mut x = Missing;

{
    let y: &mut Option<u32> = x.get_or_insert_present(Some(5));
    assert_eq!(y, &Some(5));

    *y = Some(7);
}

assert_eq!(x, Present(Some(7)));
source

pub fn get_or_insert_with<F>(&mut self, f: F) -> &mut Twhere F: FnOnce() -> T,

Inserts a value computed from f into the option if it is Missing or [Present(None)], then returns a mutable reference to the contained value.

Examples
let mut x = Missing;

{
    let y: &mut u32 = x.get_or_insert_with(|| 5);
    assert_eq!(y, &5);

    *y = 7;
}

assert_eq!(x, Present(Some(7)));
source

pub fn get_or_insert_with_present<F>(&mut self, f: F) -> &mut Option<T>where F: FnOnce() -> Option<T>,

Inserts a value computed from f into the option if it is Missing or [Present(None)], then returns a mutable reference to the contained value.

Examples
let mut x = Missing;

{
    let y: &mut u32 = x.get_or_insert_with(|| 5);
    assert_eq!(y, &5);

    *y = 7;
}

assert_eq!(x, Present(Some(7)));
source§

impl<T: Default> Field<T>

source§

impl<T: Copy> Field<&T>

source

pub fn copied(self) -> Field<T>

Maps a Field<&T> to a Field<T> by copying the contents of the option.

Examples
let x = 12;
let opt_x = Some(&x);
assert_eq!(opt_x, Some(&12));
let copied = opt_x.copied();
assert_eq!(copied, Some(12));
source§

impl<T: Copy> Field<&mut T>

source

pub fn copied(self) -> Field<T>

Maps a Field<&mut T> to an Field<T> by copying the contents of the option.

Examples
let mut x = 12;
let opt_x = Present(Some(&x));
assert_eq!(opt_x, Present(Some(&x)));
let copied = opt_x.copied();
assert_eq!(copied, Present(Some(12)));
source§

impl<T: Clone> Field<&T>

source

pub fn cloned(self) -> Field<T>

Maps a Field<&T> to a Field<T> by cloning the contents .

Examples
let x = 12;
let opt_x = Present(Some(&x));
assert_eq!(opt_x, Present(Some(&x)));
let cloned = opt_x.cloned();
assert_eq!(cloned, Present(Some(12)));
source§

impl<T: Clone> Field<&mut T>

source

pub fn cloned(self) -> Field<T>

Maps a Field<&mut T> to a Field<T> by cloning the contents.

Examples
let mut x = 12;
let opt_x = Present(Some(&mut x));
assert_eq!(opt_x, Present(Some(&mut 12)));
let cloned = opt_x.cloned();
assert_eq!(cloned, Present(Some(12)));
source§

impl<T: Deref> Field<T>

source

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>

source

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,

source

pub fn delta(&self, other: &Field<T>) -> Field<T>

Returns a new Field which is the difference between Self 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"))));

Trait Implementations§

source§

impl<T: Clone> Clone for Field<T>

source§

fn clone(&self) -> Field<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 Field<T>

source§

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

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

impl<T> Default for Field<T>

source§

fn default() -> Field<T>

Returns the “default value” for a type. Read more
source§

impl<'de, T> Deserialize<'de> for Field<T>where T: Deserialize<'de>,

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<T> From<Option<Option<T>>> for Field<T>

source§

fn from(opt: Option<Option<T>>) -> Field<T>

Converts to this type from the input type.
source§

impl<T> From<Option<T>> for Field<T>

source§

fn from(opt: Option<T>) -> Field<T>

Converts to this type from the input type.
source§

impl<T> From<T> for Field<T>

source§

fn from(val: T) -> Field<T>

Converts to this type from the input type.
source§

impl<T: PartialEq> PartialEq<Field<T>> for Field<T>

source§

fn eq(&self, other: &Field<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<T> Serialize for Field<T>where T: Serialize,

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<T: Eq> Eq for Field<T>

source§

impl<T> StructuralEq for Field<T>

source§

impl<T> StructuralPartialEq for Field<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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> ToOwned for Twhere 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 Twhere 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 Twhere 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.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,