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.
5#![no_std]
6use core::ops::{Deref, DerefMut};
7
8/// Wraps a type to make it implement `Send`.
9#[repr(transparent)]
10pub struct Send<T>(T);
11
12impl<T> Send<T> {
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> core::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    fn deref_mut(&mut self) -> &mut Self::Target {
39        &mut self.0
40    }
41}
42
43/// Wraps a type to make it implement `Sync`.
44#[repr(transparent)]
45pub struct Sync<T>(T);
46
47impl<T> Sync<T> {
48    /// # Safety
49    ///
50    /// This is not a magic way to make `t` `Sync`. It is a way to tell the compiler `t` is `Sync`
51    /// and you should only call this method if you are sure this is not a lie.
52    pub unsafe fn new(t: T) -> Self {
53        Sync(t)
54    }
55
56    /// Destroy wrapper and get original type.
57    pub fn unwrap(self) -> T {
58        self.0
59    }
60}
61
62unsafe impl<T> core::marker::Sync for Sync<T> {}
63
64impl<T> Deref for Sync<T> {
65    type Target = T;
66
67    fn deref(&self) -> &Self::Target {
68        &self.0
69    }
70}
71
72impl<T> DerefMut for Sync<T> {
73    fn deref_mut(&mut self) -> &mut Self::Target {
74        &mut self.0
75    }
76}
77
78/// Wraps a type to make it implement `Send` and `Sync`.
79#[repr(transparent)]
80pub struct SendSync<T>(T);
81
82impl<T> SendSync<T> {
83    /// # Safety
84    ///
85    /// This is not a magic way to make `t` `Send` and `Sync`. It is a way to tell the compiler `t`
86    /// is `Send` and `Sync` and you should only call this method if you are sure this is not a lie.
87    pub unsafe fn new(t: T) -> Self {
88        SendSync(t)
89    }
90
91    /// Destroy wrapper and get original type.
92    pub fn unwrap(self) -> T {
93        self.0
94    }
95}
96
97unsafe impl<T> core::marker::Send for SendSync<T> {}
98unsafe impl<T> core::marker::Sync for SendSync<T> {}
99
100impl<T> Deref for SendSync<T> {
101    type Target = T;
102
103    fn deref(&self) -> &Self::Target {
104        &self.0
105    }
106}
107
108impl<T> DerefMut for SendSync<T> {
109    fn deref_mut(&mut self) -> &mut Self::Target {
110        &mut self.0
111    }
112}