impl Default for Cell and RefCell

It is necessary to have #[deriving(Default)] for struct containing
cells like Cell<u32>.
This commit is contained in:
Stepan Koltsov 2014-11-14 22:22:42 +03:00
parent 1bf0649544
commit 7eae5b4589
2 changed files with 28 additions and 0 deletions

View File

@ -157,6 +157,7 @@
use clone::Clone;
use cmp::PartialEq;
use default::Default;
use kinds::{marker, Copy};
use ops::{Deref, DerefMut, Drop};
use option::{None, Option, Some};
@ -211,6 +212,13 @@ impl<T:Copy> Clone for Cell<T> {
}
}
#[unstable]
impl<T:Default + Copy> Default for Cell<T> {
fn default() -> Cell<T> {
Cell::new(Default::default())
}
}
#[unstable = "waiting for `PartialEq` trait to become stable"]
impl<T:PartialEq + Copy> PartialEq for Cell<T> {
fn eq(&self, other: &Cell<T>) -> bool {
@ -337,6 +345,13 @@ impl<T: Clone> Clone for RefCell<T> {
}
}
#[unstable]
impl<T:Default> Default for RefCell<T> {
fn default() -> RefCell<T> {
RefCell::new(Default::default())
}
}
#[unstable = "waiting for `PartialEq` to become stable"]
impl<T: PartialEq> PartialEq for RefCell<T> {
fn eq(&self, other: &RefCell<T>) -> bool {

View File

@ -9,6 +9,7 @@
// except according to those terms.
use core::cell::*;
use core::default::Default;
use std::mem::drop;
#[test]
@ -146,3 +147,15 @@ fn as_unsafe_cell() {
unsafe { *r2.as_unsafe_cell().get() = 1u; }
assert_eq!(1u, *r2.borrow());
}
#[test]
fn cell_default() {
let cell: Cell<u32> = Default::default();
assert_eq!(0, cell.get());
}
#[test]
fn refcell_default() {
let cell: RefCell<u64> = Default::default();
assert_eq!(0, *cell.borrow());
}