force_send_sync/
lib.rs

1//! Please do not use this crate. The Rust compiler tries to protect you for a reason. Do under no
2//! circumstances use this to silence some compiler error you do not understand. Only use this if
3//! you do understand why your type is `Send` and or `Sync`, and also understand why the compiler
4//! disagrees with you.
5use std::ops::{Deref, DerefMut};
6
7/// Wraps a type to make it implement `Send`.
8#[repr(transparent)]
9pub struct Send<T>(T);
10
11impl<T> Send<T> {
12
13    /// # Safety
14    ///
15    /// This is not a magic way to make `t` `Send`. It is a way to tell the compiler `t` is `Send`
16    /// and you should only call this method if you are sure this is not a lie.
17    pub unsafe fn new(t: T) -> Self {
18        Send(t)
19    }
20
21    /// Destroy wrapper and get original type.
22    pub fn unwrap(self) -> T {
23        self.0
24    }
25}
26
27unsafe impl<T> std::marker::Send for Send<T> {}
28
29impl<T> Deref for Send<T> {
30    type Target = T;
31
32    fn deref(&self) -> &Self::Target {
33        &self.0
34    }
35}
36
37impl<T> DerefMut for Send<T> {
38
39    fn deref_mut(&mut self) -> &mut Self::Target {
40        &mut self.0
41    }
42}
43
44/// Wraps a type to make it implement `Sync`.
45#[repr(transparent)]
46pub struct Sync<T>(T);
47
48impl<T> Sync<T> {
49
50    /// # Safety
51    ///
52    /// This is not a magic way to make `t` `Sync`. It is a way to tell the compiler `t` is `Sync`
53    /// and you should only call this method if you are sure this is not a lie.
54    pub unsafe fn new(t: T) -> Self {
55        Sync(t)
56    }
57
58    /// Destroy wrapper and get original type.
59    pub fn unwrap(self) -> T {
60        self.0
61    }
62}
63
64unsafe impl<T> std::marker::Sync for Sync<T> {}
65
66impl<T> Deref for Sync<T> {
67    type Target = T;
68
69    fn deref(&self) -> &Self::Target {
70        &self.0
71    }
72}
73
74impl<T> DerefMut for Sync<T> {
75
76    fn deref_mut(&mut self) -> &mut Self::Target {
77        &mut self.0
78    }
79}
80
81/// Wraps a type to make it implement `Send` and `Sync`.
82#[repr(transparent)]
83pub struct SendSync<T>(T);
84
85impl<T> SendSync<T> {
86
87    /// # Safety
88    ///
89    /// This is not a magic way to make `t` `Send` and `Sync`. It is a way to tell the compiler `t`
90    /// is `Send` and `Sync` and you should only call this method if you are sure this is not a lie.
91    pub unsafe fn new(t: T) -> Self {
92        SendSync(t)
93    }
94
95    /// Destroy wrapper and get original type.
96    pub fn unwrap(self) -> T {
97        self.0
98    }
99}
100
101unsafe impl<T> std::marker::Send for SendSync<T> {}
102unsafe impl<T> std::marker::Sync for SendSync<T> {}
103
104impl<T> Deref for SendSync<T> {
105    type Target = T;
106
107    fn deref(&self) -> &Self::Target {
108        &self.0
109    }
110}
111
112impl<T> DerefMut for SendSync<T> {
113
114    fn deref_mut(&mut self) -> &mut Self::Target {
115        &mut self.0
116    }
117}