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
use core::any::Any;
use core::fmt::{Debug, Display};

/// A trait that makes use of string like types (`String` and `&str`)
/// easier.
///
pub trait StringLike: Into<String> + AsRef<str> {
    /// Calling `into` will consume the object,
    /// calling `as_ref` only gives borrowed string.
    /// This combines them together to give a owned string
    /// without consuming the value.
    ///
    fn clone_string(&self) -> String {
        self.as_ref().to_string()
    }
}
impl<T> StringLike for T where T: Into<String> + AsRef<str> {}

/// A trait for field access
///
pub trait Access<'a, Source> {
    type Target: 'a;
    fn access<'b>(&self, src: &'b Source) -> Self::Target
    where
        'b: 'a;
}
pub trait AccessMut<'a, Source> {
    type Target: 'a;
    fn access_mut<'b>(&self, src: &'b mut Source) -> Self::Target
    where
        'b: 'a;
}

/// A wrapper trait for wrapped values.
/// It represents any types that are both `Any` and `Debug`.
///
pub trait AnyDebugDisplay: Any + Debug + Display {
    /// Obtain a trait object for `Any`
    fn as_any(&self) -> &dyn Any;
    /// Obtain a trait object for `Debug`
    fn as_debug(&self) -> &dyn Debug;
    /// Obtain a trait object for `Display`
    fn as_display(&self) -> &dyn Display;
    /// Turn a concret box into a box of `Any`
    fn into_boxed_any(self: Box<Self>) -> Box<dyn Any>;
}
impl<T> AnyDebugDisplay for T
where
    T: Any + Debug + Display,
{
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn as_debug(&self) -> &dyn Debug {
        self
    }
    fn as_display(&self) -> &dyn Display {
        self
    }
    fn into_boxed_any(self: Box<Self>) -> Box<dyn Any> {
        self
    }
}