Removed mut-fields from Mut, removed borrow_const from Mut
This commit is contained in:
parent
00dbbd01c2
commit
ca52d08c4b
1 changed files with 7 additions and 70 deletions
|
|
@ -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<T> {
|
||||
priv mut value: T,
|
||||
priv mut mode: Mode
|
||||
priv value: T,
|
||||
priv mode: Mode
|
||||
}
|
||||
|
||||
pub type Mut<T> = Data<T>;
|
||||
|
|
@ -50,23 +50,19 @@ pub impl<T> Data<T> {
|
|||
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<R>(&self, op: &fn(t: &const T) -> R) -> R {
|
||||
op(&const self.value)
|
||||
}
|
||||
|
||||
fn borrow_imm<R>(&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| {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue