[−][src]Struct value_bag::ValueBag
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
, andStream
trait implementations. - Using the
ValueBag::to_*
methods. - Using the
ValueBag::downcast_ref
method.
Implementations
impl<'v> ValueBag<'v>
[src]
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]
T: Error + 'static,
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]
T: Debug + 'static,
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]
T: Display + 'static,
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]
T: Serialize + 'static,
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]
T: Value + 'static,
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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Serialize for T where
T: Serialize + ?Sized,
[src]
T: Serialize + ?Sized,
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
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>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,