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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
// Copyright 2018 Anthony Ramine
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! `Inert` lets you access non-`Sync` data in `Sync` context, hiding from the
//! user anything that may not be sound to use when the value is shared.

#![cfg_attr(not(feature = "std"), no_std)]

#[doc(inline)]
pub use inert_derive::neutralize;

#[cfg(feature = "std")]
extern crate core;

use core::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell};
use core::cmp::Ordering;
use core::fmt;
use core::marker::PhantomData;
use core::ops::Deref;

#[cfg(feature = "std")]
use std::borrow::Cow;
#[cfg(feature = "std")]
use std::rc::Rc;

/// Marker trait for types that can be safely neutralized.
pub unsafe trait Neutralize: NeutralizeMut {}

/// Marker trait for types that can be safely neutralized mutably.
pub unsafe trait NeutralizeMut: NeutralizeUnsafe {}

/// Unsafely neutralizes a reference, returning a `Sync` view to it.
///
/// For example, `RefCell<T>` implements it with `Output` as `T::Output`,
/// accessing the inner value of the cell directly through `RefCell::as_ptr`.
pub unsafe trait NeutralizeUnsafe {
    /// The type of the `Sync` view.
    type Output: ?Sized + Sync;

    /// Unsafely neutralizes `self`.
    ///
    /// # Safety
    ///
    /// It is undefined behaviour to use `self` as long as any thread is
    /// still doing something with the return value of that method.
    unsafe fn neutralize_unsafe(&self) -> &Self::Output;
}

/// A wrapper for neutralized values.
///
/// If `T` is `Neutralize`, then it can be safely neutralized from an
/// immutable reference. Almost everything implements `Neutralize` but
/// `RefCell<T>` (and similar types).
///
/// If `T` is `NeutralizeMut`, then it can be safely neutralized from a mutable
/// reference. Almost everything implements `NeutralizeMut` but `Rc<RefCell<T>>`
/// and `&'a RefCell<T>` (and similar types).
///
/// If `T` is `NeutralizeUnsafe`, this type derefs to `T::Output`,
/// with no safe way to reach out for the `T` value itself, which is why
/// it is sound for `Inert<T>` to be `Sync`.
#[repr(transparent)]
pub struct Inert<T: ?Sized> {
    value: Neutralized<T>,
}
unsafe impl<T> Sync for Inert<T> where T: ?Sized {}

impl<T> Inert<T>
where
    T: ?Sized,
{
    /// Creates a new `Inert<T>` from a reference.
    #[inline]
    pub fn new(value: &T) -> &Self
    where
        T: Neutralize,
    {
        unsafe { Self::new_unchecked(value) }
    }

    /// Creates a new `Inert<T>` from a mutable reference.
    #[inline]
    pub fn new_mut(value: &mut T) -> &Self
    where
        T: NeutralizeMut,
    {
        unsafe { Self::new_unchecked(value) }
    }

    /// Unsafely creates a new `Inert<T>` from a reference.
    ///
    /// # Safety
    ///
    /// The user must swear on the holy baguette that they won't do anything
    /// with the `&T` as long as any thread is still doing things with the
    /// `&Inert<T>`, either directly or through other neutralized values
    /// reached through the inner `&T::Output` value, lest they provoke
    /// undefined behaviour, or worse, spoil their entire wheat harvest.
    #[inline]
    pub unsafe fn new_unchecked(value: &T) -> &Self {
        &*(value as *const T as *const Self)
    }

    /// Returns a reference to the inner `T`.
    #[inline]
    pub fn get_ref(this: &Self) -> &T
    where
        T: Sync,
    {
        unsafe { Self::get_ref_unchecked(this) }
    }

    /// Unsafely returns a reference to the inner `T`.
    ///
    /// # Safety
    ///
    /// Undefined behaviour is possible if `T` is not `Sync`.
    #[inline]
    pub unsafe fn get_ref_unchecked(this: &Self) -> &T {
        this.value.as_ref()
    }
}

