rust/src/libstd/cell.rs

75 lines
1.8 KiB
Rust
Raw Normal View History

// Copyright 2012-2013 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-11-16 15:26:15 -06:00
//! Runtime move semantics
#[missing_doc];
use cast::transmute_mut;
use prelude::*;
/*
A dynamic, mutable location.
Similar to a mutable option type, but friendlier.
*/
2013-06-28 16:36:33 -05:00
#[no_freeze]
2013-05-24 00:16:15 -05:00
#[deriving(Clone, DeepClone, Eq)]
#[allow(missing_doc)]
pub struct Cell<T> {
priv value: Option<T>
}
impl<T> Cell<T> {
/// Creates a new full cell with the given value.
pub fn new(value: T) -> Cell<T> {
Cell { value: Some(value) }
}
/// Yields the value, failing if the cell is empty.
pub fn take(&self) -> T {
let this = unsafe { transmute_mut(self) };
if this.is_empty() {
fail!("attempt to take an empty cell");
}
this.value.take_unwrap()
}
/// Yields the value if the cell is full, or `None` if it is empty.
pub fn take_opt(&self) -> Option<T> {
let this = unsafe { transmute_mut(self) };
this.value.take()
}
/// Returns true if the cell is empty and false if the cell is full.
pub fn is_empty(&self) -> bool {
self.value.is_none()
}
}
#[test]
fn test_basic() {
let value_cell = Cell::new(~10);
2013-03-28 20:39:09 -05:00
assert!(!value_cell.is_empty());
let value = value_cell.take();
2013-03-28 20:39:09 -05:00
assert!(value == ~10);
assert!(value_cell.is_empty());
}
#[test]
#[should_fail]
fn test_take_empty() {
2013-11-16 15:26:15 -06:00
let value_cell: Cell<~int> = Cell::new(~0);
value_cell.take();
value_cell.take();
}