2013-05-30 03:16:33 -07:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08: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-11-16 13:26:15 -08:00
|
|
|
//! Runtime move semantics
|
2013-03-24 18:59:04 -07:00
|
|
|
|
2013-05-31 15:17:22 -07:00
|
|
|
#[missing_doc];
|
|
|
|
|
|
2013-04-02 23:15:04 +02:00
|
|
|
use cast::transmute_mut;
|
2013-02-25 13:23:16 -08:00
|
|
|
use prelude::*;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2013-03-24 18:59:04 -07:00
|
|
|
/*
|
|
|
|
|
A dynamic, mutable location.
|
|
|
|
|
|
|
|
|
|
Similar to a mutable option type, but friendlier.
|
|
|
|
|
*/
|
2012-08-09 19:06:06 -07:00
|
|
|
|
2013-06-28 14:36:33 -07:00
|
|
|
#[no_freeze]
|
2013-05-24 01:16:15 -04:00
|
|
|
#[deriving(Clone, DeepClone, Eq)]
|
2013-05-28 16:35:52 -05:00
|
|
|
#[allow(missing_doc)]
|
2012-09-28 14:55:31 -07:00
|
|
|
pub struct Cell<T> {
|
2013-04-22 13:01:32 -04:00
|
|
|
priv value: Option<T>
|
2012-08-09 19:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
2013-06-04 12:03:58 +02: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 19:06:06 -07:00
|
|
|
|
|
|
|
|
/// Yields the value, failing if the cell is empty.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn take(&self) -> T {
|
2013-05-10 15:15:06 -07:00
|
|
|
let this = unsafe { transmute_mut(self) };
|
|
|
|
|
if this.is_empty() {
|
2013-10-21 13:08:31 -07:00
|
|
|
fail!("attempt to take an empty cell");
|
2012-08-09 19:06:06 -07:00
|
|
|
}
|
2012-08-12 16:36:07 -07:00
|
|
|
|
2013-07-17 23:41:50 +02:00
|
|
|
this.value.take_unwrap()
|
2012-08-09 19:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
2013-08-21 18:43:50 -04: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 19:06:06 -07:00
|
|
|
/// Returns true if the cell is empty and false if the cell is full.
|
2013-05-31 15:17:22 -07:00
|
|
|
pub fn is_empty(&self) -> bool {
|
2012-08-09 19:06:06 -07:00
|
|
|
self.value.is_none()
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-12 16:36:07 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_basic() {
|
2013-06-04 12:03:58 +02:00
|
|
|
let value_cell = Cell::new(~10);
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!(!value_cell.is_empty());
|
2012-08-12 16:36:07 -07:00
|
|
|
let value = value_cell.take();
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!(value == ~10);
|
|
|
|
|
assert!(value_cell.is_empty());
|
2012-08-12 16:36:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[should_fail]
|
|
|
|
|
fn test_take_empty() {
|
2013-11-16 13:26:15 -08:00
|
|
|
let value_cell: Cell<~int> = Cell::new(~0);
|
|
|
|
|
value_cell.take();
|
2012-08-12 16:36:07 -07:00
|
|
|
value_cell.take();
|
2013-04-04 11:34:35 -06:00
|
|
|
}
|