use std::fmt;
use std::cell::UnsafeCell;
pub struct Take<T> {
val: UnsafeCell<Option<T>>,
}
impl<T> Take<T> {
pub fn new(val: T) -> Take<T> {
Take { val: UnsafeCell::new(Some(val)) }
}
pub fn take(&self) -> T {
unsafe { (*self.val.get()).take() }.expect("value already consumed")
}
fn inner(&self) -> &Option<T> {
unsafe { &*self.val.get() }
}
}
impl<T: fmt::Debug> fmt::Debug for Take<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Take")
.field("val", self.inner())
.finish()
}
}