2013-05-30 05:16:33 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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-24 20:59:04 -05:00
|
|
|
//! A mutable, nullable memory location
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
#[missing_doc];
|
|
|
|
|
2013-04-02 16:15:04 -05:00
|
|
|
use cast::transmute_mut;
|
2013-08-08 18:34:08 -05:00
|
|
|
use unstable::finally::Finally;
|
2013-02-25 15:23:16 -06:00
|
|
|
use prelude::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-03-24 20:59:04 -05:00
|
|
|
/*
|
|
|
|
A dynamic, mutable location.
|
|
|
|
|
|
|
|
Similar to a mutable option type, but friendlier.
|
|
|
|
*/
|
2012-08-09 21:06:06 -05:00
|
|
|
|
2013-06-28 16:36:33 -05:00
|
|
|
#[no_freeze]
|
2013-05-24 00:16:15 -05:00
|
|
|
#[deriving(Clone, DeepClone, Eq)]
|
2013-05-28 16:35:52 -05:00
|
|
|
#[allow(missing_doc)]
|
2012-09-28 16:55:31 -05:00
|
|
|
pub struct Cell<T> {
|
2013-04-22 12:01:32 -05:00
|
|
|
priv value: Option<T>
|
2012-08-09 21:06:06 -05:00
|
|
|
}
|
|
|
|
|
2013-06-04 05:03:58 -05:00
|
|
|
impl<T> Cell<T> {
|
|
|
|
/// Creates a new full cell with the given value.
|
|
|
|
pub fn new(value: T) -> Cell<T> {
|
|
|
|
Cell { value: Some(value) }
|
|
|
|
}
|
2012-08-09 21:06:06 -05:00
|
|
|
|
2013-06-04 05:03:58 -05:00
|
|
|
/// Creates a new empty cell with no value inside.
|
|
|
|
pub fn new_empty() -> Cell<T> {
|
|
|
|
Cell { value: None }
|
|
|
|
}
|
2012-08-09 21:06:06 -05:00
|
|
|
|
|
|
|
/// Yields the value, failing if the cell is empty.
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn take(&self) -> T {
|
2013-05-10 17:15:06 -05:00
|
|
|
let this = unsafe { transmute_mut(self) };
|
|
|
|
if this.is_empty() {
|
2013-10-21 15:08:31 -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
|
|
|
|
2013-07-17 16:41:50 -05:00
|
|
|
this.value.take_unwrap()
|
2012-08-09 21:06:06 -05:00
|
|
|
}
|
|
|
|
|
2013-08-21 17:43:50 -05:00
|
|
|
/// 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()
|
|
|
|
}
|
|
|
|
|
2012-08-09 21:06:06 -05:00
|
|
|
/// Returns the value, failing if the cell is full.
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn put_back(&self, value: T) {
|
2013-05-10 17:15:06 -05:00
|
|
|
let this = unsafe { transmute_mut(self) };
|
|
|
|
if !this.is_empty() {
|
2013-10-21 15:08:31 -05:00
|
|
|
fail!("attempt to put a value back into a full cell");
|
2012-08-09 21:06:06 -05:00
|
|
|
}
|
2013-05-10 17:15:06 -05:00
|
|
|
this.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-05-31 17:17:22 -05:00
|
|
|
pub 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
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
/// Calls a closure with a reference to the value.
|
|
|
|
pub fn with_ref<R>(&self, op: &fn(v: &T) -> R) -> R {
|
2013-08-08 18:34:08 -05:00
|
|
|
do self.with_mut_ref |ptr| { op(ptr) }
|
2012-08-16 21:36:49 -05:00
|
|
|
}
|
2013-04-04 12:34:35 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
/// Calls a closure with a mutable reference to the value.
|
|
|
|
pub fn with_mut_ref<R>(&self, op: &fn(v: &mut T) -> R) -> R {
|
2013-08-08 18:34:08 -05:00
|
|
|
let mut v = Some(self.take());
|
|
|
|
do (|| {
|
|
|
|
op(v.get_mut_ref())
|
|
|
|
}).finally {
|
|
|
|
self.put_back(v.take_unwrap());
|
|
|
|
}
|
2013-04-04 12:34:35 -05:00
|
|
|
}
|
2012-08-09 21:06:06 -05:00
|
|
|
}
|
2012-08-12 18:36:07 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basic() {
|
2013-06-04 05:03:58 -05:00
|
|
|
let value_cell = Cell::new(~10);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!value_cell.is_empty());
|
2012-08-12 18:36:07 -05:00
|
|
|
let value = value_cell.take();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(value == ~10);
|
|
|
|
assert!(value_cell.is_empty());
|
2013-02-15 01:30:30 -06:00
|
|
|
value_cell.put_back(value);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!value_cell.is_empty());
|
2012-08-12 18:36:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_take_empty() {
|
2013-08-08 13:38:10 -05:00
|
|
|
let value_cell: Cell<~int> = Cell::new_empty();
|
2012-08-12 18:36:07 -05:00
|
|
|
value_cell.take();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
|
|
|
fn test_put_back_non_empty() {
|
2013-06-04 05:03:58 -05:00
|
|
|
let value_cell = Cell::new(~10);
|
2012-08-12 18:36:07 -05:00
|
|
|
value_cell.put_back(~20);
|
2012-08-16 21:36:49 -05:00
|
|
|
}
|
2013-04-04 12:34:35 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_ref() {
|
|
|
|
let good = 6;
|
2013-06-04 05:03:58 -05:00
|
|
|
let c = Cell::new(~[1, 2, 3, 4, 5, 6]);
|
2013-04-04 12:34:35 -05:00
|
|
|
let l = do c.with_ref() |v| { v.len() };
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(l, good);
|
2013-04-04 12:34:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_mut_ref() {
|
|
|
|
let good = ~[1, 2, 3];
|
2013-04-12 00:10:01 -05:00
|
|
|
let v = ~[1, 2];
|
2013-06-04 05:03:58 -05:00
|
|
|
let c = Cell::new(v);
|
2013-04-04 12:34:35 -05:00
|
|
|
do c.with_mut_ref() |v| { v.push(3); }
|
|
|
|
let v = c.take();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(v, good);
|
2013-04-04 12:34:35 -05:00
|
|
|
}
|