shadow_shim_helper_rs/util/
mod.rs

1pub mod time;
2
3/// A trait to prevent type inference during function calls.
4///
5/// Useful when you have a type that wraps a pointer (like `ForeignArrayPtr`) and you don't want
6/// Rust to infer the type of pointer during creation.  Instead, the caller must specify the generic
7/// type.
8///
9/// Example:
10///
11/// ```ignore
12/// let x: ForeignArrayPtr<u8>;
13///
14/// // normally the `<u8>` wouldn't be required since Rust would infer it from the type of `x`, but
15/// // for this function using [`NoTypeInference`], the `<u8>` is required and must match
16/// x = ForeignArrayPtr::new::<u8>(...);
17/// ```
18pub trait NoTypeInference {
19    type This;
20}
21
22impl<T> NoTypeInference for T {
23    type This = T;
24}
25
26/// A type that allows us to make a pointer Send + Sync since there is no way
27/// to add these traits to the pointer itself.
28pub struct SyncSendPointer<T>(*mut T);
29
30// We can't automatically `derive` Copy and Clone without unnecessarily
31// requiring T to be Copy and Clone.
32// https://github.com/rust-lang/rust/issues/26925
33impl<T> Copy for SyncSendPointer<T> {}
34impl<T> Clone for SyncSendPointer<T> {
35    fn clone(&self) -> Self {
36        *self
37    }
38}
39
40unsafe impl<T> Send for SyncSendPointer<T> {}
41unsafe impl<T> Sync for SyncSendPointer<T> {}
42
43impl<T> SyncSendPointer<T> {
44    /// # Safety
45    ///
46    /// The object pointed to by `ptr` must actually be `Sync` and `Send` or
47    /// else not subsequently used in contexts where it matters.
48    pub unsafe fn new(ptr: *mut T) -> Self {
49        Self(ptr)
50    }
51
52    pub fn ptr(&self) -> *mut T {
53        self.0
54    }
55}
56
57impl<T> std::fmt::Debug for SyncSendPointer<T> {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        write!(f, "{:?}", self.ptr())
60    }
61}
62
63impl<T> PartialEq for SyncSendPointer<T> {
64    fn eq(&self, other: &Self) -> bool {
65        std::ptr::eq(self.ptr(), other.ptr())
66    }
67}
68
69impl<T> Eq for SyncSendPointer<T> {}
70
71/// A type that allows us to make a pointer Send since there is no way
72/// to add this traits to the pointer itself.
73pub struct SendPointer<T>(*mut T);
74
75// We can't automatically `derive` Copy and Clone without unnecessarily
76// requiring T to be Copy and Clone.
77// https://github.com/rust-lang/rust/issues/26925
78impl<T> Copy for SendPointer<T> {}
79impl<T> Clone for SendPointer<T> {
80    fn clone(&self) -> Self {
81        *self
82    }
83}
84
85unsafe impl<T> Send for SendPointer<T> {}
86
87impl<T> SendPointer<T> {
88    /// # Safety
89    ///
90    /// The object pointed to by `ptr` must actually be `Send` or else not
91    /// subsequently used in contexts where it matters.
92    pub unsafe fn new(ptr: *mut T) -> Self {
93        Self(ptr)
94    }
95
96    pub fn ptr(&self) -> *mut T {
97        self.0
98    }
99}
100
101impl<T> std::fmt::Debug for SendPointer<T> {
102    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
103        write!(f, "{:?}", self.ptr())
104    }
105}
106
107impl<T> PartialEq for SendPointer<T> {
108    fn eq(&self, other: &Self) -> bool {
109        std::ptr::eq(self.ptr(), other.ptr())
110    }
111}
112
113impl<T> Eq for SendPointer<T> {}
114
115/// Implements [`Debug`](std::fmt::Debug) using the provided closure.
116pub struct DebugFormatter<F>(pub F)
117where
118    F: Fn(&mut std::fmt::Formatter<'_>) -> std::fmt::Result;
119
120impl<F> std::fmt::Debug for DebugFormatter<F>
121where
122    F: Fn(&mut std::fmt::Formatter<'_>) -> std::fmt::Result,
123{
124    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125        self.0(f)
126    }
127}