typemap/
internals.rs

1use uany::{UnsafeAny, UnsafeAnyExt};
2use std::any::Any;
3use std::fmt::Debug;
4
5/// A marker trait meant for use as the `A` parameter in `TypeMap`.
6///
7/// This can be used to construct `TypeMap`s containing only types which
8/// implement `Debug` like so: `TypeMap::<DebugAny>::custom()`, which produces
9/// a `TypeMap<DebugAny>`. Combine `DebugAny` with `Send` or `Sync` to add
10/// additional bounds.
11///
12/// There is also an exported alias for this type of `TypeMap`, `DebugMap`.
13pub trait DebugAny: Any + Debug { }
14impl<T: Any + Debug> DebugAny for T { }
15
16unsafe impl UnsafeAnyExt for DebugAny {}
17unsafe impl UnsafeAnyExt for DebugAny + Send {}
18unsafe impl UnsafeAnyExt for DebugAny + Sync {}
19unsafe impl UnsafeAnyExt for DebugAny + Send + Sync {}
20
21/// A marker trait meant for use as the `A` parameter in `TypeMap`.
22///
23/// This can be used to construct `TypeMap`s containing only types which
24/// implement `Clone` like so: `TypeMap::<CloneAny>::custom()`, which produces
25/// a `TypeMap<CloneAny>`. Combine `CloneAny` with `Send` or `Sync` to add
26/// additional bounds.
27///
28/// There is also an exported alias for this type of `TypeMap`, `CloneAny`.
29pub trait CloneAny: Any {
30    #[doc(hidden)]
31    fn clone_any(&self) -> Box<CloneAny>;
32    #[doc(hidden)]
33    fn clone_any_send(&self) -> Box<CloneAny + Send> where Self: Send;
34    #[doc(hidden)]
35    fn clone_any_sync(&self) -> Box<CloneAny + Sync> where Self: Sync;
36    #[doc(hidden)]
37    fn clone_any_send_sync(&self) -> Box<CloneAny + Send + Sync> where Self: Send + Sync;
38}
39
40impl<T: Any + Clone> CloneAny for T {
41    fn clone_any(&self) -> Box<CloneAny> { Box::new(self.clone()) }
42
43    fn clone_any_send(&self) -> Box<CloneAny + Send> where Self: Send {
44        Box::new(self.clone())
45    }
46
47    fn clone_any_sync(&self) -> Box<CloneAny + Sync> where Self: Sync {
48        Box::new(self.clone())
49    }
50
51    fn clone_any_send_sync(&self) -> Box<CloneAny + Send + Sync>
52    where Self: Send + Sync {
53        Box::new(self.clone())
54    }
55}
56
57impl Clone for Box<CloneAny> {
58    fn clone(&self) -> Box<CloneAny> { (**self).clone_any() }
59}
60
61impl Clone for Box<CloneAny + Send> {
62    fn clone(&self) -> Box<CloneAny + Send> { (**self).clone_any_send() }
63}
64
65impl Clone for Box<CloneAny + Sync> {
66    fn clone(&self) -> Box<CloneAny + Sync> { (**self).clone_any_sync() }
67}
68
69impl Clone for Box<CloneAny + Send + Sync> {
70    fn clone(&self) -> Box<CloneAny + Send + Sync> { (**self).clone_any_send_sync() }
71}
72
73unsafe impl UnsafeAnyExt for CloneAny {}
74unsafe impl UnsafeAnyExt for CloneAny + Send {}
75unsafe impl UnsafeAnyExt for CloneAny + Sync {}
76unsafe impl UnsafeAnyExt for CloneAny + Send + Sync {}
77
78#[doc(hidden)] // Not actually exported
79pub unsafe trait Implements<A: ?Sized + UnsafeAnyExt> {
80    fn into_object(self) -> Box<A>;
81}
82
83unsafe impl<T: UnsafeAny> Implements<UnsafeAny> for T {
84    fn into_object(self) -> Box<UnsafeAny> { Box::new(self) }
85}
86
87unsafe impl<T: UnsafeAny + Send> Implements<(UnsafeAny + Send)> for T {
88    fn into_object(self) -> Box<UnsafeAny + Send> { Box::new(self) }
89}
90
91unsafe impl<T: UnsafeAny + Sync> Implements<(UnsafeAny + Sync)> for T {
92    fn into_object(self) -> Box<UnsafeAny + Sync> { Box::new(self) }
93}
94
95unsafe impl<T: UnsafeAny + Send + Sync> Implements<(UnsafeAny + Send + Sync)> for T {
96    fn into_object(self) -> Box<UnsafeAny + Send + Sync> { Box::new(self) }
97}
98
99unsafe impl<T: CloneAny> Implements<CloneAny> for T {
100    fn into_object(self) -> Box<CloneAny> { Box::new(self) }
101}
102
103unsafe impl<T: CloneAny + Send> Implements<(CloneAny + Send)> for T {
104    fn into_object(self) -> Box<CloneAny + Send> { Box::new(self) }
105}
106
107unsafe impl<T: CloneAny + Send + Sync> Implements<(CloneAny + Send + Sync)> for T {
108    fn into_object(self) -> Box<CloneAny + Send + Sync> { Box::new(self) }
109}
110
111unsafe impl<T: DebugAny> Implements<DebugAny> for T {
112    fn into_object(self) -> Box<DebugAny> { Box::new(self) }
113}
114
115unsafe impl<T: DebugAny + Send> Implements<DebugAny + Send> for T {
116    fn into_object(self) -> Box<DebugAny + Send> { Box::new(self) }
117}
118
119unsafe impl<T: DebugAny + Sync> Implements<DebugAny + Sync> for T {
120    fn into_object(self) -> Box<DebugAny + Sync> { Box::new(self) }
121}
122
123unsafe impl<T: DebugAny + Send + Sync> Implements<DebugAny + Send + Sync> for T {
124    fn into_object(self) -> Box<DebugAny + Send + Sync> { Box::new(self) }
125}