impl<T> Deref for Inert<T>
where
    T: ?Sized + NeutralizeUnsafe,
{
    type Target = T::Output;

    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe { Self::get_ref_unchecked(self).neutralize_unsafe() }
    }
}

/// A neutralized value.
///
/// Used by `#[inert::neutralize(as unsafe Foo)]`, shouldn't be needed directly.
/// Helpful nonetheless to ensure the `T` value cannot be safely accessed.
#[repr(transparent)]
pub struct Neutralized<T: ?Sized> {
    _marker: PhantomData<*mut ()>,
    value: T,
}
unsafe impl<T> Sync for Neutralized<T> where T: ?Sized {}

impl<T> Neutralized<T>
where
    T: ?Sized,
{
    /// Unsafely returns a reference to the inner `T` value.
    #[inline]
    pub unsafe fn as_ref(&self) -> &T {
        &self.value
    }
}

// Obviously everything in this crate is unsafe so it needs serious scrutiny,
// but this part especially is where all the magic happens.
//
// The following implementations are how the two safe methods `Inert::new` and
// `Inert::new_mut` are constrained to actually be sound.

unsafe impl<'a, T> NeutralizeMut for &'a T where T: ?Sized + Neutralize {}
unsafe impl<'a, T> Neutralize for &'a T where T: ?Sized + Neutralize {}

unsafe impl<'a, T> NeutralizeMut for &'a mut T where T: ?Sized + NeutralizeMut {}
unsafe impl<'a, T> Neutralize for &'a mut T where T: ?Sized + Neutralize {}

unsafe impl<T> NeutralizeMut for Cell<T> where T: ?Sized + NeutralizeMut {}

unsafe impl<T> NeutralizeMut for RefCell<T> where T: ?Sized + NeutralizeMut {}

unsafe impl<'a, T> NeutralizeMut for Ref<'a, T> where T: ?Sized + Neutralize {}
unsafe impl<'a, T> Neutralize for Ref<'a, T> where T: ?Sized + Neutralize {}

unsafe impl<'a, T> NeutralizeMut for RefMut<'a, T> where T: ?Sized + NeutralizeMut {}
unsafe impl<'a, T> Neutralize for RefMut<'a, T> where T: ?Sized + Neutralize {}

#[cfg(feature = "std")]
unsafe impl<T> NeutralizeMut for Rc<T> where T: ?Sized + Neutralize {}
#[cfg(feature = "std")]
unsafe impl<T> Neutralize for Rc<T> where T: ?Sized + Neutralize {}

#[cfg(feature = "std")]
unsafe impl<'a, T> NeutralizeMut for Cow<'a, T>
where
    T: 'a + ?Sized + Neutralize + ToOwned,
    T::Owned: NeutralizeMut<Output = T::Output>,
{
}

#[cfg(feature = "std")]
unsafe impl<'a, T> Neutralize for Cow<'a, T>
where
    T: 'a + ?Sized + Neutralize + ToOwned,
    T::Owned: Neutralize<Output = T::Output>,
{
}

// There follow all the `NeutralizeUnsafe` impls for the types that we
// mentioned in the previous important section.

unsafe impl<'a, T> NeutralizeUnsafe for &'a T
where
    T: ?Sized + NeutralizeUnsafe,
{
    type Output = T::Output;

    #[inline]
    unsafe fn neutralize_unsafe(&self) -> &Self::Output {
        T::neutralize_unsafe(self)
    }
}

unsafe impl<'a, T> NeutralizeUnsafe for &'a mut T
where
    T: ?Sized + NeutralizeUnsafe,
{
    type Output = T::Output;

    #[inline]
    unsafe fn neutralize_unsafe(&self) -> &Self::Output {
        T::neutralize_unsafe(self)
    }
}

unsafe impl<T> NeutralizeUnsafe for Cell<T>
where
    T: ?Sized,
{
    type Output = Inert<T>;

    #[inline]
    unsafe fn neutralize_unsafe(&self) -> &Self::Output {
        Inert::new_unchecked(&*self.as_ptr())
    }
}

unsafe impl<T> NeutralizeUnsafe for RefCell<T>
where
    T: ?Sized,
{
    type Output = InertRefCell<T>;

    #[inline]
    unsafe fn neutralize_unsafe(&self) -> &Self::Output {
        &*(self as *const Self as *const Self::Output)
    }
}

/// An inert `RefCell<T>`.
pub struct InertRefCell<T: ?Sized> {
    value: Neutralized<RefCell<T>>,
}

impl<T> InertRefCell<T>
where
    T: ?Sized,
{
    /// Returns an inert reference to the inner `T`.
    ///
    /// # Panics
    ///
    /// This method panics if the `RefCell<T>` is currently borrowed mutably.
    pub fn borrow(&self) -> &Inert<T> {
        // FIXME(nox): This is nuts but life is too short to wait for
        // RefCell::borrow_state to land.
        //
        // https://github.com/rust-lang/rust/pull/59211

        struct RefCellRepr<T: ?Sized> {
            flag: Cell<isize>,
            _value: UnsafeCell<T>,
        }

        #[cold]
        #[inline(never)]
        fn panic() {
            panic!("already mutably borrowed")
        }

        unsafe {
            if (*(self as *const Self as *const RefCellRepr<T>)).flag.get() < 0 {
                panic();
            }
            Inert::new_unchecked(&*self.value.as_ref().as_ptr())
        }
    }
}

unsafe impl<'a, T> NeutralizeUnsafe for Ref<'a, T>
where
    T: ?Sized,
{
    type Output = Inert<T>;

    #[inline]
    unsafe fn neutralize_unsafe(&self) -> &Self::Output {
        Inert::new_unchecked(self)
    }
}

unsafe impl<'a, T> NeutralizeUnsafe for RefMut<'a, T>
where
    T: ?Sized,
{
    type Output = Inert<T>;

    #[inline]
    unsafe fn neutralize_unsafe(&self) -> &Self::Output {
        Inert::new_unchecked(self)
    }
}

#[cfg(feature = "std")]
unsafe impl<T> NeutralizeUnsafe for Rc<T>
where
    T: ?Sized,
{
    type Output = Inert<T>;

    #[inline]
    unsafe fn neutralize_unsafe(&self) -> &Self::Output {
        Inert::new_unchecked(self)
    }
}

#[cfg(feature = "std")]
unsafe impl<'a, T> NeutralizeUnsafe for Cow<'a, T>
where
    T: 'a + ?Sized + NeutralizeUnsafe + ToOwned,
    T::Owned: NeutralizeUnsafe<Output = T::Output>,
{
    type Output = T::Output;

    #[inline]
    unsafe fn neutralize_unsafe(&self) -> &Self::Output {
        match *self {
            Cow::Borrowed(ref this) => T::neutralize_unsafe(this),
            Cow::Owned(ref this) => {
                // We can't just deref the Cow<T> because it calls
                // T::Owned::borrow and T::Owned may not be Sync.
                T::Owned::neutralize_unsafe(this)
            },
        }
    }
}

// Inert<T> implements many traits through T::Output.

impl<T> AsRef<T::Output> for Inert<T>
where
    T: ?Sized + NeutralizeUnsafe,
{
    #[inline]
    fn as_ref(&self) -> &T::Output {
        self
    }
}

impl<T> PartialEq for Inert<T>
where
    T: ?Sized + NeutralizeUnsafe,
    T::Output: PartialEq,
{
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        T::Output::eq(self, other)
    }
}

impl<T> Eq for Inert<T>
where
    T: ?Sized + NeutralizeUnsafe,
    T::Output: Eq,
{
}

impl<T> PartialOrd for Inert<T>
where
    T: ?Sized + NeutralizeUnsafe,
    T::Output: PartialOrd,
{
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        T::Output::partial_cmp(self, other)
    }
}

