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
//! This crate provides convenient wrappers for dealing with `&mut Option<T>`. There are two main types, `OptionGuard` and `OptionGuardMut`:
//! 
//! ## `OptionGuard`
//! 
//! Using `EmptyOptionExt::steal` on an `&mut Option<T>` produces the `T` from the option as well as an `OptionGuard`. If `OptionGuard::restore` is not called before the `OptionGuard` is dropped, then a panic will occur.
//! 
//! ### Examples
//! 
//! Calling `guard.restore()` puts the stolen value back into the original option:
//! 
//! ```rust
//! use empty_option::EmptyOptionExt;
//! 
//! // A mutable option, from which we shall steal a value!
//! let mut thing = Some(5);
//! 
//! // Scope so that when we do `guard.restore()`, the mutable borrow on `thing` will end.
//! {
//!     // Steal the value - we now have the guard and also a concrete `T` from our `Option<T>`.
//!     let (guard, five) = thing.steal();
//! 
//!     assert_eq!(five, 5);
//! 
//!     // Move the value back into `thing` - we're done.
//!     guard.restore(6);
//! }
//! 
//! // The value is returned by `guard.restore()`.
//! assert_eq!(thing, Some(6));
//! ```
//! 
//! But, if the guard is dropped instead, a runtime panic results.
//! 
//! ```rust,should_panic
//! use empty_option::EmptyOptionExt;
//! 
//! let mut thing = Some(5);
//! 
//! let (_, _) = thing.steal();
//! 
//! // Never return the value!
//! ```
//!
//! Calling `.steal()` on a `None` immediately panics:
//!
//! ```rust,should_panic
//! let mut thing = None;
//! 
//! // Panics here!
//! let (guard, _) = thing.steal();
//! 
//! guard.restore(5);
//! ```
//! 
//! ## `OptionGuardMut`
//! 
//! Using `EmptyOptionExt::steal_mut` on an `&mut Option<T>` produces an `OptionGuardMut`, which dereferences to a `T`. To get the inner value out, `OptionGuardMut::into_inner` can be called. On `Drop`, if the `OptionGuardMut` is not consumed with `OptionGuardMut::into_inner`, the value in the `OptionGuardMut` will be returned to the `Option` that it was borrowed from.
//! 
//! ### Examples
//! 
//! Take a value from an option, which is automatically returned:
//! 
//! ```rust
//! use empty_option::EmptyOptionExt;
//! 
//! let mut thing = Some(5);
//! 
//! {
//!     let mut stolen = thing.steal_mut();
//! 
//!     assert_eq!(*stolen, 5);
//! 
//!     *stolen = 6;
//! }
//! 
//! assert_eq!(thing, Some(6));
//! ```
//! 
//! If the guard is consumed, the value is never returned.
//! 
//! ```rust
//! use empty_option::EmptyOptionExt;
//! 
//! let mut thing = Some(5);
//! 
//! {
//!     // Keep the thing!
//!     let stolen = thing.steal_mut().into_inner();
//! 
//!     assert_eq!(stolen, 5);
//! }
//! 
//! assert_eq!(thing, None);
//!
//! ```
//! 
//! Calling `steal_mut` on a `None` immediately panics:
//!
//! ```rust,should_panic
//! let mut thing: Option<i32> = None;
//! 
//! // Panics here!
//! thing.steal_mut();
//! ```

use std::mem;
use std::ops::{Deref, DerefMut};


/// Extension trait providing nice method sugar for `steal` and `steal_mut`.
pub trait EmptyOptionExt {
    type Inner;

    /// Take a value out of an option, providing a guard which panics if the value is not returned.
    /// Panics on `None`.
    fn steal(&mut self) -> (OptionGuard<Self::Inner>, Self::Inner);

    /// Take a value out of an option, providing a guard which returns the value unless consumed by
    /// `OptionGuardMut::into_inner`. Panics on `None`.
    fn steal_mut<'a>(&'a mut self) -> OptionGuardMut<'a, Self::Inner>;
}


/// An option which has had its value taken. On `Drop`, `OptionGuard` will panic - in order to
/// prevent a panic, the stolen value must be moved back in with `OptionGuard::restore`.
///
/// This is useful if you are using an `Option` because you have a value which you need to take,
/// and then deal with by-value, but you want to preserve the invariant that your optional value is
/// always present.
/// 
/// # Examples
///
/// Calling `guard.restore()` puts the stolen value back into the original option:
///
/// ```
/// # use empty_option::EmptyOptionExt;
/// // A mutable option, from which we shall steal a value!
/// let mut thing = Some(5);
/// 
/// // Scope so that when we do `guard.restore()`, the mutable borrow on `thing` will end.
/// {
///     // Steal the value - we now have the guard and also a concrete `T` from our `Option<T>`.
///     let (guard, five) = thing.steal();
/// 
///     assert_eq!(five, 5);
/// 
///     // Move the value back into `thing` - we're done.
///     guard.restore(6);
/// }
/// 
/// // The value is returned by `guard.restore()`.
/// assert_eq!(thing, Some(6));
/// ```
///
/// But, if the guard is dropped instead, a runtime panic results.
///
/// ```should_panic
/// # use empty_option::EmptyOptionExt;
/// let mut thing = Some(5);
/// 
/// let (_, _) = thing.steal();
/// 
/// // Never return the value!
/// ```
///
/// Calling `.steal()` on a `None` immediately panics:
///
/// ```rust,should_panic
/// let mut thing = None;
/// 
/// // Panics here!
/// let (guard, _) = thing.steal();
/// 
/// guard.restore(5);
/// ```
pub struct OptionGuard<'a, T: 'a> {
    opt: &'a mut Option<T>,
}


