2014-06-28 22:57:36 +02:00
|
|
|
use core::cell::*;
|
2014-11-14 20:22:42 +01:00
|
|
|
use core::default::Default;
|
2014-06-28 22:57:36 +02:00
|
|
|
use std::mem::drop;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoketest_cell() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = Cell::new(10);
|
2019-03-15 12:08:07 +01:00
|
|
|
assert_eq!(x, Cell::new(10));
|
|
|
|
assert_eq!(x.get(), 10);
|
2014-06-28 22:57:36 +02:00
|
|
|
x.set(20);
|
2019-03-15 12:08:07 +01:00
|
|
|
assert_eq!(x, Cell::new(20));
|
|
|
|
assert_eq!(x.get(), 20);
|
2014-06-28 22:57:36 +02:00
|
|
|
|
2015-01-25 22:05:03 +01:00
|
|
|
let y = Cell::new((30, 40));
|
2019-03-15 12:08:07 +01:00
|
|
|
assert_eq!(y, Cell::new((30, 40)));
|
|
|
|
assert_eq!(y.get(), (30, 40));
|
2014-06-28 22:57:36 +02:00
|
|
|
}
|
|
|
|
|
2018-04-06 15:02:21 +02:00
|
|
|
#[test]
|
|
|
|
fn cell_update() {
|
|
|
|
let x = Cell::new(10);
|
|
|
|
|
|
|
|
assert_eq!(x.update(|x| x + 5), 15);
|
|
|
|
assert_eq!(x.get(), 15);
|
|
|
|
|
|
|
|
assert_eq!(x.update(|x| x / 3), 5);
|
|
|
|
assert_eq!(x.get(), 5);
|
|
|
|
}
|
|
|
|
|
2014-06-28 22:57:36 +02:00
|
|
|
#[test]
|
|
|
|
fn cell_has_sensible_show() {
|
|
|
|
let x = Cell::new("foo bar");
|
2014-12-20 09:09:35 +01:00
|
|
|
assert!(format!("{:?}", x).contains(x.get()));
|
2014-06-28 22:57:36 +02:00
|
|
|
|
|
|
|
x.set("baz qux");
|
2014-12-20 09:09:35 +01:00
|
|
|
assert!(format!("{:?}", x).contains(x.get()));
|
2014-06-28 22:57:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ref_and_refmut_have_sensible_show() {
|
|
|
|
let refcell = RefCell::new("foo");
|
|
|
|
|
|
|
|
let refcell_refmut = refcell.borrow_mut();
|
2014-12-20 09:09:35 +01:00
|
|
|
assert!(format!("{:?}", refcell_refmut).contains("foo"));
|
2014-06-28 22:57:36 +02:00
|
|
|
drop(refcell_refmut);
|
|
|
|
|
|
|
|
let refcell_ref = refcell.borrow();
|
2014-12-20 09:09:35 +01:00
|
|
|
assert!(format!("{:?}", refcell_ref).contains("foo"));
|
2014-06-28 22:57:36 +02:00
|
|
|
drop(refcell_ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double_imm_borrow() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 22:57:36 +02:00
|
|
|
let _b1 = x.borrow();
|
|
|
|
x.borrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_mut_then_imm_borrow() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 22:57:36 +02:00
|
|
|
let _b1 = x.borrow_mut();
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_err());
|
2014-06-28 22:57:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_imm_then_borrow_mut() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 22:57:36 +02:00
|
|
|
let _b1 = x.borrow();
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow_mut().is_err());
|
2014-06-28 22:57:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_double_borrow_mut() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_ok());
|
2014-06-28 22:57:36 +02:00
|
|
|
let _b1 = x.borrow_mut();
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_err());
|
2014-06-28 22:57:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn imm_release_borrow_mut() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 22:57:36 +02:00
|
|
|
{
|
|
|
|
let _b1 = x.borrow();
|
|
|
|
}
|
|
|
|
x.borrow_mut();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn mut_release_borrow_mut() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 22:57:36 +02:00
|
|
|
{
|
|
|
|
let _b1 = x.borrow_mut();
|
|
|
|
}
|
|
|
|
x.borrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn double_borrow_single_release_no_borrow_mut() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 22:57:36 +02:00
|
|
|
let _b1 = x.borrow();
|
|
|
|
{
|
|
|
|
let _b2 = x.borrow();
|
|
|
|
}
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_err());
|
2014-06-28 22:57:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-02-01 00:08:25 +01:00
|
|
|
#[should_panic]
|
2014-06-28 22:57:36 +02:00
|
|
|
fn discard_doesnt_unborrow() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 22:57:36 +02:00
|
|
|
let _b = x.borrow();
|
|
|
|
let _ = _b;
|
|
|
|
let _b = x.borrow_mut();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-05-28 22:58:04 +02:00
|
|
|
fn ref_clone_updates_flag() {
|
2015-01-25 22:05:03 +01:00
|
|
|
let x = RefCell::new(0);
|
2014-06-28 22:57:36 +02:00
|
|
|
{
|
|
|
|
let b1 = x.borrow();
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_err());
|
2014-06-28 22:57:36 +02:00
|
|
|
{
|
2015-05-28 22:58:04 +02:00
|
|
|
let _b2 = Ref::clone(&b1);
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_err());
|
2014-06-28 22:57:36 +02:00
|
|
|
}
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_err());
|
2014-06-28 22:57:36 +02:00
|
|
|
}
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_ok());
|
2014-06-28 22:57:36 +02:00
|
|
|
}
|
2014-10-21 21:59:21 +02:00
|
|
|
|
2015-05-28 23:00:52 +02:00
|
|
|
#[test]
|
|
|
|
fn ref_map_does_not_update_flag() {
|
|
|
|
let x = RefCell::new(Some(5));
|
|
|
|
{
|
2019-04-20 18:44:29 +02:00
|
|
|
let b1: Ref<'_, Option<u32>> = x.borrow();
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_err());
|
2015-05-28 23:00:52 +02:00
|
|
|
{
|
2019-04-20 18:44:29 +02:00
|
|
|
let b2: Ref<'_, u32> = Ref::map(b1, |o| o.as_ref().unwrap());
|
2015-05-28 23:00:52 +02:00
|
|
|
assert_eq!(*b2, 5);
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_err());
|
2015-05-28 23:00:52 +02:00
|
|
|
}
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_ok());
|
2015-05-28 23:00:52 +02:00
|
|
|
}
|
2016-12-14 22:02:00 +01:00
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_ok());
|
2015-05-28 23:00:52 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 07:32:05 +02:00
|
|
|
#[test]
|
|
|
|
fn ref_map_split_updates_flag() {
|
|
|
|
let x = RefCell::new([1, 2]);
|
|
|
|
{
|
|
|
|
let b1 = x.borrow();
|
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_err());
|
|
|
|
{
|
|
|
|
let (_b2, _b3) = Ref::map_split(b1, |slc| slc.split_at(1));
|
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_err());
|
|
|
|
}
|
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_ok());
|
|
|
|
}
|
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_ok());
|
|
|
|
|
|
|
|
{
|
|
|
|
let b1 = x.borrow_mut();
|
|
|
|
assert!(x.try_borrow().is_err());
|
|
|
|
assert!(x.try_borrow_mut().is_err());
|
|
|
|
{
|
|
|
|
let (_b2, _b3) = RefMut::map_split(b1, |slc| slc.split_at_mut(1));
|
|
|
|
assert!(x.try_borrow().is_err());
|
|
|
|
assert!(x.try_borrow_mut().is_err());
|
|
|
|
drop(_b2);
|
|
|
|
assert!(x.try_borrow().is_err());
|
|
|
|
assert!(x.try_borrow_mut().is_err());
|
|
|
|
}
|
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_ok());
|
|
|
|
}
|
|
|
|
assert!(x.try_borrow().is_ok());
|
|
|
|
assert!(x.try_borrow_mut().is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ref_map_split() {
|
|
|
|
let x = RefCell::new([1, 2]);
|
|
|
|
let (b1, b2) = Ref::map_split(x.borrow(), |slc| slc.split_at(1));
|
|
|
|
assert_eq!(*b1, [1]);
|
|
|
|
assert_eq!(*b2, [2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ref_mut_map_split() {
|
|
|
|
let x = RefCell::new([1, 2]);
|
|
|
|
{
|
|
|
|
let (mut b1, mut b2) = RefMut::map_split(x.borrow_mut(), |slc| slc.split_at_mut(1));
|
|
|
|
assert_eq!(*b1, [1]);
|
|
|
|
assert_eq!(*b2, [2]);
|
|
|
|
b1[0] = 2;
|
|
|
|
b2[0] = 1;
|
|
|
|
}
|
|
|
|
assert_eq!(*x.borrow(), [2, 1]);
|
|
|
|
}
|
|
|
|
|
2015-05-28 23:00:52 +02:00
|
|
|
#[test]
|
|
|
|
fn ref_map_accessor() {
|
|
|
|
struct X(RefCell<(u32, char)>);
|
|
|
|
impl X {
|
2019-04-20 18:44:29 +02:00
|
|
|
fn accessor(&self) -> Ref<'_, u32> {
|
2015-05-28 23:00:52 +02:00
|
|
|
Ref::map(self.0.borrow(), |tuple| &tuple.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let x = X(RefCell::new((7, 'z')));
|
2019-04-20 18:44:29 +02:00
|
|
|
let d: Ref<'_, u32> = x.accessor();
|
2015-05-28 23:00:52 +02:00
|
|
|
assert_eq!(*d, 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ref_mut_map_accessor() {
|
|
|
|
struct X(RefCell<(u32, char)>);
|
|
|
|
impl X {
|
2019-04-20 18:44:29 +02:00
|
|
|
fn accessor(&self) -> RefMut<'_, u32> {
|
2015-05-28 23:00:52 +02:00
|
|
|
RefMut::map(self.0.borrow_mut(), |tuple| &mut tuple.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let x = X(RefCell::new((7, 'z')));
|
|
|
|
{
|
2019-12-07 05:18:12 +01:00
|
|
|
let mut d: RefMut<'_, u32> = x.accessor();
|
2015-05-28 23:00:52 +02:00
|
|
|
assert_eq!(*d, 7);
|
|
|
|
*d += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(*x.0.borrow(), (8, 'z'));
|
|
|
|
}
|
|
|
|
|
2014-10-21 21:59:21 +02:00
|
|
|
#[test]
|
2016-08-11 23:08:24 +02:00
|
|
|
fn as_ptr() {
|
2015-03-26 01:06:52 +01:00
|
|
|
let c1: Cell<usize> = Cell::new(0);
|
2015-02-03 14:50:52 +01:00
|
|
|
c1.set(1);
|
2016-08-11 23:08:24 +02:00
|
|
|
assert_eq!(1, unsafe { *c1.as_ptr() });
|
2014-10-21 21:59:21 +02:00
|
|
|
|
2015-03-26 01:06:52 +01:00
|
|
|
let c2: Cell<usize> = Cell::new(0);
|
2019-12-07 05:18:12 +01:00
|
|
|
unsafe {
|
|
|
|
*c2.as_ptr() = 1;
|
|
|
|
}
|
2015-02-03 14:50:52 +01:00
|
|
|
assert_eq!(1, c2.get());
|
2014-10-21 21:59:21 +02:00
|
|
|
|
2015-03-26 01:06:52 +01:00
|
|
|
let r1: RefCell<usize> = RefCell::new(0);
|
2015-02-03 14:50:52 +01:00
|
|
|
*r1.borrow_mut() = 1;
|
2016-08-11 23:08:24 +02:00
|
|
|
assert_eq!(1, unsafe { *r1.as_ptr() });
|
2014-10-21 21:59:21 +02:00
|
|
|
|
2015-03-26 01:06:52 +01:00
|
|
|
let r2: RefCell<usize> = RefCell::new(0);
|
2019-12-07 05:18:12 +01:00
|
|
|
unsafe {
|
|
|
|
*r2.as_ptr() = 1;
|
|
|
|
}
|
2015-02-03 14:50:52 +01:00
|
|
|
assert_eq!(1, *r2.borrow());
|
2014-10-21 21:59:21 +02:00
|
|
|
}
|
2014-11-14 20:22:42 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_default() {
|
|
|
|
let cell: Cell<u32> = Default::default();
|
|
|
|
assert_eq!(0, cell.get());
|
|
|
|
}
|
|
|
|
|
2017-01-25 04:44:33 +01:00
|
|
|
#[test]
|
|
|
|
fn cell_set() {
|
|
|
|
let cell = Cell::new(10);
|
|
|
|
cell.set(20);
|
|
|
|
assert_eq!(20, cell.get());
|
|
|
|
|
|
|
|
let cell = Cell::new("Hello".to_owned());
|
|
|
|
cell.set("World".to_owned());
|
|
|
|
assert_eq!("World".to_owned(), cell.into_inner());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_replace() {
|
|
|
|
let cell = Cell::new(10);
|
|
|
|
assert_eq!(10, cell.replace(20));
|
|
|
|
assert_eq!(20, cell.get());
|
|
|
|
|
|
|
|
let cell = Cell::new("Hello".to_owned());
|
|
|
|
assert_eq!("Hello".to_owned(), cell.replace("World".to_owned()));
|
|
|
|
assert_eq!("World".to_owned(), cell.into_inner());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_into_inner() {
|
|
|
|
let cell = Cell::new(10);
|
|
|
|
assert_eq!(10, cell.into_inner());
|
|
|
|
|
|
|
|
let cell = Cell::new("Hello world".to_owned());
|
|
|
|
assert_eq!("Hello world".to_owned(), cell.into_inner());
|
|
|
|
}
|
|
|
|
|
2014-11-14 20:22:42 +01:00
|
|
|
#[test]
|
|
|
|
fn refcell_default() {
|
|
|
|
let cell: RefCell<u64> = Default::default();
|
|
|
|
assert_eq!(0, *cell.borrow());
|
|
|
|
}
|
2015-04-23 12:53:54 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unsafe_cell_unsized() {
|
|
|
|
let cell: &UnsafeCell<[i32]> = &UnsafeCell::new([1, 2, 3]);
|
|
|
|
{
|
|
|
|
let val: &mut [i32] = unsafe { &mut *cell.get() };
|
|
|
|
val[0] = 4;
|
|
|
|
val[2] = 5;
|
|
|
|
}
|
|
|
|
let comp: &mut [i32] = &mut [4, 2, 5];
|
|
|
|
assert_eq!(unsafe { &mut *cell.get() }, comp);
|
|
|
|
}
|
|
|
|
|
2015-09-20 12:35:08 +02:00
|
|
|
#[test]
|
|
|
|
fn refcell_unsized() {
|
|
|
|
let cell: &RefCell<[i32]> = &RefCell::new([1, 2, 3]);
|
|
|
|
{
|
|
|
|
let b = &mut *cell.borrow_mut();
|
|
|
|
b[0] = 4;
|
|
|
|
b[2] = 5;
|
|
|
|
}
|
|
|
|
let comp: &mut [i32] = &mut [4, 2, 5];
|
|
|
|
assert_eq!(&*cell.borrow(), comp);
|
|
|
|
}
|
2016-03-31 16:11:59 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn refcell_ref_coercion() {
|
|
|
|
let cell: RefCell<[i32; 3]> = RefCell::new([1, 2, 3]);
|
|
|
|
{
|
2019-04-20 18:44:29 +02:00
|
|
|
let mut cellref: RefMut<'_, [i32; 3]> = cell.borrow_mut();
|
2016-03-31 16:11:59 +02:00
|
|
|
cellref[0] = 4;
|
2019-04-20 18:44:29 +02:00
|
|
|
let mut coerced: RefMut<'_, [i32]> = cellref;
|
2016-03-31 16:11:59 +02:00
|
|
|
coerced[2] = 5;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let comp: &mut [i32] = &mut [4, 2, 5];
|
2019-04-20 18:44:29 +02:00
|
|
|
let cellref: Ref<'_, [i32; 3]> = cell.borrow();
|
2016-03-31 16:11:59 +02:00
|
|
|
assert_eq!(&*cellref, comp);
|
2019-04-20 18:44:29 +02:00
|
|
|
let coerced: Ref<'_, [i32]> = cellref;
|
2016-03-31 16:11:59 +02:00
|
|
|
assert_eq!(&*coerced, comp);
|
|
|
|
}
|
|
|
|
}
|
2017-07-31 19:14:16 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn refcell_swap_borrows() {
|
|
|
|
let x = RefCell::new(0);
|
|
|
|
let _b = x.borrow();
|
|
|
|
let y = RefCell::new(1);
|
|
|
|
x.swap(&y);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn refcell_replace_borrows() {
|
|
|
|
let x = RefCell::new(0);
|
|
|
|
let _b = x.borrow();
|
|
|
|
x.replace(1);
|
|
|
|
}
|