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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use core::alloc::Layout;
use core::mem::{self, ManuallyDrop};

#[cfg(feature = "maybe_uninit")]
use core::mem::MaybeUninit;

macro_rules! assert_layout_eq {
    ($t:ty, $u:ty) => {
        {
            let layout_t = Layout::new::<$t>();
            let layout_u = Layout::new::<$u>();
            assert_eq!(layout_t, layout_u);
        }
    }
}

macro_rules! assert_ptr_layout_eq {
    ($t:ty, $u:ty) => {
        {
            let layout_ptr_t = Layout::new::<*const $t>();
            let layout_ptr_u = Layout::new::<*const $u>();
            assert_eq!(layout_ptr_t, layout_ptr_u);
        }
    }
}

/// Unsafe, unchecked, coercion between types of the same size and alignment.
pub unsafe trait Coerce<T: ?Sized> {
    /// Performs the coercion on an owned value.
    ///
    /// # Examples
    ///
    /// ```
    /// # use core::num::NonZeroU64;
    /// use coercion::Coerce;
    ///
    /// // Safe because 1 is non-zero.
    /// let nonzero: NonZeroU64 = unsafe { 1u64.coerce() };
    /// assert_eq!(nonzero.get(), 1);
    /// ```
    unsafe fn coerce(self) -> T
        where Self: Sized, T: Sized
    {
        assert_layout_eq!(Self, T);
        assert_ptr_layout_eq!(Self, T);

        let this = ManuallyDrop::new(self);
        let r = mem::transmute_copy(&this);
        r
    }

    /// Perform the conversion on a `const` pointer.
    fn coerce_ptr(this: *const Self) -> *const T {
        assert_ptr_layout_eq!(Self, T);

        let r = unsafe { mem::transmute_copy(&this) };
        r
    }

    /// Perform the conversion on a `mut` pointer.
    fn coerce_mut_ptr(this: *mut Self) -> *mut T {
        assert_ptr_layout_eq!(Self, T);

        Self::coerce_ptr(this) as *mut T
    }
}

#[repr(C)]
struct FatPtr {
    ptr: usize,
    len: usize,
}

unsafe impl<T, U> Coerce<[U]> for [T]
where T: Coerce<U>,
{
    fn coerce_ptr(this: *const Self) -> *const [U] {
        assert_layout_eq!(T,U);

        unsafe {
            let repr: FatPtr = mem::transmute(this);
            mem::transmute(repr)
        }
    }
}


unsafe impl<T: ?Sized, U: ?Sized> Coerce<Box<U>> for Box<T>
where T: Coerce<U>
{
    unsafe fn coerce(self) -> Box<U> {
        assert_ptr_layout_eq!(T, U);
        assert_layout_eq!(Box<T>,Box<U>);

        let t_ptr: *mut T = Box::into_raw(self);
        let u_ptr: *mut U = T::coerce_mut_ptr(t_ptr);

        Box::from_raw(u_ptr)
    }

    fn coerce_ptr(this: *const Self) -> *const Box<U> {
        this as *const Box<U>
    }
}

unsafe impl Coerce<str> for [u8] {
    #[inline(always)]
    fn coerce_ptr(this: *const Self) -> *const str {
        unsafe {
            mem::transmute(this)
        }
    }
}

unsafe impl Coerce<[u8]> for str {
    #[inline(always)]
    fn coerce_ptr(this: *const Self) -> *const [u8] {
        unsafe {
            mem::transmute(this)
        }
    }
}

#[cfg(feature = "maybe_uninit")]
unsafe impl<T> Coerce<T> for MaybeUninit<T> {
    fn coerce_ptr(this: *const Self) -> *const T {
        this as *const T
    }
}

#[cfg(feature = "maybe_uninit")]
unsafe impl<T> Coerce<MaybeUninit<T>> for T {
    fn coerce_ptr(this: *const Self) -> *const MaybeUninit<T> {
        this as *const _
    }
}

