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
use super::{DropFields, MovedOutFields, PrePostDropFields};

use std_::mem::ManuallyDrop;

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

/// Helper type for moving fields out of a Structural type.
///
/// # Drop behavior
///
/// The wrapped value is put inside a `ManuallyDrop` so that its destructor doesn't run.
///
/// When this is dropped,instead of running the destructor for the wrapped value,
/// this calls [`DropFields::drop_fields`] on it,
/// to drop the fields that haven't been moved out.
///
/// [`DropFields::drop_fields`]: ./trait.DropFields.html#tymethod.drop_fields
///
/// # Example
///
/// For an example that uses `IntoFieldsWrapper` there's the
/// [implementation example] for [`RevIntoMultiFieldImpl`]
///
/// [implementation example]:
/// ../multi_fields/trait.RevIntoMultiFieldImpl.html#implementation-example
///
/// [`RevIntoMultiFieldImpl`]: ../multi_fields/trait.RevIntoMultiFieldImpl.html
///
pub struct IntoFieldsWrapper<T: DropFields> {
    value: ManuallyDrop<T>,
    moved: MovedOutFields,
}

impl<T: DropFields> IntoFieldsWrapper<T> {
    /// Constructs this `IntoFieldsWrapper`,wrapping the `value`.
    ///
    /// Also calls `DropFields::pre_move` on the wrapped value
    ///
    #[inline(always)]
    pub fn new(mut value: T) -> Self {
        DropFields::pre_move(&mut value);
        Self {
            value: ManuallyDrop::new(value),
            moved: MovedOutFields::new(),
        }
    }

    /// Gets mutable references to the wrapped value,
    /// and the `MovedOutFields` that tracks which fields were moved out of it.
    ///
    /// # Safety
    ///
    /// The returned references must not be mutated,
    /// only passed to accessor trait (declared in structural) methods for moving out fields .
    ///
    /// Mutating `MovedOutFields` incorrectly can lead to leaks and double dropped fields.
    #[inline(always)]
    pub unsafe fn inner_and_moved_mut(&mut self) -> (&mut T, &mut MovedOutFields) {
        (&mut self.value, &mut self.moved)
    }

    /// Gets mutable pointers to the wrapped value,
    /// and the `MovedOutFields` that tracks which fields were moved out of it.
    ///
    /// # Safety
    ///
    /// The returned pointers must not be mutated,
    /// only passed to accessor trait (declared in structural) methods for moving out fields .
    ///
    /// Mutating `MovedOutFields` incorrectly can lead to leaks and double dropped fields.
    #[inline(always)]
    pub unsafe fn inner_and_moved_raw(&mut self) -> (*mut T, *mut MovedOutFields) {
        (&mut *self.value as *mut T, &mut self.moved as *mut _)
    }
}

impl<T: DropFields> Drop for IntoFieldsWrapper<T> {
    #[inline(always)]
    fn drop(&mut self) {
        unsafe {
            DropFields::drop_fields(&mut *self.value, self.moved);
        }
    }
}

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

macro_rules! declare_run_on_drop {
    (
        $(#[$meta:meta])*
        struct $struct:ident
        $(where[$($where_preds:tt)*])?
        $(#[$new_meta:meta])*
        $(unsafe $(#$dummy:ident#)?)? fn new($($extra_var:ident : $extra_ty:ty),* $(,)?)
        this=$this:ident,
        fn drop(){$($drop:tt)*}
    ) => (
        $(#[$meta])*
        pub struct $struct<'a,T>
        $(where $($where_preds)*)?
        {
            mutref:&'a mut T,
            $($extra_var : $extra_ty,)*
        }

        impl<'a,T> $struct<'a,T>
        $(where $($where_preds)*)?
        {
            $(#[$new_meta])*
            /// # Drop order
            ///
            /// Remember that variables on the stack are dropped in the opposite order
            /// than they are declared.
            ///
            /// In this example:
            /// ```ignore
            /// let a=Foo;
            /// let b=Bar;
            /// let c=Baz;
            /// ```
            /// `c` gets dropped first,then `b`, then `a`.
            #[inline(always)]
            pub $(unsafe $(#$dummy#)?)?
            fn new(mutref:&'a mut T $(,$extra_var : $extra_ty)*)->Self{
                Self{
                    mutref,
                    $($extra_var,)*
                }
            }

            /// Reborrows the wrapped mutable reference.
            ///
            /// # Safety
            ///
            /// The returned references must not be mutated,
            /// only passed to accessor trait (declared in structural) methods for moving out fields .
            #[inline(always)]
            pub unsafe fn get_mut(&mut self)->&mut T{
                self.mutref
            }
        }

        impl<'a,T> Drop for  $struct<'a,T>
        $(where $($where_preds)*)?
        {
            #[inline(always)]
            fn drop(&mut self){
                let $this=self;
                $($drop)*
            }
        }

    )
}

declare_run_on_drop! {
    /// A guard that drops the value that a mutable reference points when *it* is dropped.
    struct RunDrop

    /// Constructs this RunDrop.
    ///
    /// # Safety
    ///
    /// In the destructor for this type,
    /// this drops the value that the mutable reference points to.
    ///
    /// Once the destructor for this type runs,the pointed-to value must not be used again,
    /// that includes the destructor for the value running.
    unsafe fn new()

    this=this,
    fn drop(){
        unsafe{
            std_::ptr::drop_in_place(this.mutref)
        }
    }
}

declare_run_on_drop! {
    /// A guard that calls [`PrePostDropFields::post_drop`] on the mutable reference
    /// when *it* is dropped.
    ///
    /// [`PrePostDropFields::post_drop`]: ./trait.PrePostDropFields.html#tymethod.post_drop
    struct RunPostDrop
    where[ T: PrePostDropFields ]

    /// Constructs this RunPostDrop.
    ///
    /// # Safety
    ///
    /// This has the same safety requirements as [`PrePostDropFields::post_drop`].
    ///
    /// [`PrePostDropFields::post_drop`]: ./trait.PrePostDropFields.html#tymethod.post_drop
    unsafe fn new()

    this=this,
    fn drop(){
        unsafe{
            PrePostDropFields::post_drop(this.mutref)
        }
    }
}

declare_run_on_drop! {
    /// A guard that calls [`DropFields::drop_fields`] on the mutable reference
    /// when *it* is dropped.
    ///
    /// [`DropFields::drop_fields`]: ./trait.DropFields.html#tymethod.drop_fields
    struct RunDropFields
    where[ T: DropFields ]

    /// Constructs this RunDropFields.
    ///
    /// # Safety
    ///
    /// This has the same safety requirements as [`DropFields::drop_fields`].
    ///
    /// [`DropFields::drop_fields`]: ./trait.DropFields.html#tymethod.drop_fields
    unsafe fn new(moved: MovedOutFields)

    this=this,
    fn drop(){
        unsafe{
            this.mutref.drop_fields(this.moved)
        }
    }
}

impl<'a, T> RunDropFields<'a, T>
where
    T: DropFields,
{
    /// Gets mutable references to the wrapped value,
    /// and the `MovedOutFields` that tracks which fields were moved out of it
    ///
    /// # Safety
    ///
    /// The returned references must not be mutated,
    /// only passed to accessor trait (declared in structural) methods for moving out fields .
    ///
    /// Mutating `MovedOutFields` incorrectly can lead to leaks and double dropped fields.
    pub unsafe fn get_mut_and_moved_fields(&mut self) -> (&mut T, &mut MovedOutFields) {
        (&mut self.mutref, &mut self.moved)
    }
}