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