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
/*!
Some helper functions.
*/

use std_::{fmt, marker::PhantomData, mem::ManuallyDrop};

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

mod deref_nested;

pub use self::deref_nested::{DerefNested, DerefNestedOut};

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

/// Defined this function just in case that `unreachable_unchecked`
/// doesn't optimize as expected.
#[inline(always)]
#[doc(hidden)]
pub unsafe fn unreachable_unchecked() -> ! {
    std_::hint::unreachable_unchecked()
}

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

/// Gets a `PhantomData<T>`.
#[inline(always)]
pub fn as_phantomdata<T>(_: &T) -> PhantomData<T> {
    PhantomData
}

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

/// Helper type to cause an abort in a `#![no_std]` context
pub(crate) struct InfinitePanic;

impl Drop for InfinitePanic {
    fn drop(&mut self) {
        let _guard = InfinitePanic;
        panic!();
    }
}

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

#[doc(hidden)]
#[inline(never)]
#[cold]
pub fn abort_fmt(args: fmt::Arguments<'_>) -> ! {
    #[cfg(not(feature = "std"))]
    {
        let _guard = InfinitePanic;
        panic!("{}", args)
    }
    #[cfg(feature = "std")]
    {
        std::eprintln!("{}", args);
        std::process::abort()
    }
}

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

/// Information about a panic,used in `abort_with_info`.
#[derive(Debug, Copy, Clone)]
#[doc(hidden)]
pub struct PanicInfo {
    pub file: &'static str,
    pub line: u32,
    pub context: &'static str,
}

/// Prints an error message for attempting to panic out of the `abort_on_return` macro,
/// then aborts the process.
#[inline(never)]
#[cold]
#[doc(hidden)]
pub fn abort_with_info(info: &'static PanicInfo) -> ! {
    abort_fmt(format_args!(
        "\n\
        Attempted to panic.\n\
        file:{}\n\
        line:{}\n\
        {}\n\
        Aborting to handle the panic...\n\
        ",
        info.file, info.line, info.context,
    ))
}

#[doc(hidden)]
pub struct AbortBomb {
    pub fuse: &'static PanicInfo,
}

impl Drop for AbortBomb {
    fn drop(&mut self) {
        abort_with_info(self.fuse);
    }
}

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

// Used to get a `&T` from both a `T` and a `&T`
#[doc(hidden)]
#[allow(non_camel_case_types)]
pub trait _Structural_BorrowSelf {
    fn _structural_borrow_self(&self) -> &Self;
    fn _structural_borrow_self_mut(&mut self) -> &mut Self;
}

impl<T> _Structural_BorrowSelf for T
where
    T: ?Sized,
{
    #[inline(always)]
    fn _structural_borrow_self(&self) -> &Self {
        self
    }

    #[inline(always)]
    fn _structural_borrow_self_mut(&mut self) -> &mut Self {
        self
    }
}

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

/// Takes the contents out of a `ManuallyDrop<T>`.
///
/// # Safety
///
/// After this function is called `slot` becomes uninitialized and
/// must not be used again.
pub unsafe fn take_manuallydrop<T>(slot: &mut ManuallyDrop<T>) -> T {
    #[cfg(feature = "rust_1_42")]
    {
        ManuallyDrop::take(slot)
    }
    #[cfg(not(feature = "rust_1_42"))]
    {
        ManuallyDrop::into_inner(std_::ptr::read(slot))
    }
}

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

/// A wrapper type to run a closure(`F` type parameter) with a value(`T` type parameter).
///
/// This type allows accessing the value before it's passed by value to the closure.
pub struct RunOnDrop<T, F>
where
    F: FnOnce(T),
{
    value: ManuallyDrop<T>,
    function: ManuallyDrop<F>,
}

impl<T, F> RunOnDrop<T, F>
where
    F: FnOnce(T),
{
    /// Constructs this RunOnDrop.
    #[inline(always)]
    pub fn new(value: T, function: F) -> Self {
        Self {
            value: ManuallyDrop::new(value),
            function: ManuallyDrop::new(function),
        }
    }
}

impl<T, F> RunOnDrop<T, F>
where
    F: FnOnce(T),
{
    /// Reborrows the wrapped value mutably.
    #[inline(always)]
    pub fn get_mut(&mut self) -> &mut T {
        &mut *self.value
    }
}

impl<'a, T, F> RunOnDrop<&'a mut T, F>
where
    F: FnOnce(&'a mut T),
{
    /// Reborrows the wrapped reference.
    #[inline(always)]
    pub fn reborrow(&self) -> &T {
        &*self.value
    }
    /// Reborrows the wrapped reference mutably.
    #[inline(always)]
    pub fn reborrow_mut(&mut self) -> &mut T {
        &mut *self.value
    }
}

impl<'a, T, F> Drop for RunOnDrop<T, F>
where
    F: FnOnce(T),
{
    #[inline(always)]
    fn drop(&mut self) {
        unsafe {
            let value = take_manuallydrop(&mut self.value);
            let function = take_manuallydrop(&mut self.function);
            function(value);
        }
    }
}