Rename the poorly named Managed<T> type to Mut<T>.

The Mut<T> type is intended to allow freezable data stuctures to be stored in
`@mut` boxes. Currently this causes borrowck to be very conserivative since it
cannot prove that you are not modifying such a structure while iterating over
it, for example.  But if you do `@Mut<T>` instead of `@mut T`, you will
effectively convert borrowck's static checks into dynamic ones.  This lets
you use the e.g. send_map just like a Java Map or something else.
This commit is contained in:
Niko Matsakis 2012-09-10 16:34:31 -07:00
parent 2026359518
commit 29003c799f
4 changed files with 31 additions and 23 deletions

View File

@ -57,7 +57,7 @@ export hash;
export cmp;
export num;
export path;
export managed;
export mutable;
export flate;
export unit;
export uniq;
@ -225,7 +225,7 @@ mod run;
mod sys;
mod unsafe;
mod managed;
mod mutable;
mod flate;

View File

@ -15,7 +15,7 @@ mutation when the data structure should be immutable.
use util::with;
use unsafe::transmute_immut;
export Managed;
export Mut;
enum Mode { ReadOnly, Mutable, Immutable }
@ -24,18 +24,26 @@ struct Data<T> {
priv mut mode: Mode
}
type Managed<T> = @Data<T>;
type Mut<T> = Data<T>;
fn Managed<T>(+t: T) -> Managed<T> {
@Data {value: t, mode: ReadOnly}
fn Mut<T>(+t: T) -> Mut<T> {
Data {value: t, mode: ReadOnly}
}
fn unwrap<T>(+m: Mut<T>) -> 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, mode: _} = m;
return move value;
}
impl<T> Data<T> {
fn borrow_mut<R>(op: &fn(t: &mut T) -> R) -> R {
match self.mode {
Immutable => fail fmt!("%? currently immutable",
self.value),
ReadOnly | Mutable => {}
Immutable => fail fmt!("%? currently immutable",
self.value),
ReadOnly | Mutable => {}
}
do with(&mut self.mode, Mutable) {
@ -64,7 +72,7 @@ impl<T> Data<T> {
#[ignore(cfg(windows))]
#[should_fail]
fn test_mut_in_imm() {
let m = Managed(1);
let m = @Mut(1);
do m.borrow_imm |_p| {
do m.borrow_mut |_q| {
// should not be permitted
@ -76,7 +84,7 @@ fn test_mut_in_imm() {
#[ignore(cfg(windows))]
#[should_fail]
fn test_imm_in_mut() {
let m = Managed(1);
let m = @Mut(1);
do m.borrow_mut |_p| {
do m.borrow_imm |_q| {
// should not be permitted
@ -86,7 +94,7 @@ fn test_imm_in_mut() {
#[test]
fn test_const_in_mut() {
let m = Managed(1);
let m = @Mut(1);
do m.borrow_mut |p| {
do m.borrow_const |q| {
assert *p == *q;
@ -98,7 +106,7 @@ fn test_const_in_mut() {
#[test]
fn test_mut_in_const() {
let m = Managed(1);
let m = @Mut(1);
do m.borrow_const |p| {
do m.borrow_mut |q| {
assert *p == *q;
@ -110,7 +118,7 @@ fn test_mut_in_const() {
#[test]
fn test_imm_in_const() {
let m = Managed(1);
let m = @Mut(1);
do m.borrow_const |p| {
do m.borrow_imm |q| {
assert *p == *q;
@ -120,7 +128,7 @@ fn test_imm_in_const() {
#[test]
fn test_const_in_imm() {
let m = Managed(1);
let m = @Mut(1);
do m.borrow_imm |p| {
do m.borrow_const |q| {
assert *p == *q;
@ -133,7 +141,7 @@ fn test_const_in_imm() {
#[ignore(cfg(windows))]
#[should_fail]
fn test_mut_in_imm_in_const() {
let m = Managed(1);
let m = @Mut(1);
do m.borrow_const |_p| {
do m.borrow_imm |_q| {
do m.borrow_mut |_r| {

View File

@ -5,7 +5,7 @@
use io::WriterUtil;
use to_str::ToStr;
use managed::Managed;
use mutable::Mut;
use send_map::linear::LinearMap;
use core::cmp::Eq;
@ -463,7 +463,7 @@ fn hash_from_uints<V: Copy>(items: &[(uint, V)]) -> hashmap<uint, V> {
}
// XXX Transitional
impl<K: Eq IterBytes Hash Copy, V: Copy> Managed<LinearMap<K, V>>:
impl<K: Eq IterBytes Hash Copy, V: Copy> @Mut<LinearMap<K, V>>:
map<K, V> {
pure fn size() -> uint {
unchecked {

View File

@ -6,7 +6,7 @@
use std;
use std::map;
use managed::Managed;
use mutable::Mut;
use send_map::linear::*;
use io::WriterUtil;
@ -166,11 +166,11 @@ fn main(args: ~[~str]) {
{
let rng = rand::seeded_rng(copy seed);
let mut results = empty_results();
int_benchmarks::<Managed<LinearMap<uint, uint>>>(
|| Managed(LinearMap()),
int_benchmarks::<@Mut<LinearMap<uint, uint>>>(
|| @Mut(LinearMap()),
rng, num_keys, &mut results);
str_benchmarks::<Managed<LinearMap<~str, uint>>>(
|| Managed(LinearMap()),
str_benchmarks::<@Mut<LinearMap<~str, uint>>>(
|| @Mut(LinearMap()),
rng, num_keys, &mut results);
write_results("libstd::map::hashmap", &results);
}