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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Traits for newtype wrappers.

#[cfg(feature = "alloc")]
use alloc::{
    boxed::Box,
    rc::Rc,
    sync::Arc,
    vec::Vec,
};

use std_::mem;

use crate::utils::transmute_ignore_size;

/// Trait for `#[repr(transparent)]` newtypes,
/// which are safe to transmute to/from their contents.
///
/// For additional conversion methods, you can use the [`TransparentNewtypeExt`] extension trait.
///
/// # Safety
///
/// Implementors must only implement this trait for `#[repr(transparent)]` wrappers,
/// with the same alignment as its only non-zero-sized field,
/// and the type of that field must be used as the [`TransparentNewtype::Inner`]
/// associated type.
///
/// This trait can be implemented with any of these macros:
/// - The [`TransparentNewtype`] derive macro (requires the "derive" feature).
/// - The [`impl_transparent_newtype`] macro
/// - The [`delegate_transparent_newtype_impl`] macro.
///
/// # Example
///
/// A totally ordered 32 bit float.
///
/// ```
/// use core_extensions::{TransparentNewtype, TransparentNewtypeExt};
/// # use std::cmp::{Eq,Ord,PartialOrd,PartialEq,Ordering};
///
/// pub struct TotalF32(pub f32);
///
/// // Eq, Ord, PartialEq, PartialOrd impls not shown
/// # impl Eq for TotalF32{}
/// #
/// # impl Ord for TotalF32{
/// #     fn cmp(&self, other: &Self) -> Ordering {
/// #         self.0.partial_cmp(&other.0).unwrap_or_else(||{
/// #             match (self.0.is_nan(),other.0.is_nan()) {
/// #                 (false,_    )=>Ordering::Less,
/// #                 (true ,false)=>Ordering::Greater,
/// #                 (true ,true )=>Ordering::Equal,
/// #             }
/// #         })
/// #     }
/// # }
/// # impl PartialOrd for TotalF32{
/// #     fn partial_cmp(&self, other: &Self) -> Option<Ordering>{
/// #         Some(self.cmp(other))
/// #     }
/// # }
/// # impl PartialEq for TotalF32 {
/// #     fn eq(&self, other: &Self) -> bool {
/// #         self.cmp(other)==Ordering::Equal
/// #     }
/// # }
///
/// unsafe impl TransparentNewtype for TotalF32{
///     type Inner = f32;
///     core_extensions::impl_transparent_newtype!{Self}
/// }
///
/// let mut list = vec![1.0, 0.0, 2.0];
///
/// <[TotalF32]>::from_inner_mut(&mut list).sort();
///
/// assert_eq!(list, vec![0.0, 1.0, 2.0]);
///
/// ```
///
/// [`TransparentNewtype::Inner`]: #associatedtype.Inner
/// [`TransparentNewtypeExt`]: ./trait.TransparentNewtypeExt.html
/// [`TransparentNewtype`]: ../derive.TransparentNewtype.html
/// [`impl_transparent_newtype`]: ../macro.impl_transparent_newtype.html
/// [`delegate_transparent_newtype_impl`]: ../macro.delegate_transparent_newtype_impl.html
///
pub unsafe trait TransparentNewtype {
    /// The wrapped type
    type Inner: ?Sized;

    #[doc(hidden)]
    #[allow(non_upper_case_globals)]
    const __DUMMY_bCj7dq3Pud: () = ();

    /// Converts `*const Self::Inner` to `*const Self`.
    fn from_inner_raw(from: *const Self::Inner) -> *const Self;
    
    /// Converts `*mut Self::Inner` to `*mut Self`.
    fn from_inner_raw_mut(v: *mut Self::Inner) -> *mut Self;
    
    /// Converts `*const Self` to `*const Self::Inner`.
    fn as_inner_raw(from: *const Self) -> *const Self::Inner;
    
    /// Converts `*mut Self` to `*mut Self::Inner`.
    fn as_inner_raw_mut(this: *mut Self) -> *mut Self::Inner;
}

/// Extension trait for [`TransparentNewtype`]s
/// 
/// [`TransparentNewtype`]: ./trait.TransparentNewtype.html
/// 
/// 
pub trait TransparentNewtypeExt: TransparentNewtype {
    /// Converts `Self::Inner` to `Self`.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// use core_extensions::TransparentNewtypeExt;
    /// 
    /// use std::num::Wrapping;
    /// use std::mem::ManuallyDrop;
    /// 
    /// assert_eq!(Wrapping::from_inner(3), Wrapping(3));
    /// assert_eq!(ManuallyDrop::from_inner(5), ManuallyDrop::new(5));
    /// 
    /// ```
    #[inline(always)]
    fn from_inner(v: Self::Inner) -> Self
    where
        Self: Sized,
        Self::Inner: Sized,
    {
        check_same_size_alignment::<Self::Inner, Self>();
        unsafe { transmute_ignore_size::<Self::Inner, Self>(v) }
    }

    /// Converts `&Self::Inner` to a `&Self`.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// use core_extensions::TransparentNewtypeExt;
    /// 
    /// use std::num::Wrapping;
    /// use std::mem::ManuallyDrop;
    /// 
    /// assert_eq!(Wrapping::from_inner_ref(&3), &Wrapping(3));
    /// assert_eq!(ManuallyDrop::from_inner_ref(&5), &ManuallyDrop::new(5));
    /// 
    /// ```
    #[inline(always)]
    fn from_inner_ref(v: &Self::Inner) -> &Self {
        unsafe { &*Self::from_inner_raw(v) }
    }
    
    /// Converts `&mut Self::Inner` to a `&mut Self`.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// use core_extensions::TransparentNewtypeExt;
    /// 
    /// use std::num::Wrapping;
    /// use std::mem::ManuallyDrop;
    /// 
    /// assert_eq!(Wrapping::from_inner_ref(&mut 3), &mut Wrapping(3));
    /// assert_eq!(ManuallyDrop::from_inner_ref(&mut 5), &mut ManuallyDrop::new(5));
    /// 
    /// ```
    #[inline(always)]
    fn from_inner_mut(v: &mut Self::Inner) -> &mut Self {
        unsafe { &mut *Self::from_inner_raw_mut(v) }
    }

    /// Converts `Box<Self::Inner>` to a `Box<Self>` without allocating.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// use core_extensions::TransparentNewtypeExt;
    /// 
    /// use std::num::Wrapping;
    /// use std::mem::ManuallyDrop as MD;
    /// 
    /// assert_eq!(Wrapping::from_inner_box(Box::new(3)), Box::new(Wrapping(3)));
    /// assert_eq!(MD::from_inner_box(Box::new(5)), Box::new(MD::new(5)));
    /// 
    /// ```
    #[cfg(feature = "alloc")]
    #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
    #[inline(always)]
    fn from_inner_box(v: Box<Self::Inner>) -> Box<Self> {
        unsafe { Box::from_raw(Self::from_inner_raw_mut(Box::into_raw(v))) }
    }

    /// Converts `Arc<Self::Inner>` to a `Arc<Self>` without allocating.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// use core_extensions::TransparentNewtypeExt;
    /// 
    /// use std::num::Wrapping;
    /// use std::mem::ManuallyDrop as MD;
    /// use std::sync::Arc;
    /// 
    /// assert_eq!(Wrapping::from_inner_arc(Arc::new(3)), Arc::new(Wrapping(3)));
    /// assert_eq!(MD::from_inner_arc(Arc::new(5)), Arc::new(MD::new(5)));
    /// 
    /// ```
    #[cfg(feature = "alloc")]
    #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
    #[inline(always)]
    fn from_inner_arc(v: Arc<Self::Inner>) -> Arc<Self> {
        unsafe { Arc::from_raw(Self::from_inner_raw(Arc::into_raw(v))) }
    }
    
    /// Converts `Rc<Self::Inner>` to a `Rc<Self>` without allocating.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// use core_extensions::TransparentNewtypeExt;
    /// 
    /// use std::num::Wrapping;
    /// use std::mem::ManuallyDrop as MD;
    /// use std::rc::Rc;
    /// 
    /// assert_eq!(Wrapping::from_inner_rc(Rc::new(3)), Rc::new(Wrapping(3)));
    /// assert_eq!(MD::from_inner_rc(Rc::new(5)), Rc::new(MD::new(5)));
    /// 
    /// ```
    #[cfg(feature = "alloc")]
    #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
    #[inline(always)]
    fn from_inner_rc(v: Rc<Self::Inner>) -> Rc<Self> {
        unsafe { Rc::from_raw(Self::from_inner_raw(Rc::into_raw(v))) }
    }

