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
use std::any::Any;

/// Trait to be implemented by user-defined data.
pub trait UserData: Any + Send + Sync {
    /// Clone this trait-object.
    fn clone_boxed(&self) -> Box<dyn UserData>;
    /// Clone as its super-trait trait objects.
    fn to_any(&self) -> Box<dyn Any + Send + Sync>;
    /// Downcast to Any.
    fn as_any(&self) -> &(dyn Any + Send + Sync);
}

impl<T: Clone + Any + Send + Sync> UserData for T {
    #[inline]
    fn clone_boxed(&self) -> Box<dyn UserData> {
        Box::new(self.clone())
    }

    #[inline]
    fn to_any(&self) -> Box<dyn Any + Send + Sync> {
        Box::new(self.clone())
    }

    #[inline]
    fn as_any(&self) -> &(dyn Any + Send + Sync) {
        self
    }
}

// We need this because we must not implement Clone for Box<UserData>
// directly otherwise Box<UserData> would implement UserData too and
// we want to avoid the user mistakenly nesting user-datas.
pub(crate) struct UserDataBox(pub Box<dyn UserData>);

impl Clone for UserDataBox {
    #[inline]
    fn clone(&self) -> Self {
        UserDataBox(self.0.clone_boxed())
    }
}