1pub mod time;
23/// 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 {
19type This;
20}
2122impl<T> NoTypeInference for T {
23type This = T;
24}
2526/// 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);
2930// 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> {
35fn clone(&self) -> Self {
36*self
37}
38}
3940unsafe impl<T> Send for SyncSendPointer<T> {}
41unsafe impl<T> Sync for SyncSendPointer<T> {}
4243impl<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.
48pub unsafe fn new(ptr: *mut T) -> Self {
49Self(ptr)
50 }
5152pub fn ptr(&self) -> *mut T {
53self.0
54}
55}
5657impl<T> std::fmt::Debug for SyncSendPointer<T> {
58fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59write!(f, "{:?}", self.ptr())
60 }
61}
6263impl<T> PartialEq for SyncSendPointer<T> {
64fn eq(&self, other: &Self) -> bool {
65 std::ptr::eq(self.ptr(), other.ptr())
66 }
67}
6869impl<T> Eq for SyncSendPointer<T> {}
7071/// 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);
7475// 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> {
80fn clone(&self) -> Self {
81*self
82}
83}
8485unsafe impl<T> Send for SendPointer<T> {}
8687impl<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.
92pub unsafe fn new(ptr: *mut T) -> Self {
93Self(ptr)
94 }
9596pub fn ptr(&self) -> *mut T {
97self.0
98}
99}
100101impl<T> std::fmt::Debug for SendPointer<T> {
102fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
103write!(f, "{:?}", self.ptr())
104 }
105}
106107impl<T> PartialEq for SendPointer<T> {
108fn eq(&self, other: &Self) -> bool {
109 std::ptr::eq(self.ptr(), other.ptr())
110 }
111}
112113impl<T> Eq for SendPointer<T> {}
114115/// Implements [`Debug`](std::fmt::Debug) using the provided closure.
116pub struct DebugFormatter<F>(pub F)
117where
118F: Fn(&mut std::fmt::Formatter<'_>) -> std::fmt::Result;
119120impl<F> std::fmt::Debug for DebugFormatter<F>
121where
122F: Fn(&mut std::fmt::Formatter<'_>) -> std::fmt::Result,
123{
124fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125self.0(f)
126 }
127}