[][src]Struct value_bag::ValueBag

pub struct ValueBag<'v> { /* fields omitted */ }

A dynamic structured value.

Capturing values

There are a few ways to capture a value:

  • Using the ValueBag::capture_* methods.
  • Using the standard From trait.
  • Using the Fill API.

Using the ValueBag::capture_* methods

ValueBag offers a few constructor methods that capture values of different kinds. These methods require a T: 'static to support downcasting.

use value_bag::ValueBag;

let value = ValueBag::capture_debug(&42i32);

assert_eq!(Some(42), value.to_i32());

Using the standard From trait

Standard types that implement ToValue also implement From.

use value_bag::ValueBag;

let value = ValueBag::from(42i32);

assert_eq!(Some(42), value.to_i32());
use value_bag::ValueBag;

let value = ValueBag::from(&42i32 as &dyn Debug);

assert_eq!(None, value.to_i32());

Using the Fill API

The Fill trait is a way to bridge APIs that may not be directly compatible with other constructor methods.

use value_bag::{ValueBag, Slot, Fill, Error};

struct FillSigned;

impl Fill for FillSigned {
    fn fill(&self, slot: &mut Slot) -> Result<(), Error> {
        slot.fill_any(42i32)
    }
}

let value = ValueBag::from_fill(&FillSigned);

assert_eq!(Some(42), value.to_i32());
use value_bag::{ValueBag, Slot, Fill, Error};

struct FillDebug;

impl Fill for FillDebug {
    fn fill(&self, slot: &mut Slot) -> Result<(), Error> {
        slot.fill_debug(&42i32 as &dyn Debug)
    }
}

let value = ValueBag::from_fill(&FillDebug);

assert_eq!(None, value.to_i32());

Inspecting values

Once you have a ValueBag there are also a few ways to inspect it:

  • Using the Debug, Display, Serialize, and Stream trait implementations.
  • Using the ValueBag::to_* methods.
  • Using the ValueBag::downcast_ref method.

Implementations

impl<'v> ValueBag<'v>[src]

pub fn from_fill<T>(value: &'v T) -> Self where
    T: Fill
[src]

Get a value from a fillable slot.

impl<'v> ValueBag<'v>[src]

pub fn to_str(&self) -> Option<Cow<'_, str>>[src]

Try get a str from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones. If the serialization implementation produces a short lived string it will be allocated.

impl<'v> ValueBag<'v>[src]

pub fn to_usize(&self) -> Option<usize>[src]

Try get a usize from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_u8(&self) -> Option<u8>[src]

Try get a u8 from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_u16(&self) -> Option<u16>[src]

Try get a u16 from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_u32(&self) -> Option<u32>[src]

Try get a u32 from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_u64(&self) -> Option<u64>[src]

Try get a u64 from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_isize(&self) -> Option<isize>[src]

Try get a isize from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_i8(&self) -> Option<i8>[src]

Try get a i8 from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_i16(&self) -> Option<i16>[src]

Try get a i16 from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_i32(&self) -> Option<i32>[src]

Try get a i32 from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_i64(&self) -> Option<i64>[src]

Try get a i64 from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_f32(&self) -> Option<f32>[src]

Try get a f32 from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_f64(&self) -> Option<f64>[src]

Try get a f64 from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_bool(&self) -> Option<bool>[src]

Try get a bool from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_char(&self) -> Option<char>[src]

Try get a char from this value.

This method is cheap for primitive types, but may call arbitrary serialization implementations for complex ones.

pub fn to_borrowed_str(&self) -> Option<&str>[src]

Try get a str from this value.

This method is cheap for primitive types. It won't allocate an owned String if the value is a complex type.

pub fn is<T: 'static>(&self) -> bool[src]

Check whether this value can be downcast to T.

pub fn downcast_ref<T: 'static>(&self) -> Option<&T>[src]

Try downcast this value to T.

impl<'v> ValueBag<'v>[src]

pub fn capture_error<T>(value: &'v T) -> Self where
    T: Error + 'static, 
[src]

Get a value from an error.

pub fn to_error<'a>(&'a self) -> Option<impl Error + 'a>[src]

Try get an error from this value.

impl<'v> ValueBag<'v>[src]

pub fn capture_debug<T>(value: &'v T) -> Self where
    T: Debug + 'static, 
[src]

Get a value from a debuggable type.

This method will attempt to capture the given value as a well-known primitive before resorting to using its Debug implementation.

pub fn capture_display<T>(value: &'v T) -> Self where
    T: Display + 'static, 
[src]

Get a value from a displayable type.

This method will attempt to capture the given value as a well-known primitive before resorting to using its Display implementation.

impl<'v> ValueBag<'v>[src]

pub fn capture_serde<T>(value: &'v T) -> Self where
    T: Serialize + 'static, 
[src]

Get a value from a structured type.

This method will attempt to capture the given value as a well-known primitive before resorting to using its Value implementation.

impl<'v> ValueBag<'v>[src]

pub fn capture_sval<T>(value: &'v T) -> Self where
    T: Value + 'static, 
[src]

Get a value from a structured type.

This method will attempt to capture the given value as a well-known primitive before resorting to using its Value implementation.

Trait Implementations

impl<'v> Clone for ValueBag<'v>[src]

impl<'v> Debug for ValueBag<'v>[src]

impl<'v> Display for ValueBag<'v>[src]

impl<'v> From<&'v (dyn Debug + 'v)> for ValueBag<'v>[src]

impl<'v> From<&'v (dyn Display + 'v)> for ValueBag<'v>[src]

impl<'v> From<&'v (dyn Error + 'v)> for ValueBag<'v>[src]

impl<'v> From<&'v (dyn Serialize + 'v)> for ValueBag<'v>[src]

impl<'v> From<&'v (dyn Value + 'v)> for ValueBag<'v>[src]

impl<'v> From<&'v str> for ValueBag<'v>[src]

impl<'v> From<()> for ValueBag<'v>[src]

impl<'v> From<bool> for ValueBag<'v>[src]

impl<'v> From<char> for ValueBag<'v>[src]

impl<'v> From<f32> for ValueBag<'v>[src]

impl<'v> From<f64> for ValueBag<'v>[src]

impl<'v> From<i16> for ValueBag<'v>[src]

impl<'v> From<i32> for ValueBag<'v>[src]

impl<'v> From<i64> for ValueBag<'v>[src]

impl<'v> From<i8> for ValueBag<'v>[src]

impl<'v> From<isize> for ValueBag<'v>[src]

impl<'v> From<u16> for ValueBag<'v>[src]

impl<'v> From<u32> for ValueBag<'v>[src]

impl<'v> From<u64> for ValueBag<'v>[src]

impl<'v> From<u8> for ValueBag<'v>[src]

impl<'v> From<usize> for ValueBag<'v>[src]

impl<'v> Serialize for ValueBag<'v>[src]

impl<'v> Value for ValueBag<'v>[src]

Auto Trait Implementations

impl<'v> !RefUnwindSafe for ValueBag<'v>

impl<'v> !Send for ValueBag<'v>

impl<'v> !Sync for ValueBag<'v>

impl<'v> Unpin for ValueBag<'v>

impl<'v> !UnwindSafe for ValueBag<'v>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Serialize for T where
    T: Serialize + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.