Skip to content

impl Default for Cell and RefCell #18960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions src/libcoretest/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// except according to those terms.

use core::cell::*;
use core::default::Default;
use std::mem::drop;

#[test]
Expand Down Expand Up @@ -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());
}