neon/sys/
debug_send_wrapper.rs

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
//! Wrapper that ensures types are always used from the same thread
//! in debug builds. It is a zero-cost in release builds.

pub(super) use wrapper::DebugSendWrapper;

#[cfg(debug_assertions)]
mod wrapper {
    use std::ops::Deref;

    #[repr(transparent)]
    pub struct DebugSendWrapper<T>(send_wrapper::SendWrapper<T>);

    impl<T> DebugSendWrapper<T> {
        pub fn new(value: T) -> Self {
            Self(send_wrapper::SendWrapper::new(value))
        }

        pub fn take(self) -> T {
            self.0.take()
        }
    }

    impl<T> Deref for DebugSendWrapper<T> {
        type Target = T;

        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }
}

#[cfg(not(debug_assertions))]
mod wrapper {
    use std::ops::Deref;

    #[repr(transparent)]
    pub struct DebugSendWrapper<T>(T);

    impl<T> DebugSendWrapper<T> {
        pub fn new(value: T) -> Self {
            Self(value)
        }

        pub fn take(self) -> T {
            self.0
        }
    }

    impl<T> Deref for DebugSendWrapper<T> {
        type Target = T;

        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }
}