MaybeUndefined

Enum MaybeUndefined 

Source
pub enum MaybeUndefined<T> {
    Undefined,
    Null,
    Value(T),
}
Expand description

Similar to Option, but it has three states, undefined, null and x.

When using with Serde, you will likely want to skip serialization of undefined and add a default for deserialization.

§Example

use agent_client_protocol_schema::MaybeUndefined;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
struct A {
    #[serde(default, skip_serializing_if = "MaybeUndefined::is_undefined")]
    a: MaybeUndefined<i32>,
}

Variants§

§

Undefined

§

Null

§

Value(T)

Implementations§

Source§

impl<T> MaybeUndefined<T>

Source

pub const fn is_undefined(&self) -> bool

Returns true if the MaybeUndefined<T> is undefined.

Source

pub const fn is_null(&self) -> bool

Returns true if the MaybeUndefined<T> is null.

Source

pub const fn is_value(&self) -> bool

Returns true if the MaybeUndefined<T> contains value.

Source

pub const fn value(&self) -> Option<&T>

Borrow the value, returns None if the the MaybeUndefined<T> is undefined or null, otherwise returns Some(T).

Source

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

Converts the MaybeUndefined<T> to Option<T>.

Source

pub const fn as_opt_ref(&self) -> Option<Option<&T>>

Converts the MaybeUndefined<T> to Option<Option<T>>.

Source

pub fn as_opt_deref<U>(&self) -> Option<Option<&U>>
where T: Deref<Target = U>, U: ?Sized,

Converts the MaybeUndefined<T> to Option<Option<&U>>.

Source

pub fn contains_value<U>(&self, x: &U) -> bool
where U: PartialEq<T>,

Returns true if the MaybeUndefined<T> contains the given value.

Source

pub fn contains<U>(&self, x: Option<&U>) -> bool
where U: PartialEq<T>,

Returns true if the MaybeUndefined<T> contains the given nullable value.

Source

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

Maps a MaybeUndefined<T> to MaybeUndefined<U> by applying a function to the contained nullable value

Source

pub fn map_value<U, F>(self, f: F) -> MaybeUndefined<U>
where F: FnOnce(T) -> U,

Maps a MaybeUndefined<T> to MaybeUndefined<U> by applying a function to the contained value

Source

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

Update value if the MaybeUndefined<T> is not undefined.

§Example
use agent_client_protocol_schema::MaybeUndefined;

let mut value = None;

MaybeUndefined::Value(10i32).update_to(&mut value);
assert_eq!(value, Some(10));

MaybeUndefined::Undefined.update_to(&mut value);
assert_eq!(value, Some(10));

MaybeUndefined::Null.update_to(&mut value);
assert_eq!(value, None);
Source§

impl<T, E> MaybeUndefined<Result<T, E>>

Source

pub fn transpose(self) -> Result<MaybeUndefined<T>, E>

Transposes a MaybeUndefined of a Result into a Result of a MaybeUndefined.

MaybeUndefined::Undefined will be mapped to Ok(MaybeUndefined::Undefined). MaybeUndefined::Null will be mapped to Ok(MaybeUndefined::Null). MaybeUndefined::Value(Ok(_)) and MaybeUndefined::Value(Err(_)) will be mapped to Ok(MaybeUndefined::Value(_)) and Err(_).

§Errors

Returns an error if the input is MaybeUndefined::Value(Err(_)).

Trait Implementations§

Source§

impl<T> Clone for MaybeUndefined<T>
where T: Clone,

Source§

fn clone(&self) -> MaybeUndefined<T>

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for MaybeUndefined<T>
where T: Debug,

Source§

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

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

impl<T> Default for MaybeUndefined<T>

Source§

fn default() -> MaybeUndefined<T>

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

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

Source§

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

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

impl<T> From<Option<Option<T>>> for MaybeUndefined<T>

Source§

fn from(value: Option<Option<T>>) -> MaybeUndefined<T>

Converts to this type from the input type.
Source§

impl<T> Hash for MaybeUndefined<T>
where T: Hash,

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> IntoMaybeUndefined<T> for MaybeUndefined<T>

Source§

impl<T> JsonSchema for MaybeUndefined<T>
where T: JsonSchema,

Source§

fn schema_name() -> Cow<'static, str>

The name of the generated JSON Schema. Read more
Source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

fn json_schema(generator: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
Source§

fn inline_schema() -> bool

Whether JSON Schemas generated for this type should be included directly in parent schemas, rather than being re-used where possible using the $ref keyword. Read more
Source§

impl<T> Ord for MaybeUndefined<T>
where T: Ord,

Source§

fn cmp(&self, other: &MaybeUndefined<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T> PartialEq for MaybeUndefined<T>
where T: PartialEq,

Source§

fn eq(&self, other: &MaybeUndefined<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> PartialOrd for MaybeUndefined<T>
where T: PartialOrd,

Source§

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

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

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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

Source§

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

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

impl<T> Copy for MaybeUndefined<T>
where T: Copy,

Source§

impl<T> Eq for MaybeUndefined<T>
where T: Eq,

Source§

impl<T> StructuralPartialEq for MaybeUndefined<T>

Auto Trait Implementations§

§

impl<T> Freeze for MaybeUndefined<T>
where T: Freeze,

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

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> IntoMaybeUndefined<T> for T

Source§

impl<T> IntoOption<T> for T

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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