unsafe impl<'a, T: ?Sized, U: ?Sized> Coerce<&'a T> for &'a U
where T: Coerce<U>,
{
    fn coerce_ptr(this: *const Self) -> *const &'a T {
        this as *const &'a T
    }
}

unsafe impl<'a, T: ?Sized, U: ?Sized> Coerce<&'a mut T> for &'a mut U
where T: Coerce<U>,
{
    fn coerce_ptr(this: *const Self) -> *const &'a mut T {
        this as *const &'a mut T
    }
}

unsafe impl<'a, T: ?Sized, U: ?Sized> Coerce<*const T> for *const U
where T: Coerce<U>,
{
    fn coerce_ptr(this: *const Self) -> *const *const T {
        this as *const *const T
    }
}

unsafe impl<'a, T: ?Sized, U: ?Sized> Coerce<*mut T> for *mut U
where T: Coerce<U>,
{
    fn coerce_ptr(this: *const Self) -> *const *mut T {
        this as *const *mut T
    }
}

unsafe impl<T: ?Sized, U: ?Sized> Coerce<ManuallyDrop<U>> for ManuallyDrop<T>
where T: Coerce<U>,
{}

mod impls {
    use crate::unsafe_impl_coerce;

    use core::num::*;
    use core::sync::atomic::*;

    // primitive impls
    unsafe_impl_coerce! {
        { u8  | i8  | NonZeroU8  | NonZeroI8  | AtomicU8  | AtomicI8 | bool | AtomicBool };
        { u16 | i16 | NonZeroU16 | NonZeroI16 | AtomicU16 | AtomicI16 };
        { u32 | i32 | NonZeroU32 | NonZeroI32 | AtomicU32 | AtomicI32 };
        { u64 | i64 | NonZeroU64 | NonZeroI64 | AtomicU64 | AtomicI64 };
        { u128 | i128 | NonZeroU128 | NonZeroI128 };
        { usize | isize | NonZeroUsize | NonZeroIsize | AtomicUsize | AtomicIsize };
    }
}

#[cfg(test)]
mod tests {
    use crate::Coerce;

    use std::num::NonZeroU64;

    #[test]
    fn box_coercion() {
        let boxed_u64: Box<u64> = Box::new(1u64);

        let boxed_nonzero_u64: Box<NonZeroU64> = unsafe { boxed_u64.coerce() };

        assert_eq!(*boxed_nonzero_u64, NonZeroU64::new(1).unwrap());
    }

    #[test]
    fn boxed_slice_coercion() {
        let boxed_u64 = vec![1u64; 100].into_boxed_slice();

        let boxed_nonzero: Box<[NonZeroU64]> = unsafe { boxed_u64.coerce() };

        assert_eq!(boxed_nonzero, vec![NonZeroU64::new(1).unwrap(); 100].into_boxed_slice());
    }

    #[cfg(feature = "maybe_uninit")]
    #[test]
    fn maybe_uninit() {
        use core::mem::MaybeUninit;

        let mut uninit: Box<MaybeUninit<NonZeroU64>> = Box::new(MaybeUninit::uninit());

        uninit.write(NonZeroU64::new(123456).unwrap());

        unsafe {
            let init: Box<NonZeroU64> = uninit.coerce();

            assert_eq!(init.get(), 123456);
        }
    }

    #[cfg(feature = "maybe_uninit")]
    #[test]
    fn maybe_uninit_slice() {
        use core::mem::MaybeUninit;

        let mut uninit: Box<[MaybeUninit<bool>]> = vec![MaybeUninit::<bool>::uninit(); 100].into_boxed_slice();

        for b in &mut uninit[..] {
            b.write(true);
        }

        unsafe {
            let init: Box<[bool]> = uninit.coerce();

            assert_eq!(&init[..], &vec![true; 100][..]);
        }
    }
}