Add map and filter_map associated functions to std::cell::Ref and RefMut

See design discussion in https://github.com/rust-lang/rust/pull/25747
This commit is contained in:
Simon Sapin
2015-05-28 23:00:52 +02:00
parent c516eee503
commit d0afa6ede3
3 changed files with 209 additions and 1 deletions

View File

@@ -129,6 +129,82 @@ fn ref_clone_updates_flag() {
assert_eq!(x.borrow_state(), BorrowState::Unused);
}
#[test]
fn ref_map_does_not_update_flag() {
let x = RefCell::new(Some(5));
{
let b1: Ref<Option<u32>> = x.borrow();
assert_eq!(x.borrow_state(), BorrowState::Reading);
{
let b2: Ref<u32> = Ref::map(b1, |o| o.as_ref().unwrap());
assert_eq!(*b2, 5);
assert_eq!(x.borrow_state(), BorrowState::Reading);
}
assert_eq!(x.borrow_state(), BorrowState::Unused);
}
assert_eq!(x.borrow_state(), BorrowState::Unused);
}
#[test]
fn ref_map_accessor() {
struct X(RefCell<(u32, char)>);
impl X {
fn accessor(&self) -> Ref<u32> {
Ref::map(self.0.borrow(), |tuple| &tuple.0)
}
}
let x = X(RefCell::new((7, 'z')));
let d: Ref<u32> = x.accessor();
assert_eq!(*d, 7);
}
#[test]
fn ref_filter_map_accessor() {
struct X(RefCell<Result<u32, ()>>);
impl X {
fn accessor(&self) -> Option<Ref<u32>> {
Ref::filter_map(self.0.borrow(), |r| r.as_ref().ok())
}
}
let x = X(RefCell::new(Ok(7)));
let d: Ref<u32> = x.accessor().unwrap();
assert_eq!(*d, 7);
}
#[test]
fn ref_mut_map_accessor() {
struct X(RefCell<(u32, char)>);
impl X {
fn accessor(&self) -> RefMut<u32> {
RefMut::map(self.0.borrow_mut(), |tuple| &mut tuple.0)
}
}
let x = X(RefCell::new((7, 'z')));
{
let mut d: RefMut<u32> = x.accessor();
assert_eq!(*d, 7);
*d += 1;
}
assert_eq!(*x.0.borrow(), (8, 'z'));
}
#[test]
fn ref_mut_filter_map_accessor() {
struct X(RefCell<Result<u32, ()>>);
impl X {
fn accessor(&self) -> Option<RefMut<u32>> {
RefMut::filter_map(self.0.borrow_mut(), |r| r.as_mut().ok())
}
}
let x = X(RefCell::new(Ok(7)));
{
let mut d: RefMut<u32> = x.accessor().unwrap();
assert_eq!(*d, 7);
*d += 1;
}
assert_eq!(*x.0.borrow(), Ok(8));
}
#[test]
fn as_unsafe_cell() {
let c1: Cell<usize> = Cell::new(0);

View File

@@ -24,6 +24,7 @@
#![feature(step_by)]
#![feature(slice_patterns)]
#![feature(float_from_str_radix)]
#![feature(cell_extras)]
extern crate core;
extern crate test;