2012-08-31 15:07:49 -07:00
|
|
|
#[forbid(deprecated_mode)];
|
2012-08-09 19:06:06 -07:00
|
|
|
/// A dynamic, mutable location.
|
|
|
|
|
///
|
|
|
|
|
/// Similar to a mutable option type, but friendlier.
|
|
|
|
|
|
2012-09-28 14:55:31 -07:00
|
|
|
pub struct Cell<T> {
|
2012-09-07 14:50:47 -07:00
|
|
|
mut value: Option<T>
|
2012-08-09 19:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a new full cell with the given value.
|
2012-09-28 14:55:31 -07:00
|
|
|
pub fn Cell<T>(+value: T) -> Cell<T> {
|
2012-08-20 12:23:37 -07:00
|
|
|
Cell { value: Some(move value) }
|
2012-08-09 19:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
2012-09-28 14:55:31 -07:00
|
|
|
pub fn empty_cell<T>() -> Cell<T> {
|
2012-08-20 12:23:37 -07:00
|
|
|
Cell { value: None }
|
2012-08-09 19:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Cell<T> {
|
|
|
|
|
/// Yields the value, failing if the cell is empty.
|
|
|
|
|
fn take() -> T {
|
2012-08-12 16:36:07 -07:00
|
|
|
if self.is_empty() {
|
2012-08-12 16:26:45 -07:00
|
|
|
fail ~"attempt to take an empty cell";
|
2012-08-09 19:06:06 -07:00
|
|
|
}
|
2012-08-12 16:36:07 -07:00
|
|
|
|
2012-08-20 12:23:37 -07:00
|
|
|
let mut value = None;
|
2012-08-12 16:36:07 -07:00
|
|
|
value <-> self.value;
|
2012-09-11 17:17:54 -07:00
|
|
|
return option::unwrap(move value);
|
2012-08-09 19:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the value, failing if the cell is full.
|
|
|
|
|
fn put_back(+value: T) {
|
2012-08-12 16:36:07 -07:00
|
|
|
if !self.is_empty() {
|
2012-08-12 16:26:45 -07:00
|
|
|
fail ~"attempt to put a value back into a full cell";
|
2012-08-09 19:06:06 -07:00
|
|
|
}
|
2012-08-20 12:23:37 -07:00
|
|
|
self.value = Some(move value);
|
2012-08-09 19:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns true if the cell is empty and false if the cell is full.
|
|
|
|
|
fn is_empty() -> bool {
|
|
|
|
|
self.value.is_none()
|
|
|
|
|
}
|
2012-08-16 19:36:49 -07:00
|
|
|
|
|
|
|
|
// Calls a closure with a reference to the value.
|
2012-08-13 15:06:13 -07:00
|
|
|
fn with_ref<R>(op: fn(v: &T) -> R) -> R {
|
|
|
|
|
let v = self.take();
|
|
|
|
|
let r = op(&v);
|
2012-09-10 17:50:48 -07:00
|
|
|
self.put_back(move v);
|
|
|
|
|
move r
|
2012-08-16 19:36:49 -07:00
|
|
|
}
|
2012-08-09 19:06:06 -07:00
|
|
|
}
|
2012-08-12 16:36:07 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_basic() {
|
|
|
|
|
let value_cell = Cell(~10);
|
|
|
|
|
assert !value_cell.is_empty();
|
|
|
|
|
let value = value_cell.take();
|
|
|
|
|
assert value == ~10;
|
|
|
|
|
assert value_cell.is_empty();
|
|
|
|
|
value_cell.put_back(value);
|
|
|
|
|
assert !value_cell.is_empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[should_fail]
|
|
|
|
|
#[ignore(cfg(windows))]
|
|
|
|
|
fn test_take_empty() {
|
|
|
|
|
let value_cell = empty_cell::<~int>();
|
|
|
|
|
value_cell.take();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[should_fail]
|
|
|
|
|
#[ignore(cfg(windows))]
|
|
|
|
|
fn test_put_back_non_empty() {
|
|
|
|
|
let value_cell = Cell(~10);
|
|
|
|
|
value_cell.put_back(~20);
|
2012-08-16 19:36:49 -07:00
|
|
|
}
|