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

Variants

Missing

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, 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

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(), "air"); // fails

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

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

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

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`

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

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

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

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

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

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Deserialize this value from the given Serde deserializer. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.