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