Skip to content

Commit bbdb92c

Browse files
committed
---
yaml --- r: 23361 b: refs/heads/master c: bc5eb95 h: refs/heads/master i: 23359: f2176e0 v: v3
1 parent 3ff9e1a commit bbdb92c

File tree

8 files changed

+368
-11
lines changed

8 files changed

+368
-11
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 182814ef8191e2b153806380f8d3d46069c69a8d
2+
refs/heads/master: bc5eb95222260c3c85db659de389ce207acb594c
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be

trunk/src/libcore/core.rc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export hash;
5555
export cmp;
5656
export num;
5757
export path;
58+
export managed;
5859

5960
// NDM seems to be necessary for resolve to work
6061
export option_iter;
@@ -261,6 +262,7 @@ mod sys;
261262
#[warn(non_camel_case_types)]
262263
mod unsafe;
263264

265+
mod managed;
264266

265267
// Modules supporting compiler-generated code
266268
// Exported but not part of the public interface

trunk/src/libcore/managed.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*!
2+
3+
Module for wrapping freezable data structures in managed boxes.
4+
Normally freezable data structures require an unaliased reference,
5+
such as `T` or `~T`, so that the compiler can track when they are
6+
being mutated. The `rw<T>` type converts these static checks into
7+
dynamic checks: your program will fail if you attempt to perform
8+
mutation when the data structure should be immutable.
9+
10+
*/
11+
12+
#[forbid(non_camel_case_types)];
13+
#[forbid(deprecated_mode)];
14+
#[forbid(deprecated_pattern)];
15+
16+
import util::with;
17+
import unsafe::transmute_immut;
18+
19+
export Managed;
20+
21+
enum Mode { ReadOnly, Mutable, Immutable }
22+
23+
struct Data<T> {
24+
mut value: T;
25+
mut mode: Mode;
26+
}
27+
28+
type Managed<T> = @Data<T>;
29+
30+
fn Managed<T>(+t: T) -> Managed<T> {
31+
@Data {value: t, mode: ReadOnly}
32+
}
33+
34+
impl<T> Data<T> {
35+
fn borrow_mut<R>(op: &fn(t: &mut T) -> R) -> R {
36+
match self.mode {
37+
Immutable => fail fmt!("%? currently immutable",
38+
self.value),
39+
ReadOnly | Mutable => {}
40+
}
41+
42+
do with(&mut self.mode, Mutable) {
43+
op(&mut self.value)
44+
}
45+
}
46+
47+
fn borrow_const<R>(op: &fn(t: &const T) -> R) -> R {
48+
op(&const self.value)
49+
}
50+
51+
fn borrow_imm<R>(op: &fn(t: &T) -> R) -> R {
52+
match self.mode {
53+
Mutable => fail fmt!("%? currently mutable",
54+
self.value),
55+
ReadOnly | Immutable => {}
56+
}
57+
58+
do with(&mut self.mode, Immutable) {
59+
op(unsafe{transmute_immut(&mut self.value)})
60+
}
61+
}
62+
}

trunk/src/libcore/send_map.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ mod linear {
221221
self.size -= 1;
222222
return true;
223223
}
224+
225+
fn clear() {
226+
for uint::range(0, self.buckets.len()) |idx| {
227+
self.buckets[idx] = none;
228+
}
229+
self.size = 0;
230+
}
224231
}
225232

