pub enum Nullable<T> {
    Null,
    Present(T),
}
Expand description

The Nullable type. Represents a value which may be specified as null on an API. Note that this is distinct from a value that is optional and not present!

Nullable implements many of the same methods as the Option type (map, unwrap, etc).

Variants§

§

Null

Null value

§

Present(T)

Value is present

Implementations§

source§

impl<T> Nullable<T>

source

pub fn is_present(&self) -> bool

Returns true if the Nullable is a Present value.

Examples

let x: Nullable<u32> = Nullable::Present(2);
assert_eq!(x.is_present(), true);

let x: Nullable<u32> = Nullable::Null;
assert_eq!(x.is_present(), false);
source

pub fn is_null(&self) -> bool

Returns true if the Nullable is a Null value.

Examples

let x: Nullable<u32> = Nullable::Present(2);
assert_eq!(x.is_null(), false);

let x: Nullable<u32> = Nullable::Null;
assert_eq!(x.is_null(), true);
source

pub fn as_ref(&self) -> Nullable<&T>

Converts from Nullable<T> to Nullable<&T>.

Examples

Convert an Nullable<String> into a Nullable<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 a Nullable to a reference to the value inside the original.


let num_as_str: Nullable<String> = Nullable::Present("10".to_string());
// First, cast `Nullable<String>` to `Nullable<&String>` with `as_ref`,
// then consume *that* with `map`, leaving `num_as_str` on the stack.
let num_as_int: Nullable<usize> = num_as_str.as_ref().map(|n| n.len());
println!("still can print num_as_str: {:?}", num_as_str);
source

pub fn as_mut(&mut self) -> Nullable<&mut T>

Converts from Nullable<T> to Nullable<&mut T>.

Examples

let mut x = Nullable::Present(2);
match x.as_mut() {
    Nullable::Present(v) => *v = 42,
    Nullable::Null => {},
}
assert_eq!(x, Nullable::Present(42));
source

pub fn expect(self, msg: &str) -> T

Unwraps a Nullable, yielding the content of a Nullable::Present.

Panics

Panics if the value is a Nullable::Null with a custom panic message provided by msg.

Examples

let x = Nullable::Present("value");
assert_eq!(x.expect("the world is ending"), "value");

let x: Nullable<&str> = Nullable::Null;
x.expect("the world is ending"); // panics with `the world is ending`
source

pub fn unwrap(self) -> T

Moves the value v out of the Nullable<T> if it is Nullable::Present(v).

In general, because this function may panic, its use is discouraged. Instead, prefer to use pattern matching and handle the Nullable::Null case explicitly.

Panics

Panics if the self value equals Nullable::Null.

Examples

let x = Nullable::Present("air");
assert_eq!(x.unwrap(), "air");

let x: Nullable<&str> = Nullable::Null;
assert_eq!(x.unwrap(), "air"); // fails
source

pub fn unwrap_or(self, def: T) -> T

Returns the contained value or a default.

Examples

assert_eq!(Nullable::Present("car").unwrap_or("bike"), "car");
assert_eq!(Nullable::Null.unwrap_or("bike"), "bike");
source

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

Returns the contained value or computes it from a closure.

Examples

let k = 10;
assert_eq!(Nullable::Present(4).unwrap_or_else(|| 2 * k), 4);
assert_eq!(Nullable::Null.unwrap_or_else(|| 2 * k), 20);
source

pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Nullable<U>

Maps a Nullable<T> to Nullable<U> by applying a function to a contained value.

Examples

Convert a Nullable<String> into a Nullable<usize>, consuming the original:


let maybe_some_string = Nullable::Present(String::from("Hello, World!"));
// `Nullable::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, Nullable::Present(13));
source

pub fn map_or<U, F: FnOnce(T) -> U>(self, default: U, f: F) -> U

Applies a function to the contained value (if any), or returns a default (if not).

Examples

let x = Nullable::Present("foo");
assert_eq!(x.map_or(42, |v| v.len()), 3);

let x: Nullable<&str> = Nullable::Null;
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

Applies a function to the contained value (if any), or computes a default (if not).

Examples

let k = 21;

let x = Nullable::Present("foo");
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3);

let x: Nullable<&str> = Nullable::Null;
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);
source

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

Transforms the Nullable<T> into a Result<T, E>, mapping Nullable::Present(v) to Ok(v) and Nullable::Null to Err(err).

Examples

let x = Nullable::Present("foo");
assert_eq!(x.ok_or(0), Ok("foo"));

let x: Nullable<&str> = Nullable::Null;
assert_eq!(x.ok_or(0), Err(0));
source

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

Transforms the Nullable<T> into a Result<T, E>, mapping Nullable::Present(v) to Ok(v) and Nullable::Null to Err(err()).

Examples

