2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-03-16 13:11:31 -05:00
|
|
|
use cast::transmute;
|
2013-02-25 15:23:16 -06:00
|
|
|
use option;
|
|
|
|
use prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-03-16 13:11:31 -05:00
|
|
|
impl<T:cmp::Eq> cmp::Eq for Cell<T> {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn eq(&self, other: &Cell<T>) -> bool {
|
2013-03-16 13:11:31 -05:00
|
|
|
unsafe {
|
|
|
|
let frozen_self: &Option<T> = transmute(&mut self.value);
|
|
|
|
let frozen_other: &Option<T> = transmute(&mut other.value);
|
|
|
|
frozen_self == frozen_other
|
|
|
|
}
|
|
|
|
}
|
2013-03-21 23:20:48 -05:00
|
|
|
fn ne(&self, other: &Cell<T>) -> bool { !self.eq(other) }
|
2013-03-16 13:11:31 -05:00
|
|
|
}
|
|
|
|
|
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> {
|
2013-02-15 01:30:30 -06:00
|
|
|
Cell { value: Some(value) }
|
2012-08-09 21:06:06 -05:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub 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
|
|
|
}
|
|
|
|
|
2013-02-26 19:47:41 -06:00
|
|
|
pub impl<T> Cell<T> {
|
2012-08-09 21:06:06 -05:00
|
|
|
/// Yields the value, failing if the cell is empty.
|
2013-03-04 21:36:15 -06:00
|
|
|
fn take(&self) -> T {
|
2012-08-12 18:36:07 -05:00
|
|
|
if self.is_empty() {
|
2013-02-11 21:26:38 -06: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;
|
2013-02-15 01:30:30 -06:00
|
|
|
return option::unwrap(value);
|
2012-08-09 21:06:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the value, failing if the cell is full.
|
2013-03-04 21:36:15 -06:00
|
|
|
fn put_back(&self, value: T) {
|
2012-08-12 18:36:07 -05:00
|
|
|
if !self.is_empty() {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(~"attempt to put a value back into a full cell");
|
2012-08-09 21:06:06 -05:00
|
|
|
}
|
2013-02-15 01:30:30 -06:00
|
|
|
self.value = Some(value);
|
2012-08-09 21:06:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the cell is empty and false if the cell is full.
|
2013-03-21 23:20:48 -05:00
|
|
|
fn is_empty(&self) -> 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.
|
2013-03-07 16:38:38 -06:00
|
|
|
fn with_ref<R>(&self, op: &fn(v: &T) -> R) -> R {
|
2012-08-13 17:06:13 -05:00
|
|
|
let v = self.take();
|
|
|
|
let r = op(&v);
|
2013-02-15 01:30:30 -06:00
|
|
|
self.put_back(v);
|
|
|
|
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);
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(!value_cell.is_empty());
|
2012-08-12 18:36:07 -05:00
|
|
|
let value = value_cell.take();
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(value == ~10);
|
|
|
|
fail_unless!(value_cell.is_empty());
|
2013-02-15 01:30:30 -06:00
|
|
|
value_cell.put_back(value);
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(!value_cell.is_empty());
|
2012-08-12 18:36:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
}
|