impl<T> Ord for Inert<T>
where
    T: ?Sized + NeutralizeUnsafe,
    T::Output: Ord,
{
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        T::Output::cmp(self, other)
    }
}

impl<T> fmt::Debug for Inert<T>
where
    T: ?Sized + NeutralizeUnsafe,
    T::Output: fmt::Debug,
{
    #[inline]
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        T::Output::fmt(self, fmt)
    }
}

impl<T> fmt::Display for Inert<T>
where
    T: ?Sized + NeutralizeUnsafe,
    T::Output: fmt::Display,
{
    #[inline]
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        T::Output::fmt(self, fmt)
    }
}

// 🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖
// Below this sandwich are only uninteresting and trivial implementations.
// 🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖🥖

unsafe impl<T> NeutralizeUnsafe for [T] {
    type Output = [Inert<T>];

    #[inline]
    unsafe fn neutralize_unsafe(&self) -> &Self::Output {
        &*(self as *const Self as *const Self::Output)
    }
}

unsafe impl<T> NeutralizeMut for [T] where T: NeutralizeMut {}
unsafe impl<T> Neutralize for [T] where T: Neutralize {}

#[cfg(feature = "std")]
unsafe impl<T> NeutralizeUnsafe for Vec<T> {
    // Not `Vec<Inert<T>>` so that `Cow<[T]>` works.
    type Output = [Inert<T>];

    #[inline]
    unsafe fn neutralize_unsafe(&self) -> &Self::Output {
        <[T]>::neutralize_unsafe(self)
    }
}

#[cfg(feature = "std")]
unsafe impl<T> NeutralizeMut for Vec<T> where T: NeutralizeMut {}
#[cfg(feature = "std")]
unsafe impl<T> Neutralize for Vec<T> where T: Neutralize {}

macro_rules! neutralize_as_deref {
    ($($($id:ident)::* <$($param:tt),*>,)*) => {$(
        unsafe impl<$($param),*> NeutralizeUnsafe for $($id)::* <$($param),*>
        where
            $($param: ?Sized + NeutralizeUnsafe,)*
        {
            type Output = T::Output;

            #[inline]
            unsafe fn neutralize_unsafe(&self) -> &Self::Output {
                T::neutralize_unsafe(self)
            }
        }

        unsafe impl<$($param),*> NeutralizeMut for $($id)::* <$($param),*>
        where
            $($param: ?Sized + NeutralizeMut,)*
        {
        }

        unsafe impl<$($param),*> Neutralize for $($id)::* <$($param),*>
        where
            $($param: ?Sized + Neutralize,)*
        {
        }
    )*};
}

neutralize_as_deref! {
    core::mem::ManuallyDrop<T>,
}

#[cfg(feature = "std")]
neutralize_as_deref! {
    Box<T>,
}

#[cfg(feature = "std")]
macro_rules! neutralize_as_target {
    ($($ty:ty => $output:ty,)*) => {$(
        unsafe impl NeutralizeUnsafe for $ty {
            type Output = $output;

            #[inline]
            unsafe fn neutralize_unsafe(&self) -> &Self::Output {
                <$output>::neutralize_unsafe(self)
            }
        }

        unsafe impl NeutralizeMut for $ty {}
        unsafe impl Neutralize for $ty {}
    )*};
}

#[cfg(feature = "std")]
neutralize_as_target! {
    String => str,
    std::ffi::CString => std::ffi::CStr,
    std::ffi::OsString => std::ffi::OsStr,
    std::path::PathBuf => std::path::Path,
}

