cuprate_blockchain/unsafe_sendable.rs
1//! Wrapper type for partially-`unsafe` usage of `T: !Send`.
2
3//---------------------------------------------------------------------------------------------------- Import
4use std::{
5 borrow::Borrow,
6 ops::{Deref, DerefMut},
7};
8
9use bytemuck::TransparentWrapper;
10
11//---------------------------------------------------------------------------------------------------- Aliases
12#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, TransparentWrapper)]
13#[repr(transparent)]
14/// A wrapper type that `unsafe`ly implements `Send` for any `T`.
15///
16/// This is a marker/wrapper type that allows wrapping
17/// any type `T` such that it implements `Send`.
18///
19/// This is to be used when `T` is `Send`, but only in certain
20/// situations not provable to the compiler, or is otherwise a
21/// a pain to prove and/or less efficient.
22///
23/// It is up to the users of this type to ensure their
24/// usage of `UnsafeSendable` are actually safe.
25///
26/// Notably, `heed`'s table type uses this inside `service`.
27pub(crate) struct UnsafeSendable<T>(T);
28
29#[expect(clippy::non_send_fields_in_send_ty)]
30// SAFETY: Users ensure that their usage of this type is safe.
31unsafe impl<T> Send for UnsafeSendable<T> {}
32
33impl<T> UnsafeSendable<T> {
34 /// Create a new [`UnsafeSendable`].
35 ///
36 /// # Safety
37 /// By constructing this type, you must ensure the usage
38 /// of the resulting `Self` is follows all the [`Send`] rules.
39 pub(crate) const unsafe fn new(t: T) -> Self {
40 Self(t)
41 }
42
43 /// Extract the inner `T`.
44 #[expect(dead_code)]
45 pub(crate) fn into_inner(self) -> T {
46 self.0
47 }
48}
49
50impl<T> Borrow<T> for UnsafeSendable<T> {
51 fn borrow(&self) -> &T {
52 &self.0
53 }
54}
55
56impl<T> AsRef<T> for UnsafeSendable<T> {
57 fn as_ref(&self) -> &T {
58 &self.0
59 }
60}
61
62impl<T> AsMut<T> for UnsafeSendable<T> {
63 fn as_mut(&mut self) -> &mut T {
64 &mut self.0
65 }
66}
67
68impl<T> Deref for UnsafeSendable<T> {
69 type Target = T;
70 fn deref(&self) -> &Self::Target {
71 &self.0
72 }
73}
74
75impl<T> DerefMut for UnsafeSendable<T> {
76 fn deref_mut(&mut self) -> &mut Self::Target {
77 &mut self.0
78 }
79}
80
81//---------------------------------------------------------------------------------------------------- Tests
82#[cfg(test)]
83mod test {
84 // use super::*;
85}