    /// Converts `self` to a `Self::Inner`.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// use core_extensions::TransparentNewtypeExt;
    /// 
    /// use std::num::Wrapping;
    /// use std::mem::ManuallyDrop;
    /// 
    /// assert_eq!(Wrapping(3).into_inner(), 3);
    /// assert_eq!(ManuallyDrop::new(5).into_inner(), 5);
    /// 
    /// ```
    #[inline(always)]
    fn into_inner(self) -> Self::Inner
    where
        Self: Sized,
        Self::Inner: Sized,
    {
        check_same_size_alignment::<Self::Inner, Self>();
        unsafe { transmute_ignore_size::<Self, Self::Inner>(self) }
    }

    /// Converts `self` to a `&Self::Inner`.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// use core_extensions::TransparentNewtypeExt;
    /// 
    /// use std::num::Wrapping;
    /// use std::mem::ManuallyDrop;
    /// 
    /// assert_eq!(Wrapping(3).as_inner(), &3);
    /// assert_eq!(ManuallyDrop::new(5).as_inner(), &5);
    /// 
    /// ```
    #[inline(always)]
    fn as_inner(&self) -> &Self::Inner {
        unsafe { &*Self::as_inner_raw(self) }
    }

    /// Converts `self` to a `&mut Self::Inner`.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// use core_extensions::TransparentNewtypeExt;
    /// 
    /// use std::num::Wrapping;
    /// use std::mem::ManuallyDrop;
    /// 
    /// assert_eq!(Wrapping(3).as_inner_mut(), &mut 3);
    /// assert_eq!(ManuallyDrop::new(5).as_inner_mut(), &mut 5);
    /// 
    /// ```
    #[inline(always)]
    fn as_inner_mut(&mut self) -> &mut Self::Inner {
        unsafe { &mut *Self::as_inner_raw_mut(self) }
    }

    /// Converts `self` to a `Box<Self::Inner>` without allocating.
    /// 
    /// # Example
    /// 
    /// ```rust
    /// use core_extensions::TransparentNewtypeExt;
    /// 
    /// use std::num::Wrapping;
    /// use std::mem::ManuallyDrop;
    /// 
    /// assert_eq!(Box::new(Wrapping(3)).into_inner_box(), Box::new(3));
    /// assert_eq!(Box::new(ManuallyDrop::new(5)).into_inner_box(), Box::new(5));
    /// 
    /// ```
    #[cfg(feature = "alloc")]
    #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
    #[inline(always)]
    fn into_inner_box(self: Box<Self>) -> Box<Self::Inner> {
        unsafe { Box::from_raw(Self::as_inner_raw_mut(Box::into_raw(self))) }
    }

    if_rust_1_46!{
        /// Converts `self` to a `Arc<Self::Inner>` without allocating.
        /// 
        /// # Self parameter
        /// 
        /// Enabling the "rust_1_46" feature changes this method 
        /// from taking a `this` parameter to taking a `self` parameter,
        /// which allows calling it with `.into_inner_arc()`
        /// 
        /// # Example
        /// 
        /// ```rust
        /// use core_extensions::TransparentNewtypeExt;
        /// 
        /// use std::num::Wrapping;
        /// use std::mem::ManuallyDrop;
        /// use std::sync::Arc;
        /// 
        /// assert_eq!(
        ///     Wrapping::into_inner_arc(Arc::new(Wrapping(3))),
        ///     Arc::new(3)
        /// );
        /// assert_eq!(
        ///     ManuallyDrop::into_inner_arc(Arc::new(ManuallyDrop::new(5))),
        ///     Arc::new(5)
        /// );
        /// 
        /// // Calling this as a method requires the "rust_1_46" feature
        #[cfg_attr(not(feature = "rust_1_46"), doc = "# /*")]
        /// assert_eq!(Arc::new(Wrapping(3)).into_inner_arc(), Arc::new(3));
        /// assert_eq!(Arc::new(ManuallyDrop::new(5)).into_inner_arc(), Arc::new(5));
        #[cfg_attr(not(feature = "rust_1_46"), doc = "# */")]
        /// 
        /// ```
        #[cfg(feature = "alloc")]
        #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
        #[inline(always)]
        =>
        (
            fn into_inner_arc(this: Arc<Self>) -> Arc<Self::Inner> {
                unsafe { Arc::from_raw(Self::as_inner_raw(Arc::into_raw(this))) }
            }
        )
        (
            fn into_inner_arc(self: Arc<Self>) -> Arc<Self::Inner> {
                unsafe { Arc::from_raw(Self::as_inner_raw(Arc::into_raw(self))) }
            }
        )
    }