impl<'a, T> Drop for OptionGuard<'a, T> {
    fn drop(&mut self) {
        panic!("`Some` value was never restored to a victimized Option!");
    }
}


impl<'a, T> OptionGuard<'a, T> {
    fn new(opt: &'a mut Option<T>) -> OptionGuard<'a, T> {
        OptionGuard {
            opt
        }
    }


    /// Restore a stolen value to an `Option`.
    pub fn restore(self, obj: T) {
        *self.opt = Some(obj);

        mem::forget(self);
    }
}


/// A value taken from an `Option<T>`. `OptionGuardMut<T>` dereferences to a `T`, and the inner `T`
/// can be moved out with `OptionGuardMut::into_inner`. When dropped, the `OptionGuardMut` moves
/// the taken value back into the `Option` it came from.
///
/// # Examples
///
/// Take a value from an option, which is automatically returned:
///
/// ```
/// # use empty_option::EmptyOptionExt;
/// let mut thing = Some(5);
/// 
/// {
///     let mut stolen = thing.steal_mut();
/// 
///     assert_eq!(*stolen, 5);
/// 
///     *stolen = 6;
/// }
/// 
/// assert_eq!(thing, Some(6));
/// ```
/// 
/// If the guard is consumed, the value is never returned.
///
/// ```
/// # use empty_option::EmptyOptionExt;
/// let mut thing = Some(5);
/// 
/// {
///     // Keep the thing!
///     let stolen = thing.steal_mut().into_inner();
/// 
///     assert_eq!(stolen, 5);
/// }
/// 
/// assert_eq!(thing, None);
/// ```
///
/// Calling `steal_mut` on a `None` immediately panics:
///
/// ```rust,should_panic
/// let mut thing: Option<i32> = None;
/// 
/// // Panics here!
/// thing.steal_mut();
/// ```
pub struct OptionGuardMut<'a, T: 'a> {
    origin: &'a mut Option<T>,
    value: Option<T>,
}


impl<'a, T> Drop for OptionGuardMut<'a, T> {
    fn drop(&mut self) {
        *self.origin = self.value.take();
    }
}


impl<'a, T> OptionGuardMut<'a, T> {
    /// Keep the value stolen from the `Option` and do not return it.
    pub fn into_inner(mut self) -> T {
        self.value.take().unwrap()
    }
}


impl<'a, T> Deref for OptionGuardMut<'a, T> {
    type Target = T;

    fn deref(&self) -> &T {
        self.value.as_ref().unwrap()
    }
}


impl<'a, T> DerefMut for OptionGuardMut<'a, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.value.as_mut().unwrap()
    }
}


impl<T> EmptyOptionExt for Option<T> {
    type Inner = T;

    fn steal(&mut self) -> (OptionGuard<T>, T) {
        let value = self.take().expect("attempted to steal from None");
        (OptionGuard::new(self), value)
    }

    fn steal_mut(&mut self) -> OptionGuardMut<T> {
        let value = Some(self.take().expect("attempted to steal from None"));

        OptionGuardMut {
            origin: self,
            value,
        }
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn catch_and_release() {
        let mut thing = Some(5);

        {
            let (guard, five) = thing.steal();

            assert_eq!(five, 5);

            guard.restore(6);
        }

        assert_eq!(thing, Some(6));
    }

    #[test]
    #[should_panic]
    fn catch_and_keep() {
        let mut thing = Some(5);

        let (_, _) = thing.steal();

        // Never return the value!
    }

    #[test]
    #[should_panic]
    fn catch_from_none() {
        let mut thing = None;

        let (guard, _) = thing.steal();

        guard.restore(5);
    }

    #[test]
    fn mut_and_release() {
        let mut thing = Some(5);

        {
            let mut stolen = thing.steal_mut();

            assert_eq!(*stolen, 5);

            *stolen = 6;
        }

        assert_eq!(thing, Some(6));
    }

    #[test]
    fn mut_and_keep() {
        let mut thing = Some(5);
        
        {
            // Keep the thing!
            let stolen = thing.steal_mut().into_inner();

            assert_eq!(stolen, 5);
        }

        assert_eq!(thing, None);
    }

    #[test]
    #[should_panic]
    fn mut_from_none() {
        let mut thing: Option<i32> = None;

        thing.steal_mut();
    }
}