rust/src/libcore/cell.rs

103 lines
2.6 KiB
Rust
Raw Normal View History

// 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.
use cast::transmute;
use option;
use prelude::*;
/// A dynamic, mutable location.
///
/// Similar to a mutable option type, but friendlier.
pub struct Cell<T> {
mut value: Option<T>
}
impl<T:cmp::Eq> cmp::Eq for Cell<T> {
fn eq(&self, other: &Cell<T>) -> bool {
unsafe {
let frozen_self: &Option<T> = transmute(&mut self.value);
let frozen_other: &Option<T> = transmute(&mut other.value);
frozen_self == frozen_other
}
}
fn ne(&self, other: &Cell<T>) -> bool { !self.eq(other) }
}
/// Creates a new full cell with the given value.
pub fn Cell<T>(value: T) -> Cell<T> {
2013-02-15 01:30:30 -06:00
Cell { value: Some(value) }
}
pub fn empty_cell<T>() -> Cell<T> {
2012-08-20 14:23:37 -05:00
Cell { value: None }
}
pub impl<T> Cell<T> {
/// Yields the value, failing if the cell is empty.
2013-03-04 21:36:15 -06:00
fn take(&self) -> T {
if self.is_empty() {
fail!(~"attempt to take an empty cell");
}
2012-08-20 14:23:37 -05:00
let mut value = None;
value <-> self.value;
2013-02-15 01:30:30 -06:00
return option::unwrap(value);
}
/// Returns the value, failing if the cell is full.
2013-03-04 21:36:15 -06:00
fn put_back(&self, value: T) {
if !self.is_empty() {
fail!(~"attempt to put a value back into a full cell");
}
2013-02-15 01:30:30 -06:00
self.value = Some(value);
}
/// Returns true if the cell is empty and false if the cell is full.
fn is_empty(&self) -> bool {
self.value.is_none()
}
// Calls a closure with a reference to the value.
fn with_ref<R>(&self, op: &fn(v: &T) -> R) -> R {
let v = self.take();
let r = op(&v);
2013-02-15 01:30:30 -06:00
self.put_back(v);
r
}
}
#[test]
fn test_basic() {
let value_cell = Cell(~10);
fail_unless!(!value_cell.is_empty());
let value = value_cell.take();
fail_unless!(value == ~10);
fail_unless!(value_cell.is_empty());
2013-02-15 01:30:30 -06:00
value_cell.put_back(value);
fail_unless!(!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);
}