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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use uany::{UnsafeAny, UnsafeAnyExt};
use std::any::Any;
use std::fmt::Debug;

/// A marker trait meant for use as the `A` parameter in `TypeMap`.
///
/// This can be used to construct `TypeMap`s containing only types which
/// implement `Debug` like so: `TypeMap::<DebugAny>::custom()`, which produces
/// a `TypeMap<DebugAny>`. Combine `DebugAny` with `Send` or `Sync` to add
/// additional bounds.
///
/// There is also an exported alias for this type of `TypeMap`, `DebugMap`.
pub trait DebugAny: Any + Debug { }
impl<T: Any + Debug> DebugAny for T { }

unsafe impl UnsafeAnyExt for DebugAny {}
unsafe impl UnsafeAnyExt for DebugAny + Send {}
unsafe impl UnsafeAnyExt for DebugAny + Sync {}
unsafe impl UnsafeAnyExt for DebugAny + Send + Sync {}

/// A marker trait meant for use as the `A` parameter in `TypeMap`.
///
/// This can be used to construct `TypeMap`s containing only types which
/// implement `Clone` like so: `TypeMap::<CloneAny>::custom()`, which produces
/// a `TypeMap<CloneAny>`. Combine `CloneAny` with `Send` or `Sync` to add
/// additional bounds.
///
/// There is also an exported alias for this type of `TypeMap`, `CloneAny`.
pub trait CloneAny: Any {
    #[doc(hidden)]
    fn clone_any(&self) -> Box<CloneAny>;
    #[doc(hidden)]
    fn clone_any_send(&self) -> Box<CloneAny + Send> where Self: Send;
    #[doc(hidden)]
    fn clone_any_sync(&self) -> Box<CloneAny + Sync> where Self: Sync;
    #[doc(hidden)]
    fn clone_any_send_sync(&self) -> Box<CloneAny + Send + Sync> where Self: Send + Sync;
}

impl<T: Any + Clone> CloneAny for T {
    fn clone_any(&self) -> Box<CloneAny> { Box::new(self.clone()) }

    fn clone_any_send(&self) -> Box<CloneAny + Send> where Self: Send {
        Box::new(self.clone())
    }

    fn clone_any_sync(&self) -> Box<CloneAny + Sync> where Self: Sync {
        Box::new(self.clone())
    }

    fn clone_any_send_sync(&self) -> Box<CloneAny + Send + Sync>
    where Self: Send + Sync {
        Box::new(self.clone())
    }
}

impl Clone for Box<CloneAny> {
    fn clone(&self) -> Box<CloneAny> { (**self).clone_any() }
}

impl Clone for Box<CloneAny + Send> {
    fn clone(&self) -> Box<CloneAny + Send> { (**self).clone_any_send() }
}

impl Clone for Box<CloneAny + Sync> {
    fn clone(&self) -> Box<CloneAny + Sync> { (**self).clone_any_sync() }
}

impl Clone for Box<CloneAny + Send + Sync> {
    fn clone(&self) -> Box<CloneAny + Send + Sync> { (**self).clone_any_send_sync() }
}

unsafe impl UnsafeAnyExt for CloneAny {}
unsafe impl UnsafeAnyExt for CloneAny + Send {}
unsafe impl UnsafeAnyExt for CloneAny + Sync {}
unsafe impl UnsafeAnyExt for CloneAny + Send + Sync {}

#[doc(hidden)] // Not actually exported
pub unsafe trait Implements<A: ?Sized + UnsafeAnyExt> {
    fn into_object(self) -> Box<A>;
}

unsafe impl<T: UnsafeAny> Implements<UnsafeAny> for T {
    fn into_object(self) -> Box<UnsafeAny> { Box::new(self) }
}

unsafe impl<T: UnsafeAny + Send> Implements<(UnsafeAny + Send)> for T {
    fn into_object(self) -> Box<UnsafeAny + Send> { Box::new(self) }
}

unsafe impl<T: UnsafeAny + Sync> Implements<(UnsafeAny + Sync)> for T {
    fn into_object(self) -> Box<UnsafeAny + Sync> { Box::new(self) }
}

unsafe impl<T: UnsafeAny + Send + Sync> Implements<(UnsafeAny + Send + Sync)> for T {
    fn into_object(self) -> Box<UnsafeAny + Send + Sync> { Box::new(self) }
}

unsafe impl<T: CloneAny> Implements<CloneAny> for T {
    fn into_object(self) -> Box<CloneAny> { Box::new(self) }
}

unsafe impl<T: CloneAny + Send> Implements<(CloneAny + Send)> for T {
    fn into_object(self) -> Box<CloneAny + Send> { Box::new(self) }
}

unsafe impl<T: CloneAny + Send + Sync> Implements<(CloneAny + Send + Sync)> for T {
    fn into_object(self) -> Box<CloneAny + Send + Sync> { Box::new(self) }
}

unsafe impl<T: DebugAny> Implements<DebugAny> for T {
    fn into_object(self) -> Box<DebugAny> { Box::new(self) }
}

unsafe impl<T: DebugAny + Send> Implements<DebugAny + Send> for T {
    fn into_object(self) -> Box<DebugAny + Send> { Box::new(self) }
}

unsafe impl<T: DebugAny + Sync> Implements<DebugAny + Sync> for T {
    fn into_object(self) -> Box<DebugAny + Sync> { Box::new(self) }
}

unsafe impl<T: DebugAny + Send + Sync> Implements<DebugAny + Send + Sync> for T {
    fn into_object(self) -> Box<DebugAny + Send + Sync> { Box::new(self) }
}