Skip to content

Commit 2850640

Browse files
committed
---
yaml --- r: 23364 b: refs/heads/master c: 7fe1005 h: refs/heads/master v: v3
1 parent 70ea968 commit 2850640

File tree

11 files changed

+616
-26
lines changed

11 files changed

+616
-26
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: 6083409f172c8bbea59cec4bac749c46057b4774
2+
refs/heads/master: 7fe1005fd124fe79f4a4ea17d0056e55ff7bd3ae
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/os.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ fn pipe() -> {in: c_int, out: c_int} {
358358
}
359359

360360
fn dup2(src: c_int, dst: c_int) -> c_int {
361-
libc::dup2(src, dst)
361+
libc::dup2(src, dst)
362362
}
363363

364364

@@ -963,6 +963,9 @@ mod tests {
963963
setenv(~"USERPROFILE", ~"/home/MountainView");
964964
assert os::homedir() == some(~"/home/MountainView");
965965

966+
setenv(~"USERPROFILE", ~"/home/MountainView");
967+
assert os::homedir() == some(~"/home/MountainView");
968+
966969
setenv(~"HOME", ~"/home/MountainView");
967970
setenv(~"USERPROFILE", ~"/home/PaloAlto");
968971
assert os::homedir() == some(~"/home/MountainView");

trunk/src/libcore/send_map.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ mod linear {
5858
buckets: vec::from_fn(initial_capacity, |_i| none)})
5959
}
6060

61-
// FIXME(#2979) would allow us to use region type for k
62-
unsafe fn borrow<K>(&&k: K) -> &K {
63-
let p: *K = ptr::addr_of(k);
64-
unsafe::reinterpret_cast(p)
65-
}
66-
6761
priv impl<K, V> &const LinearMap<K,V> {
6862
#[inline(always)]
6963
pure fn to_bucket(h: uint) -> uint {
@@ -155,8 +149,7 @@ mod linear {
155149
/// Assumes that there will be a bucket.
156150
/// True if there was no previous entry with that key
157151
fn insert_internal(hash: uint, +k: K, +v: V) -> bool {
158-
match self.bucket_for_key_with_hash(self.buckets, hash,
159-
unsafe{borrow(k)}) {
152+
match self.bucket_for_key_with_hash(self.buckets, hash, &k) {
160153
TableFull => {fail ~"Internal logic error";}
161154
FoundHole(idx) => {
162155
debug!{"insert fresh (%?->%?) at idx %?, hash %?",
@@ -187,7 +180,7 @@ mod linear {
187180
self.expand();
188181
}
189182

190-
let hash = self.hashfn(unsafe{borrow(k)});
183+
let hash = self.hashfn(&k);
191184
self.insert_internal(hash, k, v)
192185
}
193186

@@ -228,6 +221,13 @@ mod linear {
228221
self.size -= 1;
229222
return true;
230223
}
224+
225+
fn clear() {
226+
for uint::range(0, self.buckets.len()) |idx| {
227+
self.buckets[idx] = none;
228+
}
229+
self.size = 0;
230+
}
231231
}
232232

233233
priv impl<K,V> &LinearMap<K,V> {
@@ -279,11 +279,19 @@ mod linear {
279279

280280
impl<K,V> &LinearMap<K,V> {
281281
/*
282-
FIXME --- #2979 must be fixed to typecheck this
283-
fn find_ptr(k: K) -> option<&V> {
284-
//XXX this should not type check as written, but it should
285-
//be *possible* to typecheck it...
286-
self.with_ptr(k, |v| v)
282+
FIXME(#3148)--region inference fails to capture needed deps
283+
284+
fn find_ref(k: &K) -> option<&self/V> {
285+
match self.bucket_for_key(self.buckets, k) {
286+
FoundEntry(idx) => {
287+
match check self.buckets[idx] {
288+
some(ref bkt) => some(&bkt.value)
289+
}
290+
}
291+
TableFull | FoundHole(_) => {
292+
none
293+
}
294+
}
287295
}
288296
*/
289297

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)