macro_rules! neutralize_as_self {
    ($($($id:ident)::* $(<$($param:tt),*>)* $(($($p:ident: ($($bound:tt)*)),*))*,)*) => {$(
        unsafe impl $(<$($param),*>)* NeutralizeUnsafe for $($id)::* $(<$($param),*>)*
        $(where
            $($p: $($bound)*),*)*
        {
            type Output = Self;

            #[inline]
            unsafe fn neutralize_unsafe(&self) -> &Self::Output {
                self
            }
        }

        unsafe impl $(<$($param),*>)* NeutralizeMut for $($id)::* $(<$($param),*>)*
        $(where
            $($p: $($bound)*),*)*
        {
        }

        unsafe impl $(<$($param),*>)* Neutralize for $($id)::* $(<$($param),*>)*
        $(where
            $($p: $($bound)*),*)*
        {
        }
    )*};
}

// Non-generic primitives.
neutralize_as_self! {
    bool,
    char, str,
    f32, f64,
    i8, i16, i32, i64, i128, isize,
    u8, u16, u32, u64, u128, usize,
}

// Sync types from libcore.
neutralize_as_self! {
    core::alloc::Layout,
    core::alloc::LayoutErr,
    core::any::TypeId,
    core::ascii::EscapeDefault,
    core::cell::BorrowError,
    core::cell::BorrowMutError,
    core::char::DecodeUtf16Error,
    core::char::EscapeDebug,
    core::char::EscapeDefault,
    core::char::EscapeUnicode,
    core::char::ParseCharError,
    core::char::ToLowercase,
    core::char::ToUppercase,
    core::cmp::Ordering,
    core::fmt::Alignment,
    core::fmt::Error,
    core::mem::Discriminant<T>,
    core::num::FpCategory,
    core::num::NonZeroU8,
    core::num::NonZeroU16,
    core::num::NonZeroU32,
    core::num::NonZeroU64,
    core::num::NonZeroU128,
    core::num::NonZeroUsize,
    core::num::ParseFloatError,
    core::num::ParseIntError,
    core::ops::RangeFull,
    core::str::CharIndices<'a>,
    core::str::Chars<'a>,
    core::str::Utf8Error,
    core::sync::atomic::AtomicBool,
    core::sync::atomic::AtomicIsize,
    core::sync::atomic::AtomicPtr<T>,
    core::sync::atomic::AtomicUsize,
    core::sync::atomic::Ordering,
    core::time::Duration,
}

// SIMD types (x86).
#[cfg(target_arch = "x86")]
neutralize_as_self! {
    core::arch::x86::CpuidResult,
    core::arch::x86::__m128,
    core::arch::x86::__m128d,
    core::arch::x86::__m128i,
    core::arch::x86::__m256,
    core::arch::x86::__m256d,
    core::arch::x86::__m256i,
}

// SIMD types (x86_64).
#[cfg(target_arch = "x86_64")]
neutralize_as_self! {
    core::arch::x86_64::CpuidResult,
    core::arch::x86_64::__m128,
    core::arch::x86_64::__m128d,
    core::arch::x86_64::__m128i,
    core::arch::x86_64::__m256,
    core::arch::x86_64::__m256d,
    core::arch::x86_64::__m256i,
}