let x = Nullable::Present("foo");
assert_eq!(x.ok_or_else(|| 0), Ok("foo"));

let x: Nullable<&str> = Nullable::Null;
assert_eq!(x.ok_or_else(|| 0), Err(0));
source

pub fn and<U>(self, optb: Nullable<U>) -> Nullable<U>

Returns Nullable::Null if the Nullable is Nullable::Null, otherwise returns optb.

Examples

let x = Nullable::Present(2);
let y: Nullable<&str> = Nullable::Null;
assert_eq!(x.and(y), Nullable::Null);

let x: Nullable<u32> = Nullable::Null;
let y = Nullable::Present("foo");
assert_eq!(x.and(y), Nullable::Null);

let x = Nullable::Present(2);
let y = Nullable::Present("foo");
assert_eq!(x.and(y), Nullable::Present("foo"));

let x: Nullable<u32> = Nullable::Null;
let y: Nullable<&str> = Nullable::Null;
assert_eq!(x.and(y), Nullable::Null);
source

pub fn and_then<U, F: FnOnce(T) -> Nullable<U>>(self, f: F) -> Nullable<U>

Returns Nullable::Null if the Nullable is Nullable::Null, otherwise calls f with the wrapped value and returns the result.

Some languages call this operation flatmap.

Examples

fn sq(x: u32) -> Nullable<u32> { Nullable::Present(x * x) }
fn nope(_: u32) -> Nullable<u32> { Nullable::Null }

assert_eq!(Nullable::Present(2).and_then(sq).and_then(sq), Nullable::Present(16));
assert_eq!(Nullable::Present(2).and_then(sq).and_then(nope), Nullable::Null);
assert_eq!(Nullable::Present(2).and_then(nope).and_then(sq), Nullable::Null);
assert_eq!(Nullable::Null.and_then(sq).and_then(sq), Nullable::Null);
source

pub fn or(self, optb: Nullable<T>) -> Nullable<T>

Returns the Nullable if it contains a value, otherwise returns optb.

Examples

let x = Nullable::Present(2);
let y = Nullable::Null;
assert_eq!(x.or(y), Nullable::Present(2));

let x = Nullable::Null;
let y = Nullable::Present(100);
assert_eq!(x.or(y), Nullable::Present(100));

let x = Nullable::Present(2);
let y = Nullable::Present(100);
assert_eq!(x.or(y), Nullable::Present(2));

let x: Nullable<u32> = Nullable::Null;
let y = Nullable::Null;
assert_eq!(x.or(y), Nullable::Null);
source

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

Returns the Nullable if it contains a value, otherwise calls f and returns the result.

Examples

fn nobody() -> Nullable<&'static str> { Nullable::Null }
fn vikings() -> Nullable<&'static str> { Nullable::Present("vikings") }

assert_eq!(Nullable::Present("barbarians").or_else(vikings),
           Nullable::Present("barbarians"));
assert_eq!(Nullable::Null.or_else(vikings), Nullable::Present("vikings"));
assert_eq!(Nullable::Null.or_else(nobody), Nullable::Null);
source

pub fn take(&mut self) -> Nullable<T>

Takes the value out of the Nullable, leaving a Nullable::Null in its place.

Examples

let mut x = Nullable::Present(2);
x.take();
assert_eq!(x, Nullable::Null);

let mut x: Nullable<u32> = Nullable::Null;
x.take();
assert_eq!(x, Nullable::Null);
source§

impl<'a, T: Clone> Nullable<&'a T>

source

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

Maps an Nullable<&T> to an Nullable<T> by cloning the contents of the Nullable.

Examples

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

impl<T: Default> Nullable<T>

source

pub fn unwrap_or_default(self) -> T

Returns the contained value or a default

Consumes the self argument then, if Nullable::Present, returns the contained value, otherwise if Nullable::Null, returns the default value for that type.

Examples

let x = Nullable::Present(42);
assert_eq!(42, x.unwrap_or_default());

let y: Nullable<i32> = Nullable::Null;
assert_eq!(0, y.unwrap_or_default());

Trait Implementations§

source§

impl<T: Clone> Clone for Nullable<T>

source§

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

source§

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

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

impl<T> Default for Nullable<T>

source§

fn default() -> Nullable<T>

Returns None.

source§

impl<'de, T> Deserialize<'de> for Nullable<T>where T: DeserializeOwned,

Available on crate feature serdejson only.
source§

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

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

impl<T> From<T> for Nullable<T>

source§

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

Converts to this type from the input type.
source§

impl<T: PartialEq> PartialEq<Nullable<T>> for Nullable<T>

source§

