1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use std::any::Any;
use std::time::Instant;
use {Context, ContextError};
use futures::{Future, Poll, Async};

pub struct WithValue<V, C>
    where C: Context,
          V: Any
{
    parent: Box<C>,
    val: V,
}

impl<V, C> Context for WithValue<V, C>
    where C: Context,
          V: Any
{
    fn deadline(&self) -> Option<Instant> {
        None
    }

    fn value<T>(&self) -> Option<&T>
        where T: Any
    {
        let val_any = &self.val as &Any;
        val_any.downcast_ref::<T>().or_else(|| self.parent.as_ref().value())
    }
}

impl<V, C> Future for WithValue<V, C>
    where C: Context,
          V: Any
{
    type Item = ();
    type Error = ContextError;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        Ok(Async::NotReady)
    }
}

/// 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));
/// ```
pub fn with_value<V, C>(parent: C, val: V) -> WithValue<V, C>
    where C: Context,
          V: Any
{
    WithValue {
        parent: Box::new(parent),
        val: val,
    }
}


#[cfg(test)]
mod test {
    use with_value::with_value;
    use {Context, background};

    #[test]
    fn same_type_test() {
        let a = with_value(background(), 1);
        let b = with_value(a, 2);
        assert_eq!(b.value(), Some(&2));
    }

    #[test]
    fn same_type_workaround_test() {
        #[derive(Debug, PartialEq)]
        struct A(i32);
        #[derive(Debug, PartialEq)]
        struct B(i32);
        let a = with_value(background(), B(1));
        let b = with_value(a, B(2));
        assert_eq!(b.value(), Some(&B(2)));
    }
}