AnyContext

Trait AnyContext 

Source
pub trait AnyContext
where Self: Context<Value = Box<DynAnyValue>, Entry: Entry<ValueBorrowed = DynAnyValue>>,
{ // Provided method fn value_as<T, Q>(&self, key: &Q) -> Option<&T> where <Self::Entry as Entry>::KeyBorrowed: Borrow<Q>, Q: Debug + Eq + Hash + ?Sized, T: Any { ... } }
Expand description

The context where each entry’s value is a Box<DynAnyValue>.

Accessing the entry it contains and conveniently casting the result to a concrete type can be done with the method offered by the AnyContext trait.

For contexts implementing this trait, refer to the crate::context::map module.

§Example

// `LiteralKeyAnyMapContext` implements `AnyContext`.
let mut context = LiteralKeyAnyMapContext::new();
context.insert_with::<BoxConverter, _, _>("error-code", 42i32);
context.insert_with::<BoxConverter, _, _>("cause", "unknown");
assert_eq!(context.value_as::<i32, _>("error-code"), Some(&42i32));
assert_eq!(context.value_as::<&str, _>("cause"), Some(&"unknown"));

Provided Methods§

Source

fn value_as<T, Q>(&self, key: &Q) -> Option<&T>
where <Self::Entry as Entry>::KeyBorrowed: Borrow<Q>, Q: Debug + Eq + Hash + ?Sized, T: Any,

Returns the value corresponding to the given key and tries to cast it to the type T. Returns None if the entry doesn’t exist or the downcasting fails.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<K, KB> AnyContext for AnyMapContext<K, KB>
where K: Borrow<KB> + Debug + Send + Sync + 'static, KB: Debug + Display + Eq + Hash + ?Sized + Send + Sync,