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
//! A cell type that can move values into and out of a shared reference.
//!
//! Behaves like `RefCell<Option<T>>` but optimized for use-cases where temporary or permanent
//! ownership is required.

use std::cell::UnsafeCell;
use std::mem;

/// A cell type that can move values into and out of a shared reference.
pub struct MoveCell<T>(UnsafeCell<Option<T>>);

impl<T> MoveCell<T> {
    /// Create a new `MoveCell` with no contained value.
    pub fn new() -> MoveCell<T> {
        MoveCell(UnsafeCell::new(None))
    }

    /// Create a new `MoveCell` with the given value.
    pub fn with(val: T) -> MoveCell<T> {
        MoveCell(UnsafeCell::new(Some(val)))
    }

    /// Create a new `MoveCell<T>` around the given `Option<T>`.
    pub fn from(opt: Option<T>) -> MoveCell<T> {
        MoveCell(UnsafeCell::new(opt))
    }

    unsafe fn as_mut(&self) -> &mut Option<T> {
        &mut *self.0.get()
    }

    unsafe fn as_ref(&self) -> &Option<T> {
        & *self.0.get()
    }

    /// Place a value into this `MoveCell`, returning the previous value, if present.
    pub fn put(&self, val: T) -> Option<T> {
        mem::replace(unsafe { self.as_mut() }, Some(val))       
    }

    /// Take the value out of this `MoveCell`, leaving nothing in its place.
    pub fn take(&self) -> Option<T> {
        unsafe { self.as_mut().take() }
    }

    /// Take the value out of this `MoveCell`, leaving a clone in its place. 
    pub fn clone_inner(&self) -> Option<T> where T: Clone {
        let inner = self.take();
        inner.clone().map(|inner| self.put(inner));
        inner
    }

    /// Check if this `MoveCell` contains a value or not.
    pub fn has_value(&self) -> bool {
        unsafe { self.as_ref().is_some() }
    }
}

impl<T> Default for MoveCell<T> {
    fn default() -> Self {
        MoveCell::new()
    }
}