Struct relay_protocol::Annotated

source ·
pub struct Annotated<T>(pub Option<T>, pub Meta);
Expand description

Wrapper for data fields with optional meta data.

Tuple Fields§

§0: Option<T>§1: Meta

Implementations§

source§

impl<T> Annotated<T>

source

pub fn new(value: T) -> Self

Creates a new annotated value without meta data.

source

pub fn empty() -> Self

Creates an empty annotated value without meta data.

source

pub fn from_error<E>(err: E, value: Option<Value>) -> Self
where E: Into<Error>,

Creates an empty annotated value with error attached.

source

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

Returns a reference to the value.

Returns None if this value is not initialized, missing, or invalid.

source

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

Returns a mutable reference to the value.

Returns None if this value is not initialized, missing, or invalid.

source

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

Replaces the stored value.

source

pub fn meta(&self) -> &Meta

Returns a reference to the meta data attached to this value.

source

pub fn meta_mut(&mut self) -> &mut Meta

Returns a mutable reference to the meta data attached to this value.

source

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

Returns the stored value and drops all meta data.

source

pub fn and_then<F, U, R>(self, f: F) -> Annotated<U>
where F: FnOnce(T) -> R, R: Into<Annotated<U>>,

Calls f with the stored value if available and merges meta data into the result.

source

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

Maps an Annotated<T> to an Annotated<U> and keeps the original meta data.

source

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

Inserts a value computed from f into the value if it is None, then returns a mutable reference to the contained value.

source

pub fn merge(&mut self, other: Annotated<T>, block: impl FnOnce(&mut T, T))

Merges the supplied Annotated in the left Annotated.

source§

impl<T> Annotated<T>
where T: AsRef<str>,

source

pub fn as_str(&self) -> Option<&str>

Returns a reference to the string value if set.

source§

impl Annotated<Value>

source

pub fn as_str(&self) -> Option<&str>

Returns a reference to the string value if this value is a string and it is set.

source§

impl<T> Annotated<T>
where T: Empty,

source

pub fn skip_serialization(&self, behavior: SkipSerialization) -> bool

Returns whether this value should be skipped during serialization.

An Annotated<T> is always serialized if it has meta data. Otherwise, serialization depends on the behavior. For SkipSerialization::Empty, the Empty trait is used to determine emptiness of the contained value and defaults to false for no value.

source§

impl<T> Annotated<T>
where T: FromValue,

source

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

Deserializes an annotated from a deserializer

source

pub fn from_json(s: &str) -> Result<Self, Error>

Deserializes an annotated from a JSON string.

source

pub fn from_json_bytes(b: &[u8]) -> Result<Self, Error>

Deserializes an annotated from JSON bytes.

source§

impl<T> Annotated<T>
where T: IntoValue,

source

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

Serializes an annotated value into a serializer.

source

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

Serializes an annotated value into a JSON string.

source

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

Serializes an annotated value into a pretty JSON string.

source

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

Serializes an annotated value into a JSON string.

source

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

Serializes an annotated value into a pretty JSON string.

Trait Implementations§

source§

impl<T: Clone> Clone for Annotated<T>

source§

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

source§

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

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

impl<T> Default for Annotated<T>

source§

fn default() -> Annotated<T>

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

impl<T> Empty for Annotated<T>
where T: Empty,

Checks whether annotated data contains either a value or meta data.

source§

fn is_empty(&self) -> bool

Returns if this object contains data to be serialized.

Caution: This has different behavior than annotated.value().is_empty()! Annotated is not empty if there is meta data that needs to be serialized. This is in line with the derived implementation of Empty on structs, which calls Annotated::is_empty on every child.

To check if a value is missing or empty, use Option::is_empty on the value instead.

source§

fn is_deep_empty(&self) -> bool

Returns if this object contains nested data to be serialized.

source§

impl From<Annotated<Value>> for Value

source§

fn from(value: Annotated<Value>) -> Value

Converts to this type from the input type.
source§

impl<T> From<Option<T>> for Annotated<T>

source§

fn from(option: Option<T>) -> Self

Converts to this type from the input type.
source§

impl<T> From<T> for Annotated<T>

source§

fn from(t: T) -> Self

Converts to this type from the input type.
source§

impl From<Value> for Annotated<Value>

source§

fn from(value: Value) -> Annotated<Value>

Converts to this type from the input type.
source§

impl<T: PartialEq> PartialEq for Annotated<T>

source§

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

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

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> StructuralPartialEq for Annotated<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

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

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 T
where 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 T
where T: Clone,

source§

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 T
where U: Into<T>,

source§

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 T
where U: TryFrom<T>,

source§

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.