// Sync types from libstd.
#[cfg(feature = "std")]
neutralize_as_self! {
    std::alloc::System,
    std::collections::hash_map::DefaultHasher,
    std::collections::hash_map::RandomState,
    std::env::VarError,
    std::ffi::CStr,
    std::ffi::FromBytesWithNulError,
    std::ffi::IntoStringError,
    std::ffi::NulError,
    std::ffi::OsStr,
    std::fs::DirBuilder,
    std::fs::DirEntry,
    std::fs::File,
    std::fs::FileType,
    std::fs::Metadata,
    std::fs::OpenOptions,
    std::fs::Permissions,
    std::io::Empty,
    std::io::Error,
    std::io::ErrorKind,
    std::io::Repeat,
    std::io::SeekFrom,
    std::io::Sink,
    std::io::Stderr,
    std::io::StderrLock<'a>,
    std::io::Stdin,
    std::io::StdinLock<'a>,
    std::io::Stdout,
    std::io::StdoutLock<'a>,
    std::net::AddrParseError,
    std::net::Incoming<'a>,
    std::net::IpAddr,
    std::net::Ipv4Addr,
    std::net::Ipv6Addr,
    std::net::Shutdown,
    std::net::SocketAddr,
    std::net::SocketAddrV4,
    std::net::SocketAddrV6,
    std::net::TcpListener,
    std::net::TcpStream,
    std::net::UdpSocket,
    std::panic::Location<'a>,
    std::path::Ancestors<'a>,
    std::path::Components<'a>,
    std::path::Display<'a>,
    std::path::Iter<'a>,
    std::path::Path,
    std::path::PrefixComponent<'a>,
    std::path::StripPrefixError,
    std::process::Child,
    std::process::ChildStderr,
    std::process::ChildStdin,
    std::process::ChildStdout,
    std::process::ExitStatus,
    std::process::Output,
    std::process::Stdio,
    std::sync::Arc<T> (T: (?Sized + Send + Sync)),
    std::sync::Barrier,
    std::sync::BarrierWaitResult,
    std::sync::Condvar,
    std::sync::Mutex<T> (T: (?Sized + Send)),
    std::sync::MutexGuard<'a, T> (T: (?Sized + Sync)),
    std::sync::Once,
    std::sync::PoisonError<T> (T: (Sync)),
    std::sync::RwLockReadGuard<'a, T> (T: (?Sized + Sync)),
    std::sync::RwLockWriteGuard<'a, T> (T: (?Sized + Sync)),
    std::sync::TryLockError<T> (T: (Sync)),
    std::sync::WaitTimeoutResult,
    std::sync::Weak<T> (T: (?Sized + Send + Sync)),
    std::sync::mpsc::RecvError,
    std::sync::mpsc::RecvTimeoutError,
    std::sync::mpsc::SendError<T> (T: (Sync)),
    std::sync::mpsc::SyncSender<T> (T: (Send)),
    std::sync::mpsc::TryRecvError,
    std::sync::mpsc::TrySendError<T> (T: (Sync)),
    std::thread::AccessError,
    std::thread::Builder,
    std::thread::JoinHandle<T>,
    std::thread::LocalKey<T>,
    std::thread::Thread,
    std::thread::ThreadId,
    std::time::Instant,
    std::time::SystemTime,
    std::time::SystemTimeError,
}

macro_rules! neutralize_array {
    ($($len:expr,)*) => {$(
        unsafe impl<T> NeutralizeUnsafe for [T; $len]
        where
            T: NeutralizeUnsafe,
        {
            type Output = [Inert<T>; $len];

            #[inline]
            unsafe fn neutralize_unsafe(&self) -> &Self::Output {
                &*(self as *const Self as *const Self::Output)
            }
        }
    )*}
}

neutralize_array! {
    0, 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,
}

macro_rules! neutralize_tuple {
    ($(($($p:ident),*),)*) => {$(
        unsafe impl<$($p),*> NeutralizeUnsafe for ($($p,)*) {
            type Output = ($(Inert<$p>),*);

            #[inline]
            unsafe fn neutralize_unsafe(&self) -> &Self::Output {
                &*(self as *const Self as *const Self::Output)
            }
        }

        unsafe impl<$($p),*> NeutralizeMut for ($($p,)*)
        where
            $($p: NeutralizeMut,)*
        {
        }

        unsafe impl<$($p),*> Neutralize for ($($p,)*)
        where
            $($p: Neutralize,)*
        {
        }
    )*}
}

neutralize_tuple! {
    (),
    (A),
    (A, B),
    (A, B, C),
    (A, B, C, D),
    (A, B, C, D, E),
    (A, B, C, D, E, F),
    (A, B, C, D, E, F, G),
    (A, B, C, D, E, F, G, H),
    (A, B, C, D, E, F, G, H, I),
    (A, B, C, D, E, F, G, H, I, J),
    (A, B, C, D, E, F, G, H, I, J, K),
    (A, B, C, D, E, F, G, H, I, J, K, L),
}

