Function ctx::with_value [] [src]

pub fn with_value<V>(parent: Context, val: V) -> Context where
    V: Any

Returns a copy of parent, but with the given value associated to it.

Context values should only be used for request-scoped data that transists processes and API boundaries and not for passing optional parameters to functions.

It is recommended to use structs as values instead of simple data types like strings and ints to be very specific of what result to expect when retrieving a value. Having values of the same data type among the ancestors would always return the first hit.

Examples

use ctx::{Context, with_value, background};

let a = with_value(background(), 42);
let b = with_value(a, 1.0);
assert_eq!(b.value(), Some(42));
assert_eq!(b.value(), Some(1.0));