fn eq(&self, other: &Nullable<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: PartialOrd> PartialOrd<Nullable<T>> for Nullable<T>

source§

fn partial_cmp(&self, other: &Nullable<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T> Serialize for Nullable<T>where T: Serialize,

Available on crate feature serdejson only.
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> Validate for Nullable<T>where T: Validate,

Available on crate feature serdevalid only.
source§

impl<T, U> ValidateCompositedEnumerate<T> for Nullable<U>where T: Copy, U: ValidateCompositedEnumerate<T>,

Available on crate feature serdevalid only.
source§

impl<T, U> ValidateCompositedExclusiveMaximum<T> for Nullable<U>where T: Copy, U: ValidateCompositedExclusiveMaximum<T>,

Available on crate feature serdevalid only.
source§

impl<T, U> ValidateCompositedExclusiveMinimum<T> for Nullable<U>where T: Copy, U: ValidateCompositedExclusiveMinimum<T>,

Available on crate feature serdevalid only.
source§

impl<T> ValidateCompositedMaxLength for Nullable<T>where T: ValidateCompositedMaxLength,

Available on crate feature serdevalid only.
source§

impl<T> ValidateCompositedMaxProperties for Nullable<T>where T: ValidateCompositedMaxProperties,

Available on crate feature serdevalid only.
source§

impl<T, U> ValidateCompositedMaximum<T> for Nullable<U>where T: Copy, U: ValidateCompositedMaximum<T>,

Available on crate feature serdevalid only.
source§

impl<T> ValidateCompositedMinLength for Nullable<T>where T: ValidateCompositedMinLength,

Available on crate feature serdevalid only.
source§

impl<T> ValidateCompositedMinProperties for Nullable<T>where T: ValidateCompositedMinProperties,

Available on crate feature serdevalid only.
source§

impl<T, U> ValidateCompositedMinimum<T> for Nullable<U>where T: Copy, U: ValidateCompositedMinimum<T>,

Available on crate feature serdevalid only.
source§

impl<T, U> ValidateCompositedMultipleOf<T> for Nullable<U>where T: Copy, U: ValidateCompositedMultipleOf<T>,

Available on crate feature serdevalid only.
source§

impl<T> ValidateCompositedPattern for Nullable<T>where T: ValidateCompositedPattern,

Available on crate feature serdevalid only.
source§

impl<T: Copy> Copy for Nullable<T>

source§

impl<T> StructuralPartialEq for Nullable<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Nullable<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<Choices> CoproductSubsetter<CNil, HNil> for Choices

§

type Remainder = Choices

source§

fn subset( self ) -> Result<CNil, <Choices as CoproductSubsetter<CNil, HNil>>::Remainder>

Extract a subset of the possible types in a coproduct (or get the remaining possibilities) 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> FromJsonReader for Twhere T: DeserializeOwned + Validate,

source§

fn from_json_reader<R>(reader: R) -> Result<T, Error<Error>>where R: Read,

Convert from json reader. Read more
source§

impl<'de, T> FromJsonSlice<'de> for Twhere T: Deserialize<'de> + Validate,

source§

fn from_json_slice(slice: &'de [u8]) -> Result<T, Error<Error>>

Convert from json slice. Read more
source§

impl<'de, T> FromJsonStr<'de> for Twhere T: Deserialize<'de> + Validate,

source§

fn from_json_str(str: &'de str) -> Result<T, Error<Error>>

Convert from json str. Read more
source§

impl<T> FromJsonValue for Twhere T: DeserializeOwned + Validate,

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U, I> LiftInto<U, I> for Twhere U: LiftFrom<T, I>,

source§

fn lift_into(self) -> U

Performs the indexed conversion.
source§

impl<B> NotFound<B> for Bwhere B: Default,

source§

fn not_found() -> Response<B>

Available on crate feature server and (crate features http1 or http2) only.
Return a “not found” response
source§

impl<Source> Sculptor<HNil, HNil> for Source

§

type Remainder = Source

source§

fn sculpt(self) -> (HNil, <Source as Sculptor<HNil, HNil>>::Remainder)

Consumes the current HList and returns an HList with the requested shape. Read more
source§

impl<T> ToJsonString for Twhere T: Serialize + Validate,

source§

fn to_json_string(&self) -> Result<String, Error>

Convert to json string. Read more
source§

fn to_json_string_pretty(&self) -> Result<String, Error>

Convert to json pretty string. Read more
source§

impl<T> ToJsonValue for Twhere T: Serialize + Validate,

source§

fn to_json_value(&self) -> Result<Value, Error>

Convert to json string. Read more
source§

impl<T> ToJsonWriter for Twhere T: Serialize + Validate,

source§

fn to_json_writer<W>(&self, writer: W) -> Result<(), Error>where W: Write,

Convert to json writer. Read more
source§

fn to_json_writer_pretty<W>(&self, writer: W) -> Result<(), Error>where W: Write,

Convert to pretty json writer. Read more
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.
§

impl<T> Typeable for Twhere T: Any,

§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

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