226233
priv impl<K,V> &LinearMap<K,V> {

trunk/src/libcore/util.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ pure fn id<T>(+x: T) -> T { x }
1212
/// Ignores a value.
1313
pure fn ignore<T>(+_x: T) { }
1414

15+
/// Sets `*ptr` to `new_value`, invokes `op()`, and then restores the
16+
/// original value of `*ptr`.
17+
#[inline(always)]
18+
fn with<T: copy, R>(
19+
ptr: &mut T,
20+
+new_value: T,
21+
op: &fn() -> R) -> R
22+
{
23+
// NDM: if swap operator were defined somewhat differently,
24+
// we wouldn't need to copy...
25+
26+
let old_value = *ptr;
27+
*ptr = move new_value;
28+
let result = op();
29+
*ptr = move old_value;
30+
return move result;
31+
}
32+
1533
/**
1634
* Swap the values at two mutable locations of the same type, without
1735
* deinitialising or copying either one.

trunk/src/libstd/map.rs

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import io::WriterUtil;
66
import to_str::ToStr;
7+
import managed::Managed;
8+
import send_map::linear::LinearMap;
9+
710
export hashmap, hashfn, eqfn, set, map, chained, hashmap, str_hash;
811
export box_str_hash;
912
export bytes_hash, int_hash, uint_hash, set_add;
@@ -59,10 +62,10 @@ trait map<K: copy, V: copy> {
5962
fn find(+key: K) -> option<V>;
6063

6164
/**
62-
* Remove and return a value from the map. If the key does not exist
63-
* in the map then returns none.
65+
* Remove and return a value from the map. Returns true if the
66+
* key was present in the map, otherwise false.
6467
*/
65-
fn remove(+key: K) -> option<V>;
68+
fn remove(+key: K) -> bool;
6669

6770
/// Clear the map, removing all key/value pairs.
6871
fn clear();
@@ -279,18 +282,18 @@ mod chained {
279282
option::unwrap(opt_v)
280283
}
281284

282-
fn remove(+k: K) -> option<V> {
285+
fn remove(+k: K) -> bool {
283286
match self.search_tbl(&k, self.hasher(&k)) {
284-
not_found => none,
287+
not_found => false,
285288
found_first(idx, entry) => {
286289
self.count -= 1u;
287290
self.chains[idx] = entry.next;
288-
some(entry.value)
291+
true
289292
}
290293
found_after(eprev, entry) => {
291294
self.count -= 1u;
292295
eprev.next = entry.next;
293-
some(entry.value)
296+
true
294297
}
295298
}
296299
}
@@ -468,6 +471,93 @@ fn hash_from_uints<V: copy>(items: &[(uint, V)]) -> hashmap<uint, V> {
468471
hash_from_vec(uint::hash, uint::eq, items)
469472
}
470473

474+
// XXX Transitionary
475+
impl<K: copy, V: copy> Managed<LinearMap<K, V>>: map<K, V> {
476+
fn size() -> uint {
477+
do self.borrow_const |p| {
478+
p.len()
479+
}
480+
}
481+
482+
fn insert(+key: K, +value: V) -> bool {
483+
do self.borrow_mut |p| {
484+
p.insert(key, value)
485+
}
486+
}
487+
488+
fn contains_key(+key: K) -> bool {
489+
do self.borrow_const |p| {
490+
p.contains_key(&key)
491+
}
492+
}
493+
494+
fn contains_key_ref(key: &K) -> bool {
495+
do self.borrow_const |p| {
496+
p.contains_key(key)
497+
}
498+
}
499+
500+
fn get(+key: K) -> V {
501+
do self.borrow_const |p| {
502+
p.get(&key)
503+
}
504+
}
505+
506+
fn find(+key: K) -> option<V> {
507+
do self.borrow_const |p| {
508+
p.find(&key)
509+
}
510+
}
511+
512+
fn remove(+key: K) -> bool {
513+
do self.borrow_mut |p| {
514+
p.remove(&key)
515+
}
516+
}
517+
518+
fn clear() {
519+
do self.borrow_mut |p| {
520+
p.clear()
521+
}
522+
}
523+
524+
fn each(op: fn(+key: K, +value: V) -> bool) {
525+
do self.borrow_imm |p| {
526+
p.each(op)
527+
}
528+
}
529+
530+
fn each_key(op: fn(+key: K) -> bool) {
531+
do self.borrow_imm |p| {
532+
p.each_key(op)
533+
}
534+
}
535+
536+
fn each_value(op: fn(+value: V) -> bool) {
537+
do self.borrow_imm |p| {
538+
p.each_value(op)
539+
}
540+
}
541+
542+
fn each_ref(op: fn(key: &K, value: &V) -> bool) {
543+
do self.borrow_imm |p| {
544+
p.each_ref(op)
545+
}
546+
}
547+
548+
fn each_key_ref(op: fn(key: &K) -> bool) {
549+
do self.borrow_imm |p| {
550+
p.each_key_ref(op)
551+
}
552+
}
553+
554+
fn each_value_ref(op: fn(value: &V) -> bool) {
555+
do self.borrow_imm |p| {
556+
p.each_value_ref(op)
557+
}
558+
}
559+
}
560+
471561
#[cfg(test)]
472562
mod tests {
473563

trunk/src/libstd/smallintmap.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ impl<V: copy> smallintmap<V>: map::map<uint, V> {
8080
insert(self, key, value);
8181
return !exists;
8282
}
83-
fn remove(+key: uint) -> option<V> {
83+
fn remove(+key: uint) -> bool {
8484
if key >= self.v.len() {
85-
return none;
85+
return false;
8686
}
8787
let old = self.v.get_elt(key);
8888
self.v.set_elt(key, none);
89-
old
89+
old.is_some()
9090
}
9191
fn clear() {
9292
self.v.set(~[mut]);

0 commit comments

Comments
 (0)