repr_offset/utils.rs
1//! Miscelaneous functions.
2
3use core::marker::PhantomData;
4
5/// A helper function to force a variable to move (copy if it's a Copy type).
6///
7/// # Example
8///
9/// ```rust
10/// use repr_offset::utils::moved;
11///
12/// #[repr(C, packed)]
13/// struct Packed{
14/// foo: usize,
15/// bar: u64,
16/// }
17///
18/// let this = Packed{ foo: 21, bar: 34 };
19///
20/// assert_eq!( moved(this.foo), 21 );
21/// assert_eq!( moved(this.bar), 34 );
22///
23/// // The code below causes undefined behavior because:
24/// // -`assert_eq` borrows the operands implicitly.
25/// // - Fields of `#[repr(C, packed)]` structs create unaligned references when borrowed.
26/// // - Unaligned references are undefined behavior.
27/// //
28/// // unsafe{
29/// // assert_eq!( this.foo, 21 );
30/// // assert_eq!( this.bar, 34 );
31/// // }
32///
33/// ```
34#[inline(always)]
35pub const fn moved<T>(val: T) -> T {
36 val
37}
38
39/// A const-equivalent of `core::cmp::min::<usize>`
40pub(crate) const fn min_usize(l: usize, r: usize) -> usize {
41 let mask_r = ((l < r) as usize).wrapping_sub(1);
42 (r & mask_r) | (l & !mask_r)
43}
44
45/// Helper type with associated constants for `core::mem` functions (and a few more).
46pub(crate) struct Mem<T>(T);
47
48impl<T> Mem<T> {
49 /// Equivalent to `core::mem::size_of`.
50 pub const SIZE: usize = core::mem::size_of::<T>();
51
52 /// Equivalent to `core::mem::align_of`.
53 pub const ALIGN: usize = core::mem::align_of::<T>();
54}
55
56/// Helper type to construct certain PhantomData in const fns.
57pub struct MakePhantomData<T>(T);
58
59impl<T> MakePhantomData<T> {
60 /// Constructs a `PhantomData<fn()->T>`,
61 /// this is a workaround for constructing them inside `const fn`.
62 pub const FN_RET: PhantomData<fn() -> T> = PhantomData;
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn testing_min_usize() {
71 let max = usize::max_value();
72 for l in (0usize..10).chain(max - 10..=max) {
73 for r in (0usize..10).chain(max - 10..=max) {
74 assert_eq!(core::cmp::min(l, r), min_usize(l, r),);
75 }
76 }
77 }
78}
79
80////////////////////////////////////////////////////////////////////////////////
81
82#[doc(hidden)]
83#[repr(transparent)]
84pub struct AsPhantomDataFn<'s, T> {
85 pub reference: &'s T,
86 pub ty: PhantomData<fn() -> T>,
87}
88
89////////////////////////////////////////////////////////////////////////////////
90
91#[doc(hidden)]
92pub trait AsPhantomData: Sized {
93 #[doc(hidden)]
94 const __REPR_OFFSET_PHANTOMDATA: PhantomData<Self> = PhantomData;
95
96 #[doc(hidden)]
97 const __REPR_OFFSET_PHANTOMDATA_FN: PhantomData<fn() -> Self> = PhantomData;
98}
99
100impl<T> AsPhantomData for T {}
101
102////////////////////////////////////////////////////////////////////////////////
103
104/// Gets the type pointed-to by a pointer.
105pub unsafe trait PointerTarget {
106 /// The pointed-to type.
107 type Target;
108}
109
110unsafe impl<T> PointerTarget for &T {
111 type Target = T;
112}
113
114unsafe impl<T> PointerTarget for &mut T {
115 type Target = T;
116}
117
118unsafe impl<T> PointerTarget for *const T {
119 type Target = T;
120}
121
122unsafe impl<T> PointerTarget for *mut T {
123 type Target = T;
124}