AnyValue

Struct AnyValue 

Source
pub struct AnyValue(/* private fields */);
Expand description

AnyValue can save values of any type. In internal it save the value into a vector of type T.

§Example

let mut value = AnyValue::new();

assert_eq!(value.contain_type::<i32>(), false);
assert_eq!(value.set(vec![42]), None);
assert_eq!(value.contain_type::<i32>(), true);

assert_eq!(value.val::<i32>()?, &42);
assert_eq!(value.push(256).val::<i32>()?, &256);
value.val_mut::<i32>()?.sub_assign(128);
assert_eq!(value.val::<i32>()?, &128);

assert_eq!(value.vals::<i32>()?, &vec![42, 128]);
assert_eq!(value.pop::<i32>(), Some(128));
assert_eq!(value.vals::<i32>()?, &vec![42]);

value.entry::<u64>().or_insert(vec![9, 0, 2, 5]);
assert_eq!(value.entry::<u64>().or_default().pop(), Some(5));

value.vals_mut::<i32>()?.pop();
assert_eq!(value.vals::<i32>()?.len(), 0);

assert_eq!(value.remove::<u64>(), Some(vec![9, 0, 2]));
assert_eq!(value.contain_type::<u64>(), false);

Implementations§

Source§

impl AnyValue

Source

pub fn new() -> Self

Source

pub fn clear(&mut self)

Source

pub fn contain_type<T: ErasedTy>(&self) -> bool

Source

pub fn pop<T: ErasedTy>(&mut self) -> Option<T>

Source

pub fn entry<T: ErasedTy>(&mut self) -> Entry<'_, Vec<T>>

Source

pub fn push<T: ErasedTy>(&mut self, val: T) -> &mut Self

Push a value to the values of type T.

Source

pub fn set<T: ErasedTy>(&mut self, vals: Vec<T>) -> Option<Vec<T>>

Set the values of type T.

Source

pub fn remove<T: ErasedTy>(&mut self) -> Option<Vec<T>>

Remove the values of type T.

Source

pub fn val<T: ErasedTy>(&self) -> Result<&T, Error>

Get the last value reference of type T.

Source

pub fn val_mut<T: ErasedTy>(&mut self) -> Result<&mut T, Error>

Get the last value mutable reference of type T.

Source

pub fn vals<T: ErasedTy>(&self) -> Result<&Vec<T>, Error>

Get the values of type T.

Source

pub fn vals_mut<T: ErasedTy>(&mut self) -> Result<&mut Vec<T>, Error>

Get the values of type T.

Trait Implementations§

Source§

impl Debug for AnyValue

Source§

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

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

impl Default for AnyValue

Source§

fn default() -> AnyValue

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

Auto Trait Implementations§

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<'a, C, T> DynamicCtorThenBuilderHelper<'a, C> for T
where C: Match<'a>,

Source§

fn into_ctor_then_builder<F, O1, R>( self, func: F, ) -> DynamicCtorThenBuilder<C, T, F>
where F: Fn(&mut C, &O1) -> Result<R, Error>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

impl<T, K> WithDefaultHelper<T> for K

Source§

fn with_default<F>(self, func: F) -> WithDefault<T, F, K>
where F: Fn() -> T,

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> ErasedTy for T
where T: Any + Debug + Sync + Send + 'static,

Source§

impl<T> MayDebug for T