From ca52d08c4b62a8e501387d305eea1ccccaeb3984 Mon Sep 17 00:00:00 2001 From: Matthijs Hofstra Date: Thu, 28 Mar 2013 17:21:50 +0100 Subject: [PATCH 1/3] Removed mut-fields from Mut, removed borrow_const from Mut --- src/libcore/mutable.rs | 77 ++++-------------------------------------- 1 file changed, 7 insertions(+), 70 deletions(-) diff --git a/src/libcore/mutable.rs b/src/libcore/mutable.rs index e9639820e836b..39d184b951a33 100644 --- a/src/libcore/mutable.rs +++ b/src/libcore/mutable.rs @@ -20,13 +20,13 @@ mutation when the data structure should be immutable. */ use util::with; -use cast::transmute_immut; +use cast::{transmute_mut,transmute_immut}; enum Mode { ReadOnly, Mutable, Immutable } struct Data { - priv mut value: T, - priv mut mode: Mode + priv value: T, + priv mode: Mode } pub type Mut = Data; @@ -50,23 +50,19 @@ pub impl Data { ReadOnly | Mutable => {} } - do with(&mut self.mode, Mutable) { - op(&mut self.value) + do with( unsafe { transmute_mut(&self.mode) }, Mutable) { + op( unsafe { transmute_mut(&self.value) }) } } - fn borrow_const(&self, op: &fn(t: &const T) -> R) -> R { - op(&const self.value) - } - fn borrow_imm(&self, op: &fn(t: &T) -> R) -> R { match self.mode { Mutable => fail!(~"currently mutable"), ReadOnly | Immutable => {} } - do with(&mut self.mode, Immutable) { - op(unsafe{transmute_immut(&mut self.value)}) + do with( unsafe { transmute_mut(&self.mode) }, Immutable) { + op( &self.value ) } } @@ -97,62 +93,3 @@ pub fn test_imm_in_mut() { } } } - -#[test] -pub fn test_const_in_mut() { - let m = @Mut(1); - do m.borrow_mut |p| { - do m.borrow_const |q| { - assert!(*p == *q); - *p += 1; - assert!(*p == *q); - } - } -} - -#[test] -pub fn test_mut_in_const() { - let m = @Mut(1); - do m.borrow_const |p| { - do m.borrow_mut |q| { - assert!(*p == *q); - *q += 1; - assert!(*p == *q); - } - } -} - -#[test] -pub fn test_imm_in_const() { - let m = @Mut(1); - do m.borrow_const |p| { - do m.borrow_imm |q| { - assert!(*p == *q); - } - } -} - -#[test] -pub fn test_const_in_imm() { - let m = @Mut(1); - do m.borrow_imm |p| { - do m.borrow_const |q| { - assert!(*p == *q); - } - } -} - - -#[test] -#[ignore(cfg(windows))] -#[should_fail] -pub fn test_mut_in_imm_in_const() { - let m = @Mut(1); - do m.borrow_const |_p| { - do m.borrow_imm |_q| { - do m.borrow_mut |_r| { - } - } - } -} - From f2bc28c24f6850411db97f8256fc63cc15939929 Mon Sep 17 00:00:00 2001 From: Matthijs Hofstra Date: Thu, 28 Mar 2013 20:13:38 +0100 Subject: [PATCH 2/3] Removed all uses of Mut from workcache, replaced with @mut --- src/libstd/workcache.rs | 132 ++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 72 deletions(-) diff --git a/src/libstd/workcache.rs b/src/libstd/workcache.rs index 90f10ea50541d..6ed1364d7fcb2 100644 --- a/src/libstd/workcache.rs +++ b/src/libstd/workcache.rs @@ -27,7 +27,6 @@ use core::run; use core::hashmap::linear::LinearMap; use core::task; use core::to_bytes; -use core::mutable::Mut; /** * @@ -168,7 +167,7 @@ impl Decodable for WorkMap { struct Database { db_filename: Path, db_cache: LinearMap<~str, ~str>, - mut db_dirty: bool + db_dirty: bool } pub impl Database { @@ -210,8 +209,8 @@ pub impl Logger { } struct Context { - db: @Mut, - logger: @Mut, + db: @mut Database, + logger: @mut Logger, cfg: @json::Object, freshness: LinearMap<~str,@fn(&str,&str)->bool> } @@ -228,7 +227,7 @@ struct Exec { } struct Work { - prep: @Mut, + prep: @mut Prep, res: Option>> } @@ -261,8 +260,8 @@ fn digest_file(path: &Path) -> ~str { pub impl Context { - fn new(db: @Mut, - lg: @Mut, + fn new(db: @mut Database, + lg: @mut Logger, cfg: @json::Object) -> Context { Context { db: db, @@ -277,19 +276,19 @@ pub impl Context { Decodable>( // FIXME(#5121) @self, fn_name:&str, - blk: &fn(@Mut)->Work) -> Work { - let p = @Mut(Prep { + blk: &fn(@mut Prep)->Work) -> Work { + let p = @mut Prep { ctxt: self, fn_name: fn_name.to_owned(), declared_inputs: WorkMap::new() - }); + }; blk(p) } } trait TPrep { - fn declare_input(&self, kind:&str, name:&str, val:&str); + fn declare_input(&mut self, kind:&str, name:&str, val:&str); fn is_fresh(&self, cat:&str, kind:&str, name:&str, val:&str) -> bool; fn all_fresh(&self, cat:&str, map:&WorkMap) -> bool; fn exec T) -> Work; } -impl TPrep for @Mut { - fn declare_input(&self, kind:&str, name:&str, val:&str) { - do self.borrow_mut |p| { - p.declared_inputs.insert(WorkKey::new(kind, name), - val.to_owned()); - } +impl TPrep for Prep { + fn declare_input(&mut self, kind:&str, name:&str, val:&str) { + self.declared_inputs.insert(WorkKey::new(kind, name), + val.to_owned()); } fn is_fresh(&self, cat: &str, kind: &str, name: &str, val: &str) -> bool { - do self.borrow_imm |p| { - let k = kind.to_owned(); - let f = (*p.ctxt.freshness.get(&k))(name, val); - do p.ctxt.logger.borrow_imm |lg| { - if f { - lg.info(fmt!("%s %s:%s is fresh", - cat, kind, name)); - } else { - lg.info(fmt!("%s %s:%s is not fresh", - cat, kind, name)) - } + let k = kind.to_owned(); + let f = (*self.ctxt.freshness.get(&k))(name, val); + let lg = self.ctxt.logger; + if f { + lg.info(fmt!("%s %s:%s is fresh", + cat, kind, name)); + } else { + lg.info(fmt!("%s %s:%s is not fresh", + cat, kind, name)) } - f - } + f } fn all_fresh(&self, cat: &str, map: &WorkMap) -> bool { @@ -339,38 +333,34 @@ impl TPrep for @Mut { &self, blk: ~fn(&Exec) -> T) -> Work { let mut bo = Some(blk); - do self.borrow_imm |p| { - let cached = do p.ctxt.db.borrow_mut |db| { - db.prepare(p.fn_name, &p.declared_inputs) - }; + let cached = self.ctxt.db.prepare(self.fn_name, &self.declared_inputs); - match cached { - Some((ref disc_in, ref disc_out, ref res)) - if self.all_fresh("declared input", - &p.declared_inputs) && - self.all_fresh("discovered input", disc_in) && - self.all_fresh("discovered output", disc_out) => { - Work::new(*self, Left(json_decode(*res))) - } + match cached { + Some((ref disc_in, ref disc_out, ref res)) + if self.all_fresh("declared input", + &self.declared_inputs) && + self.all_fresh("discovered input", disc_in) && + self.all_fresh("discovered output", disc_out) => { + Work::new(@mut *self, Left(json_decode(*res))) + } - _ => { - let (chan, port) = oneshot::init(); - let mut blk = None; - blk <-> bo; - let blk = blk.unwrap(); - let chan = Cell(chan); - do task::spawn || { - let exe = Exec { - discovered_inputs: WorkMap::new(), - discovered_outputs: WorkMap::new(), - }; - let chan = chan.take(); - let v = blk(&exe); - send_one(chan, (exe, v)); - } - - Work::new(*self, Right(port)) + _ => { + let (chan, port) = oneshot::init(); + let mut blk = None; + blk <-> bo; + let blk = blk.unwrap(); + let chan = Cell(chan); + + do task::spawn || { + let exe = Exec { + discovered_inputs: WorkMap::new(), + discovered_outputs: WorkMap::new(), + }; + let chan = chan.take(); + let v = blk(&exe); + send_one(chan, (exe, v)); } + Work::new(@mut *self, Right(port)) } } } @@ -379,7 +369,7 @@ impl TPrep for @Mut { pub impl + Decodable> Work { // FIXME(#5121) - fn new(p: @Mut, e: Either>) -> Work { + fn new(p: @mut Prep, e: Either>) -> Work { Work { prep: p, res: Some(e) } } } @@ -404,15 +394,13 @@ fn unwrap = do cx.prep("test1") |prep| { From f43e6af38d742b9b6d4c697e48ea239d7ef154c2 Mon Sep 17 00:00:00 2001 From: Matthijs Hofstra Date: Thu, 28 Mar 2013 20:15:14 +0100 Subject: [PATCH 3/3] Removed libcore/mutable.rs, Mut is now dead. --- src/libcore/core.rc | 1 - src/libcore/mutable.rs | 95 ------------------------------------------ 2 files changed, 96 deletions(-) delete mode 100644 src/libcore/mutable.rs diff --git a/src/libcore/core.rc b/src/libcore/core.rc index 292dd1d92cfa3..f7c64e3f37445 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -212,7 +212,6 @@ pub mod rand; pub mod run; pub mod sys; pub mod cast; -pub mod mutable; pub mod flate; pub mod repr; pub mod cleanup; diff --git a/src/libcore/mutable.rs b/src/libcore/mutable.rs deleted file mode 100644 index 39d184b951a33..0000000000000 --- a/src/libcore/mutable.rs +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -/*! - -Module for wrapping freezable data structures in managed boxes. -Normally freezable data structures require an unaliased reference, -such as `T` or `~T`, so that the compiler can track when they are -being mutated. The `managed` type converts these static checks into -dynamic checks: your program will fail if you attempt to perform -mutation when the data structure should be immutable. - -*/ - -use util::with; -use cast::{transmute_mut,transmute_immut}; - -enum Mode { ReadOnly, Mutable, Immutable } - -struct Data { - priv value: T, - priv mode: Mode -} - -pub type Mut = Data; - -pub fn Mut(t: T) -> Mut { - Data {value: t, mode: ReadOnly} -} - -pub fn unwrap(m: Mut) -> T { - // Borrowck should prevent us from calling unwrap while the value - // is in use, as that would be a move from a borrowed value. - assert!((m.mode as uint) == (ReadOnly as uint)); - let Data {value: value, mode: _} = m; - value -} - -pub impl Data { - fn borrow_mut(&self, op: &fn(t: &mut T) -> R) -> R { - match self.mode { - Immutable => fail!(~"currently immutable"), - ReadOnly | Mutable => {} - } - - do with( unsafe { transmute_mut(&self.mode) }, Mutable) { - op( unsafe { transmute_mut(&self.value) }) - } - } - - fn borrow_imm(&self, op: &fn(t: &T) -> R) -> R { - match self.mode { - Mutable => fail!(~"currently mutable"), - ReadOnly | Immutable => {} - } - - do with( unsafe { transmute_mut(&self.mode) }, Immutable) { - op( &self.value ) - } - } - - #[inline(always)] - fn unwrap(self) -> T { unwrap(self) } -} - -#[test] -#[ignore(cfg(windows))] -#[should_fail] -pub fn test_mut_in_imm() { - let m = @Mut(1); - do m.borrow_imm |_p| { - do m.borrow_mut |_q| { - // should not be permitted - } - } -} - -#[test] -#[ignore(cfg(windows))] -#[should_fail] -pub fn test_imm_in_mut() { - let m = @Mut(1); - do m.borrow_mut |_p| { - do m.borrow_imm |_q| { - // should not be permitted - } - } -}