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
use std::any::TypeId;
use std::sync::Arc;

pub trait Inject: 'static {}

pub trait InjectExt: Inject + Default {
    fn inject(container: &crate::Container) -> Result<Self, crate::InjectError> {
        Ok(Self::default())
    }
}

pub fn id<T: 'static>() -> TypeId {
    TypeId::of::<T>()
}

impl<T: 'static> Inject for T {}
impl<T: Inject + Default> InjectExt for T {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::inject::Inject;

    #[derive(Debug, PartialEq, Clone, Copy)]
    struct FakeImpl {
        val: isize,
    }

    #[derive(Debug, PartialEq, Clone, Copy)]
    struct FakeImpl2 {
        val: isize,
    }

    trait FakeTrait: Sized + Send {}

    impl FakeTrait for FakeImpl {}

    #[test]
    fn test_reference_of_type_does_not_share_type_id_with_type() {
        assert_ne!(id::<FakeImpl>(), id::<Arc<FakeImpl>>())
    }

    #[test]
    fn test_references_of_different_types_do_not_share_type_id() {
        assert_ne!(id::<Arc<FakeImpl>>(), id::<Arc<FakeImpl2>>())
    }
}