Value

Struct Value 

Source
pub struct Value<'val, 'ty>(/* private fields */)
where
    'ty: 'val;
Expand description

A container for a type-erased value.

Value is, in essence, a smart-pointer that can hold any reflected type. It is slightly larger than a standard fat-pointer, since it contains metadata about the type it contains.

Implementations§

Source§

impl<'val, 'ty> Value<'val, 'ty>

Source

pub fn from_vec(s: &'val (dyn VecInstance<'ty> + 'ty)) -> Value<'val, 'ty>

Creates a Value from a reflected Vec

Source

pub fn from_struct(s: &'val (dyn StructInstance<'ty> + 'ty)) -> Value<'val, 'ty>

Creates a Value from a reflected struct

Source

pub fn from_enum(s: &'val (dyn EnumInstance<'ty> + 'ty)) -> Value<'val, 'ty>

Creates a Value from a reflected enum

Source

pub fn from_option(s: &'val (dyn OptionInstance<'ty> + 'ty)) -> Value<'val, 'ty>

Creates a Value from a reflected option

Source

pub fn borrow<'b, T: FromValue<'val, 'ty>>(&'b self) -> Option<T>
where 'val: 'b, 'ty: 'b,

Borrows the value as an underlying type. This will return None, when the runtime type-check fails

Source

pub fn into<T: FromValue<'val, 'ty> + 'ty>(self) -> Option<T>

Consumes the Value and returns the internal T.

This only works for types that implement FromValue, which are generally primitives that implement Copy.

Source

pub fn as_reflected_vec(&self) -> Option<&'val (dyn VecInstance<'ty> + 'ty)>

Attempts to cast Value to a VecInstance

Source

pub fn as_reflected_struct( &self, ) -> Option<&'val (dyn StructInstance<'ty> + 'ty)>

Attempts to cast Value to a StructInstance

Source

pub fn as_reflected_enum(&'val self) -> Option<&(dyn EnumInstance<'ty> + 'ty)>

Attempts to cast Value to a EnumInstance

Source

pub fn as_reflected_option( &'val self, ) -> Option<&(dyn OptionInstance<'ty> + 'ty)>

Attempts to cast Value to a OptionInstance

Source

pub fn to_owned(&self) -> OwnedValue<'ty>

Clones the underlying value, and returns a OwnedValue

Source

pub fn try_hash<H: Hasher>(&self, hasher: H) -> Option<u64>

Attempts to has the Value, only returning a value if the underlying type is hashable

Source

pub fn slow_eq<'c, 'd>(&self, r: &Value<'c, 'd>) -> bool

Checks if two Values are equal.

This is slower than the implementations in PartialEq, but it works with Values of different lifetimes. It is slower, because it is implemented using reflection calls, rather than downcasting.

Trait Implementations§

Source§

impl<'val, 'ty> Clone for Value<'val, 'ty>
where 'ty: 'val,

Source§

fn clone(&self) -> Value<'val, 'ty>

Returns a duplicate 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 Debug for Value<'_, '_>

Source§

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

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

impl<'val, 'ty> From<&'val Bytes> for Value<'val, 'ty>

Source§

fn from(bytes: &'val Bytes) -> Self

Converts to this type from the input type.
Source§

impl<'val, 'ty> From<Value<'val, 'ty>> for CowValue<'val, 'ty>

Source§

fn from(v: Value<'val, 'ty>) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Value<'_, '_>

Source§

fn eq(&self, other: &Self) -> 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.

Auto Trait Implementations§

§

impl<'val, 'ty> Freeze for Value<'val, 'ty>

§

impl<'val, 'ty> !RefUnwindSafe for Value<'val, 'ty>

§

impl<'val, 'ty> Send for Value<'val, 'ty>

§

impl<'val, 'ty> Sync for Value<'val, 'ty>

§

impl<'val, 'ty> Unpin for Value<'val, 'ty>

§

impl<'val, 'ty> !UnwindSafe for Value<'val, 'ty>

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