    if_rust_1_46!{
        /// Converts `self` to a `Rc<Self::Inner>` without allocating.
        /// 
        /// # Self parameter
        /// 
        /// Enabling the "rust_1_46" feature changes this method 
        /// from taking a `this` parameter to taking a `self` parameter,
        /// which allows calling it with `.into_inner_rc()`
        /// 
        /// # Example
        /// 
        /// ```rust
        /// use core_extensions::TransparentNewtypeExt;
        /// 
        /// use std::num::Wrapping;
        /// use std::mem::ManuallyDrop;
        /// use std::rc::Rc;
        /// 
        /// assert_eq!(
        ///     Wrapping::into_inner_rc(Rc::new(Wrapping(3))),
        ///     Rc::new(3)
        /// );
        /// assert_eq!(
        ///     ManuallyDrop::into_inner_rc(Rc::new(ManuallyDrop::new(5))),
        ///     Rc::new(5)
        /// );
        /// 
        /// // Calling this as a method requires the "rust_1_46" feature
        #[cfg_attr(not(feature = "rust_1_46"), doc = "# /*")]
        /// assert_eq!(Rc::new(Wrapping(3)).into_inner_rc(), Rc::new(3));
        /// assert_eq!(Rc::new(ManuallyDrop::new(5)).into_inner_rc(), Rc::new(5));
        #[cfg_attr(not(feature = "rust_1_46"), doc = "# */")]
        /// 
        /// ```
        #[cfg(feature = "alloc")]
        #[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
        #[inline(always)]
        =>
        (
            fn into_inner_rc(this: Rc<Self>) -> Rc<Self::Inner> {
                unsafe { Rc::from_raw(Self::as_inner_raw(Rc::into_raw(this))) }
            }
        )
        (
            fn into_inner_rc(self: Rc<Self>) -> Rc<Self::Inner> {
                unsafe { Rc::from_raw(Self::as_inner_raw(Rc::into_raw(self))) }
            }
        )
    }
}

impl<T> TransparentNewtypeExt for T
where
    T: TransparentNewtype + ?Sized
{}

///////////////////////////////////////////////////////////////////////////////

unsafe impl<T> TransparentNewtype for [T]
where
    T: TransparentNewtype,
    T::Inner: Sized,
{
    type Inner = [T::Inner];
    crate::impl_transparent_newtype!{Self}
}

///////////////////////////////////////////////////////////////////////////////

unsafe impl<T> TransparentNewtype for core::num::Wrapping<T> {
    type Inner = T;

    crate::impl_transparent_newtype!{Self}
}

///////////////////////////////////////////////////////////////////////////////

unsafe impl<T> TransparentNewtype for core::mem::ManuallyDrop<T> {
    type Inner = T;

    crate::impl_transparent_newtype!{Self}
}

///////////////////////////////////////////////////////////////////////////////

/// Converts a `Vec` of `T` into a `Vec` of the type that `T` wraps.
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
pub fn into_inner_vec<T>(this: Vec<T>) -> Vec<T::Inner>
where
    T: TransparentNewtype,
    T::Inner: Sized,
{
    unsafe{ crate::utils::transmute_vec(this) }
}

/// Converts a `Vec` of some type into a `Vec` of a wrapper around that type.
#[cfg(feature = "alloc")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "alloc")))]
pub fn from_inner_vec<T>(this: Vec<T::Inner>) -> Vec<T>
where
    T: TransparentNewtype,
    T::Inner: Sized,
{
    unsafe{ crate::utils::transmute_vec(this) }
}

///////////////////////////////////////////////////////////////////////////////

#[inline(always)]
fn check_same_size_alignment<T, U>() {
    assert_eq!(mem::size_of::<T>(), mem::size_of::<U>());
    assert_eq!(mem::align_of::<T>(), mem::align_of::<U>());
}