macro_rules! neutralize_as_ptr_cast {
    ($($($id:ident)::* <$($lt:lifetime,)* $($param:ident),*>,)*) => {$(
        unsafe impl<$($lt,)* $($param),*> NeutralizeUnsafe for $($id)::* <$($lt,)* $($param),*> {
            type Output = $($id)::* <$($lt,)* $(Inert<$param>),*>;

            #[inline]
            unsafe fn neutralize_unsafe(&self) -> &Self::Output {
                &*(self as *const Self as *const Self::Output)
            }
        }

        unsafe impl<$($lt,)* $($param),*> NeutralizeMut for $($id)::* <$($lt,)* $($param),*>
        where
            $($param: NeutralizeMut,)*
        {
        }

        unsafe impl<$($lt,)* $($param),*> Neutralize for $($id)::* <$($lt,)* $($param),*>
        where
            $($param: Neutralize,)*
        {
        }
    )*};
}

neutralize_as_ptr_cast! {
    Option<T>,
    Result<T, E>,
    core::cmp::Reverse<T>,
    core::hash::BuildHasherDefault<H>,
    core::ops::Bound<T>,
    core::ops::Range<Idx>,
    core::ops::RangeFrom<Idx>,
    core::ops::RangeInclusive<Idx>,
    core::ops::RangeTo<Idx>,
    core::ops::RangeToInclusive<Idx>,
    core::num::Wrapping<T>,
    core::slice::Chunks<'a, T>,
    core::slice::ChunksExact<'a, T>,
    core::slice::Iter<'a, T>,
    core::slice::RChunks<'a, T>,
    core::slice::RChunksExact<'a, T>,
}

#[cfg(feature = "std")]
neutralize_as_ptr_cast! {
    std::collections::binary_heap::BinaryHeap<T>,
    std::collections::binary_heap::Iter<'a, T>,
    std::collections::btree_map::BTreeMap<K, V>,
    std::collections::btree_map::Entry<'a, K, V>,
    std::collections::btree_map::Iter<'a, K, V>,
    std::collections::btree_map::Keys<'a, K, V>,
    std::collections::btree_map::OccupiedEntry<'a, K, V>,
    std::collections::btree_map::Range<'a, K, V>,
    std::collections::btree_map::VacantEntry<'a, K, V>,
    std::collections::btree_set::BTreeSet<T>,
    std::collections::btree_set::Iter<'a, T>,
    std::collections::btree_set::Range<'a, T>,
    std::collections::hash_map::HashMap<K, V, S>,
    std::collections::hash_map::Iter<'a, K, V>,
    std::collections::hash_map::Keys<'a, K, V>,
    std::collections::hash_map::Values<'a, K, V>,
    std::collections::hash_set::HashSet<T, S>,
    std::collections::hash_set::Iter<'a, T>,
    std::collections::linked_list::Iter<'a, T>,
    std::collections::linked_list::LinkedList<T>,
    std::collections::vec_deque::Iter<'a, T>,
    std::collections::vec_deque::VecDeque<T>,
    std::io::BufReader<R>,
    std::io::Chain<T, U>,
    std::io::Cursor<T>,
    std::io::IntoInnerError<W>,
    std::io::Take<T>,
    std::iter::Chain<A, B>,
    std::iter::Cycle<I>,
    std::iter::Empty<T>,
    std::iter::Enumerate<I>,
    std::iter::Fuse<I>,
    std::iter::Once<T>,
    std::iter::Rev<I>,
    std::iter::Skip<I>,
    std::iter::StepBy<I>,
    std::iter::Take<I>,
    std::iter::Zip<A, B>,
    std::panic::AssertUnwindSafe<T>,
}

// TODO(nox): https://github.com/rust-lang/rust/pull/58369
//
// * std::collections::hash_map::Entry
// * std::collections::hash_map::OccupiedEntry
// * std::collections::hash_map::VacantEntry