From 181875ca50853418abaa525614252cf46bfce10f Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Thu, 20 Mar 2014 19:55:52 -0700 Subject: [PATCH 1/2] Remove RefCell::{with, with_mut} These are superfluous now that we have fixed rvalue lifetimes and Deref. --- src/librustc/driver/driver.rs | 4 +-- src/librustc/metadata/cstore.rs | 10 +++--- src/libstd/cell.rs | 59 --------------------------------- src/libstd/gc.rs | 6 ++-- src/libstd/rc.rs | 6 ++-- 5 files changed, 11 insertions(+), 74 deletions(-) diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index ae9cd37fe6928..4676ada1727eb 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -313,10 +313,10 @@ pub fn phase_3_run_analysis_passes(sess: Session, time(time_passes, "looking for entry point", (), |_| middle::entry::find_entry_point(&sess, krate, &ast_map)); - sess.macro_registrar_fn.with_mut(|r| *r = + *sess.macro_registrar_fn.borrow_mut() = time(time_passes, "looking for macro registrar", (), |_| syntax::ext::registrar::find_macro_registrar( - sess.diagnostic(), krate))); + sess.diagnostic(), krate)); let freevars = time(time_passes, "freevar finding", (), |_| freevars::annotate_freevars(def_map, krate)); diff --git a/src/librustc/metadata/cstore.rs b/src/librustc/metadata/cstore.rs index c1d3ad76260ae..f1089891ea5fb 100644 --- a/src/librustc/metadata/cstore.rs +++ b/src/librustc/metadata/cstore.rs @@ -135,11 +135,11 @@ impl CStore { } pub fn reset(&self) { - self.metas.with_mut(|s| s.clear()); - self.extern_mod_crate_map.with_mut(|s| s.clear()); - self.used_crate_sources.with_mut(|s| s.clear()); - self.used_libraries.with_mut(|s| s.clear()); - self.used_link_args.with_mut(|s| s.clear()); + self.metas.borrow_mut().clear(); + self.extern_mod_crate_map.borrow_mut().clear(); + self.used_crate_sources.borrow_mut().clear(); + self.used_libraries.borrow_mut().clear(); + self.used_link_args.borrow_mut().clear(); } // This method is used when generating the command line to pass through to diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index 5733504c0d189..df1c29ded6e0e 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -173,28 +173,6 @@ impl RefCell { } } - /// Immutably borrows the wrapped value and applies `blk` to it. - /// - /// # Failure - /// - /// Fails if the value is currently mutably borrowed. - #[inline] - pub fn with(&self, blk: |&T| -> U) -> U { - let ptr = self.borrow(); - blk(ptr.get()) - } - - /// Mutably borrows the wrapped value and applies `blk` to it. - /// - /// # Failure - /// - /// Fails if the value is currently borrowed. - #[inline] - pub fn with_mut(&self, blk: |&mut T| -> U) -> U { - let mut ptr = self.borrow_mut(); - blk(ptr.get()) - } - /// Sets the value, replacing what was there. /// /// # Failure @@ -372,43 +350,6 @@ mod test { assert!(x.try_borrow_mut().is_none()); } - #[test] - fn with_ok() { - let x = RefCell::new(0); - assert_eq!(1, x.with(|x| *x+1)); - } - - #[test] - #[should_fail] - fn mut_borrow_with() { - let x = RefCell::new(0); - let _b1 = x.borrow_mut(); - x.with(|x| *x+1); - } - - #[test] - fn borrow_with() { - let x = RefCell::new(0); - let _b1 = x.borrow(); - assert_eq!(1, x.with(|x| *x+1)); - } - - #[test] - fn with_mut_ok() { - let x = RefCell::new(0); - x.with_mut(|x| *x += 1); - let b = x.borrow(); - assert_eq!(1, *b.get()); - } - - #[test] - #[should_fail] - fn borrow_with_mut() { - let x = RefCell::new(0); - let _b = x.borrow(); - x.with_mut(|x| *x += 1); - } - #[test] #[should_fail] fn discard_doesnt_unborrow() { diff --git a/src/libstd/gc.rs b/src/libstd/gc.rs index 907a2d21b695d..7fb23d77f3c6c 100644 --- a/src/libstd/gc.rs +++ b/src/libstd/gc.rs @@ -87,10 +87,8 @@ mod tests { fn test_clone() { let x = Gc::new(RefCell::new(5)); let y = x.clone(); - x.borrow().with_mut(|inner| { - *inner = 20; - }); - assert_eq!(y.borrow().with(|x| *x), 20); + *x.borrow().borrow_mut() = 20; + assert_eq!(*y.borrow().borrow(), 20); } #[test] diff --git a/src/libstd/rc.rs b/src/libstd/rc.rs index 605cbd3f28a12..c41e3b01f4732 100644 --- a/src/libstd/rc.rs +++ b/src/libstd/rc.rs @@ -198,10 +198,8 @@ mod tests { fn test_clone() { let x = Rc::new(RefCell::new(5)); let y = x.clone(); - x.deref().with_mut(|inner| { - *inner = 20; - }); - assert_eq!(y.deref().with(|v| *v), 20); + *x.borrow_mut() = 20; + assert_eq!(*y.borrow(), 20); } #[test] From 1d98fe12a8b2ad954f01935552d23643e96a53af Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Thu, 20 Mar 2014 20:24:31 -0700 Subject: [PATCH 2/2] Clean up marker types and Unsafe initialization --- src/libstd/cell.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index df1c29ded6e0e..b54396efec505 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -22,19 +22,17 @@ use ty::Unsafe; /// A mutable memory location that admits only `Pod` data. pub struct Cell { priv value: Unsafe, - priv marker1: marker::InvariantType, - priv marker2: marker::NoFreeze, - priv marker3: marker::NoShare, + priv marker1: marker::NoFreeze, + priv marker2: marker::NoShare, } impl Cell { /// Creates a new `Cell` containing the given value. pub fn new(value: T) -> Cell { Cell { - value: Unsafe{value: value, marker1: marker::InvariantType::}, - marker1: marker::InvariantType::, - marker2: marker::NoFreeze, - marker3: marker::NoShare, + value: Unsafe::new(value), + marker1: marker::NoFreeze, + marker2: marker::NoShare, } } @@ -75,10 +73,9 @@ impl fmt::Show for Cell { pub struct RefCell { priv value: Unsafe, priv borrow: BorrowFlag, - priv marker1: marker::InvariantType, - priv marker2: marker::NoFreeze, - priv marker3: marker::NoPod, - priv marker4: marker::NoShare, + priv marker1: marker::NoFreeze, + priv marker2: marker::NoPod, + priv marker3: marker::NoShare, } // Values [1, MAX-1] represent the number of `Ref` active @@ -91,11 +88,10 @@ impl RefCell { /// Create a new `RefCell` containing `value` pub fn new(value: T) -> RefCell { RefCell { - marker1: marker::InvariantType::, - marker2: marker::NoFreeze, - marker3: marker::NoPod, - marker4: marker::NoShare, - value: Unsafe{value: value, marker1: marker::InvariantType::}, + marker1: marker::NoFreeze, + marker2: marker::NoPod, + marker3: marker::NoShare, + value: Unsafe::new(value), borrow: UNUSED, } }