Skip to content

Commit 61b9e0e

Browse files
committedApr 11, 2013
core: changes in response to rust-lang#5656
·
1 parent 49de82c commit 61b9e0e

12 files changed

+700
-5
lines changed
 

‎src/libcore/condition.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct Condition<'self, T, U> {
2828
}
2929

3030
pub impl<'self, T, U> Condition<'self, T, U> {
31-
fn trap(&self, h: &'self fn(T) -> U) -> Trap<'self, T, U> {
31+
fn trap(&'self self, h: &'self fn(T) -> U) -> Trap<'self, T, U> {
3232
unsafe {
3333
let p : *RustClosure = ::cast::transmute(&h);
3434
let prev = task::local_data::local_data_get(self.key);

‎src/libcore/container.rs

+36
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub trait Mutable: Container {
2525
fn clear(&mut self);
2626
}
2727

28+
#[cfg(stage0)]
2829
pub trait Map<K, V>: Mutable {
2930
/// Return true if the map contains a value for the specified key
3031
fn contains_key(&self, key: &K) -> bool;
@@ -57,6 +58,41 @@ pub trait Map<K, V>: Mutable {
5758
fn remove(&mut self, key: &K) -> bool;
5859
}
5960

61+
#[cfg(stage1)]
62+
#[cfg(stage2)]
63+
#[cfg(stage3)]
64+
pub trait Map<K, V>: Mutable {
65+
/// Return true if the map contains a value for the specified key
66+
fn contains_key(&self, key: &K) -> bool;
67+
68+
// Visits all keys and values
69+
fn each<'a>(&'a self, f: &fn(&K, &'a V) -> bool);
70+
71+
/// Visit all keys
72+
fn each_key(&self, f: &fn(&K) -> bool);
73+
74+
/// Visit all values
75+
fn each_value<'a>(&'a self, f: &fn(&'a V) -> bool);
76+
77+
/// Iterate over the map and mutate the contained values
78+
fn mutate_values(&mut self, f: &fn(&K, &mut V) -> bool);
79+
80+
/// Return a reference to the value corresponding to the key
81+
fn find<'a>(&'a self, key: &K) -> Option<&'a V>;
82+
83+
/// Return a mutable reference to the value corresponding to the key
84+
fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V>;
85+
86+
/// Insert a key-value pair into the map. An existing value for a
87+
/// key is replaced by the new value. Return true if the key did
88+
/// not already exist in the map.
89+
fn insert(&mut self, key: K, value: V) -> bool;
90+
91+
/// Remove a key-value pair from the map. Return true if the key
92+
/// was present in the map, otherwise false.
93+
fn remove(&mut self, key: &K) -> bool;
94+
}
95+
6096
pub trait Set<T>: Mutable {
6197
/// Return true if the set contains a value
6298
fn contains(&self, value: &T) -> bool;

‎src/libcore/hashmap.rs

+171-2
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ priv impl<K:Hash + IterBytes + Eq,V> HashMap<K, V> {
186186
}
187187
}
188188

189+
#[cfg(stage0)]
189190
#[inline(always)]
190191
fn value_for_bucket(&self, idx: uint) -> &'self V {
191192
match self.buckets[idx] {
@@ -194,6 +195,18 @@ priv impl<K:Hash + IterBytes + Eq,V> HashMap<K, V> {
194195
}
195196
}
196197
198+
#[cfg(stage1)]
199+
#[cfg(stage2)]
200+
#[cfg(stage3)]
201+
#[inline(always)]
202+
fn value_for_bucket<'a>(&'a self, idx: uint) -> &'a V {
203+
match self.buckets[idx] {
204+
Some(ref bkt) => &bkt.value,
205+
None => fail!(~"HashMap::find: internal logic error"),
206+
}
207+
}
208+
209+
#[cfg(stage0)]
197210
#[inline(always)]
198211
fn mut_value_for_bucket(&mut self, idx: uint) -> &'self mut V {
199212
match self.buckets[idx] {
@@ -202,6 +215,17 @@ priv impl<K:Hash + IterBytes + Eq,V> HashMap<K, V> {
202215
}
203216
}
204217
218+
#[cfg(stage1)]
219+
#[cfg(stage2)]
220+
#[cfg(stage3)]
221+
#[inline(always)]
222+
fn mut_value_for_bucket<'a>(&'a mut self, idx: uint) -> &'a mut V {
223+
match self.buckets[idx] {
224+
Some(ref mut bkt) => &mut bkt.value,
225+
None => unreachable()
226+
}
227+
}
228+
205229
/// Inserts the key value pair into the buckets.
206230
/// Assumes that there will be a bucket.
207231
/// True if there was no previous entry with that key
@@ -307,6 +331,7 @@ impl<K:Hash + IterBytes + Eq,V> Map<K, V> for HashMap<K, V> {
307331
}
308332
309333
/// Visit all key-value pairs
334+
#[cfg(stage0)]
310335
fn each(&self, blk: &fn(&'self K, &'self V) -> bool) {
311336
for uint::range(0, self.buckets.len()) |i| {
312337
for self.buckets[i].each |bucket| {
@@ -317,19 +342,41 @@ impl<K:Hash + IterBytes + Eq,V> Map<K, V> for HashMap<K, V> {
317342
}
318343
}
319344
345+
/// Visit all key-value pairs
346+
#[cfg(stage1)]
347+
#[cfg(stage2)]
348+
#[cfg(stage3)]
349+
fn each<'a>(&'a self, blk: &fn(&'a K, &'a V) -> bool) {
350+
for uint::range(0, self.buckets.len()) |i| {
351+
for self.buckets[i].each |bucket| {
352+
if !blk(&bucket.key, &bucket.value) {
353+
return;
354+
}
355+
}
356+
}
357+
}
358+
320359
/// Visit all keys
321360
fn each_key(&self, blk: &fn(k: &K) -> bool) {
322361
self.each(|k, _| blk(k))
323362
}
324363
325364
/// Visit all values
365+
#[cfg(stage0)]
326366
fn each_value(&self, blk: &fn(v: &V) -> bool) {
327367
self.each(|_, v| blk(v))
328368
}
329369
370+
/// Visit all values
371+
#[cfg(stage1)]
372+
#[cfg(stage2)]
373+
#[cfg(stage3)]
374+
fn each_value<'a>(&'a self, blk: &fn(v: &'a V) -> bool) {
375+
self.each(|_, v| blk(v))
376+
}
377+
330378
/// Iterate over the map and mutate the contained values
331-
fn mutate_values(&mut self, blk: &fn(&'self K,
332-
&'self mut V) -> bool) {
379+
fn mutate_values(&mut self, blk: &fn(&K, &mut V) -> bool) {
333380
for uint::range(0, self.buckets.len()) |i| {
334381
match self.buckets[i] {
335382
Some(Bucket{key: ref key, value: ref mut value, _}) => {
@@ -341,14 +388,27 @@ impl<K:Hash + IterBytes + Eq,V> Map<K, V> for HashMap<K, V> {
341388
}
342389
343390
/// Return a reference to the value corresponding to the key
391+
#[cfg(stage0)]
344392
fn find(&self, k: &K) -> Option<&'self V> {
345393
match self.bucket_for_key(k) {
346394
FoundEntry(idx) => Some(self.value_for_bucket(idx)),
347395
TableFull | FoundHole(_) => None,
348396
}
349397
}
350398
399+
/// Return a reference to the value corresponding to the key
400+
#[cfg(stage1)]
401+
#[cfg(stage2)]
402+
#[cfg(stage3)]
403+
fn find<'a>(&'a self, k: &K) -> Option<&'a V> {
404+
match self.bucket_for_key(k) {
405+
FoundEntry(idx) => Some(self.value_for_bucket(idx)),
406+
TableFull | FoundHole(_) => None,
407+
}
408+
}
409+
351410
/// Return a mutable reference to the value corresponding to the key
411+
#[cfg(stage0)]
352412
fn find_mut(&mut self, k: &K) -> Option<&'self mut V> {
353413
let idx = match self.bucket_for_key(k) {
354414
FoundEntry(idx) => idx,
@@ -359,6 +419,20 @@ impl<K:Hash + IterBytes + Eq,V> Map<K, V> for HashMap<K, V> {
359419
}
360420
}
361421
422+
/// Return a mutable reference to the value corresponding to the key
423+
#[cfg(stage1)]
424+
#[cfg(stage2)]
425+
#[cfg(stage3)]
426+
fn find_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V> {
427+
let idx = match self.bucket_for_key(k) {
428+
FoundEntry(idx) => idx,
429+
TableFull | FoundHole(_) => return None
430+
};
431+
unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
432+
Some(::cast::transmute_mut_region(self.mut_value_for_bucket(idx)))
433+
}
434+
}
435+
362436
/// Insert a key-value pair into the map. An existing value for a
363437
/// key is replaced by the new value. Return true if the key did
364438
/// not already exist in the map.
@@ -431,6 +505,7 @@ pub impl<K: Hash + IterBytes + Eq, V> HashMap<K, V> {
431505
432506
/// Return the value corresponding to the key in the map, or insert
433507
/// and return the value if it doesn't exist.
508+
#[cfg(stage0)]
434509
fn find_or_insert(&mut self, k: K, v: V) -> &'self V {
435510
if self.size >= self.resize_at {
436511
// n.b.: We could also do this after searching, so
@@ -459,8 +534,42 @@ pub impl<K: Hash + IterBytes + Eq, V> HashMap<K, V> {
459534
}
460535
}
461536
537+
/// Return the value corresponding to the key in the map, or insert
538+
/// and return the value if it doesn't exist.
539+
#[cfg(stage1)]
540+
#[cfg(stage2)]
541+
#[cfg(stage3)]
542+
fn find_or_insert<'a>(&'a mut self, k: K, v: V) -> &'a V {
543+
if self.size >= self.resize_at {
544+
// n.b.: We could also do this after searching, so
545+
// that we do not resize if this call to insert is
546+
// simply going to update a key in place. My sense
547+
// though is that it's worse to have to search through
548+
// buckets to find the right spot twice than to just
549+
// resize in this corner case.
550+
self.expand();
551+
}
552+
553+
let hash = k.hash_keyed(self.k0, self.k1) as uint;
554+
let idx = match self.bucket_for_key_with_hash(hash, &k) {
555+
TableFull => fail!(~"Internal logic error"),
556+
FoundEntry(idx) => idx,
557+
FoundHole(idx) => {
558+
self.buckets[idx] = Some(Bucket{hash: hash, key: k,
559+
value: v});
560+
self.size += 1;
561+
idx
562+
},
563+
};
564+
565+
unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
566+
::cast::transmute_region(self.value_for_bucket(idx))
567+
}
568+
}
569+
462570
/// Return the value corresponding to the key in the map, or create,
463571
/// insert, and return a new value if it doesn't exist.
572+
#[cfg(stage0)]
464573
fn find_or_insert_with(&mut self, k: K, f: &fn(&K) -> V) -> &'self V {
465574
if self.size >= self.resize_at {
466575
// n.b.: We could also do this after searching, so
@@ -490,6 +599,40 @@ pub impl<K: Hash + IterBytes + Eq, V> HashMap<K, V> {
490599
}
491600
}
492601
602+
/// Return the value corresponding to the key in the map, or create,
603+
/// insert, and return a new value if it doesn't exist.
604+
#[cfg(stage1)]
605+
#[cfg(stage2)]
606+
#[cfg(stage3)]
607+
fn find_or_insert_with<'a>(&'a mut self, k: K, f: &fn(&K) -> V) -> &'a V {
608+
if self.size >= self.resize_at {
609+
// n.b.: We could also do this after searching, so
610+
// that we do not resize if this call to insert is
611+
// simply going to update a key in place. My sense
612+
// though is that it's worse to have to search through
613+
// buckets to find the right spot twice than to just
614+
// resize in this corner case.
615+
self.expand();
616+
}
617+
618+
let hash = k.hash_keyed(self.k0, self.k1) as uint;
619+
let idx = match self.bucket_for_key_with_hash(hash, &k) {
620+
TableFull => fail!(~"Internal logic error"),
621+
FoundEntry(idx) => idx,
622+
FoundHole(idx) => {
623+
let v = f(&k);
624+
self.buckets[idx] = Some(Bucket{hash: hash, key: k,
625+
value: v});
626+
self.size += 1;
627+
idx
628+
},
629+
};
630+
631+
unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
632+
::cast::transmute_region(self.value_for_bucket(idx))
633+
}
634+
}
635+
493636
fn consume(&mut self, f: &fn(K, V)) {
494637
let mut buckets = ~[];
495638
self.buckets <-> buckets;
@@ -506,13 +649,24 @@ pub impl<K: Hash + IterBytes + Eq, V> HashMap<K, V> {
506649
}
507650
}
508651
652+
#[cfg(stage0)]
509653
fn get(&self, k: &K) -> &'self V {
510654
match self.find(k) {
511655
Some(v) => v,
512656
None => fail!(fmt!("No entry found for key: %?", k)),
513657
}
514658
}
515659
660+
#[cfg(stage1)]
661+
#[cfg(stage2)]
662+
#[cfg(stage3)]
663+
fn get<'a>(&'a self, k: &K) -> &'a V {
664+
match self.find(k) {
665+
Some(v) => v,
666+
None => fail!(fmt!("No entry found for key: %?", k)),
667+
}
668+
}
669+
516670
/// Return true if the map contains a value for the specified key,
517671
/// using equivalence
518672
fn contains_key_equiv<Q:Hash + IterBytes + Equiv<K>>(&self, key: &Q)
@@ -525,13 +679,28 @@ pub impl<K: Hash + IterBytes + Eq, V> HashMap<K, V> {
525679

526680
/// Return the value corresponding to the key in the map, using
527681
/// equivalence
682+
#[cfg(stage0)]
528683
fn find_equiv<Q:Hash + IterBytes + Equiv<K>>(&self, k: &Q)
529684
-> Option<&'self V> {
530685
match self.bucket_for_key_equiv(k) {
531686
FoundEntry(idx) => Some(self.value_for_bucket(idx)),
532687
TableFull | FoundHole(_) => None,
533688
}
534689
}
690+
691+
/// Return the value corresponding to the key in the map, using
692+
/// equivalence
693+
#[cfg(stage1)]
694+
#[cfg(stage2)]
695+
#[cfg(stage3)]
696+
fn find_equiv<'a, Q:Hash + IterBytes + Equiv<K>>(
697+
&'a self, k: &Q) -> Option<&'a V>
698+
{
699+
match self.bucket_for_key_equiv(k) {
700+
FoundEntry(idx) => Some(self.value_for_bucket(idx)),
701+
TableFull | FoundHole(_) => None,
702+
}
703+
}
535704
}
536705

537706
impl<K:Hash + IterBytes + Eq,V:Eq> Eq for HashMap<K, V> {

‎src/libcore/option.rs

+104
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,41 @@ impl<T: Copy + Add<T,T>> Add<Option<T>, Option<T>> for Option<T> {
101101

102102
impl<T> BaseIter<T> for Option<T> {
103103
/// Performs an operation on the contained value by reference
104+
#[cfg(stage0)]
104105
#[inline(always)]
105106
fn each(&self, f: &fn(x: &'self T) -> bool) {
106107
match *self { None => (), Some(ref t) => { f(t); } }
107108
}
108109

110+
/// Performs an operation on the contained value by reference
111+
#[cfg(stage1)]
112+
#[cfg(stage2)]
113+
#[cfg(stage3)]
114+
#[inline(always)]
115+
fn each<'a>(&'a self, f: &fn(x: &'a T) -> bool) {
116+
match *self { None => (), Some(ref t) => { f(t); } }
117+
}
118+
109119
#[inline(always)]
110120
fn size_hint(&self) -> Option<uint> {
111121
if self.is_some() { Some(1) } else { Some(0) }
112122
}
113123
}
114124

115125
impl<T> MutableIter<T> for Option<T> {
126+
#[cfg(stage0)]
116127
#[inline(always)]
117128
fn each_mut(&mut self, f: &fn(&'self mut T) -> bool) {
118129
match *self { None => (), Some(ref mut t) => { f(t); } }
119130
}
131+
132+
#[cfg(stage1)]
133+
#[cfg(stage2)]
134+
#[cfg(stage3)]
135+
#[inline(always)]
136+
fn each_mut<'a>(&'a mut self, f: &fn(&'a mut T) -> bool) {
137+
match *self { None => (), Some(ref mut t) => { f(t); } }
138+
}
120139
}
121140

122141
impl<A> ExtendedIter<A> for Option<A> {
@@ -182,17 +201,40 @@ pub impl<T> Option<T> {
182201
* Update an optional value by optionally running its content by reference
183202
* through a function that returns an option.
184203
*/
204+
#[cfg(stage0)]
185205
#[inline(always)]
186206
fn chain_ref<U>(&self, f: &fn(x: &'self T) -> Option<U>) -> Option<U> {
187207
match *self { Some(ref x) => f(x), None => None }
188208
}
189209

210+
/**
211+
* Update an optional value by optionally running its content by reference
212+
* through a function that returns an option.
213+
*/
214+
#[cfg(stage1)]
215+
#[cfg(stage2)]
216+
#[cfg(stage3)]
217+
#[inline(always)]
218+
fn chain_ref<'a, U>(&'a self, f: &fn(x: &'a T) -> Option<U>) -> Option<U> {
219+
match *self { Some(ref x) => f(x), None => None }
220+
}
221+
190222
/// Maps a `some` value from one type to another by reference
223+
#[cfg(stage0)]
191224
#[inline(always)]
192225
fn map<U>(&self, f: &fn(&'self T) -> U) -> Option<U> {
193226
match *self { Some(ref x) => Some(f(x)), None => None }
194227
}
195228

229+
/// Maps a `some` value from one type to another by reference
230+
#[cfg(stage1)]
231+
#[cfg(stage2)]
232+
#[cfg(stage3)]
233+
#[inline(always)]
234+
fn map<'a, U>(&self, f: &fn(&'a T) -> U) -> Option<U> {
235+
match *self { Some(ref x) => Some(f(x)), None => None }
236+
}
237+
196238
/// As `map`, but consumes the option and gives `f` ownership to avoid
197239
/// copying.
198240
#[inline(always)]
@@ -201,11 +243,21 @@ pub impl<T> Option<T> {
201243
}
202244

203245
/// Applies a function to the contained value or returns a default
246+
#[cfg(stage0)]
204247
#[inline(always)]
205248
fn map_default<U>(&self, def: U, f: &fn(&'self T) -> U) -> U {
206249
match *self { None => def, Some(ref t) => f(t) }
207250
}
208251

252+
/// Applies a function to the contained value or returns a default
253+
#[cfg(stage1)]
254+
#[cfg(stage2)]
255+
#[cfg(stage3)]
256+
#[inline(always)]
257+
fn map_default<'a, U>(&'a self, def: U, f: &fn(&'a T) -> U) -> U {
258+
match *self { None => def, Some(ref t) => f(t) }
259+
}
260+
209261
/// As `map_default`, but consumes the option and gives `f`
210262
/// ownership to avoid copying.
211263
#[inline(always)]
@@ -244,13 +296,39 @@ pub impl<T> Option<T> {
244296
case explicitly.
245297
*/
246298
#[inline(always)]
299+
#[cfg(stage0)]
247300
fn get_ref(&self) -> &'self T {
248301
match *self {
249302
Some(ref x) => x,
250303
None => fail!(~"option::get_ref none")
251304
}
252305
}
253306
307+
/**
308+
Gets an immutable reference to the value inside an option.
309+
310+
# Failure
311+
312+
Fails if the value equals `None`
313+
314+
# Safety note
315+
316+
In general, because this function may fail, its use is discouraged
317+
(calling `get` on `None` is akin to dereferencing a null pointer).
318+
Instead, prefer to use pattern matching and handle the `None`
319+
case explicitly.
320+
*/
321+
#[inline(always)]
322+
#[cfg(stage1)]
323+
#[cfg(stage2)]
324+
#[cfg(stage3)]
325+
fn get_ref<'a>(&'a self) -> &'a T {
326+
match *self {
327+
Some(ref x) => x,
328+
None => fail!(~"option::get_ref none")
329+
}
330+
}
331+
254332
/**
255333
Gets a mutable reference to the value inside an option.
256334
@@ -266,13 +344,39 @@ pub impl<T> Option<T> {
266344
case explicitly.
267345
*/
268346
#[inline(always)]
347+
#[cfg(stage0)]
269348
fn get_mut_ref(&mut self) -> &'self mut T {
270349
match *self {
271350
Some(ref mut x) => x,
272351
None => fail!(~"option::get_mut_ref none")
273352
}
274353
}
275354
355+
/**
356+
Gets a mutable reference to the value inside an option.
357+
358+
# Failure
359+
360+
Fails if the value equals `None`
361+
362+
# Safety note
363+
364+
In general, because this function may fail, its use is discouraged
365+
(calling `get` on `None` is akin to dereferencing a null pointer).
366+
Instead, prefer to use pattern matching and handle the `None`
367+
case explicitly.
368+
*/
369+
#[inline(always)]
370+
#[cfg(stage1)]
371+
#[cfg(stage2)]
372+
#[cfg(stage3)]
373+
fn get_mut_ref<'a>(&'a mut self) -> &'a mut T {
374+
match *self {
375+
Some(ref mut x) => x,
376+
None => fail!(~"option::get_mut_ref none")
377+
}
378+
}
379+
276380
#[inline(always)]
277381
fn unwrap(self) -> T {
278382
/*!

‎src/libcore/result.rs

+7
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,16 @@ pub fn map_err<T:Copy,E,F:Copy>(res: &Result<T, E>, op: &fn(&E) -> F)
226226
}
227227
228228
pub impl<T, E> Result<T, E> {
229+
#[cfg(stage0)]
229230
#[inline(always)]
230231
fn get_ref(&self) -> &'self T { get_ref(self) }
231232
233+
#[cfg(stage1)]
234+
#[cfg(stage2)]
235+
#[cfg(stage3)]
236+
#[inline(always)]
237+
fn get_ref<'a>(&'a self) -> &'a T { get_ref(self) }
238+
232239
#[inline(always)]
233240
fn is_ok(&self) -> bool { is_ok(self) }
234241

‎src/libcore/rt/rtio.rs

+5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ pub trait EventLoop {
2222
fn run(&mut self);
2323
fn callback(&mut self, ~fn());
2424
/// The asynchronous I/O services. Not all event loops may provide one
25+
#[cfg(stage0)]
2526
fn io(&mut self) -> Option<&'self mut IoFactoryObject>;
27+
#[cfg(stage1)]
28+
#[cfg(stage2)]
29+
#[cfg(stage3)]
30+
fn io<'a>(&'a mut self) -> Option<&'a mut IoFactoryObject>;
2631
}
2732

2833
pub trait IoFactory {

‎src/libcore/rt/sched.rs

+40
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ pub impl Scheduler {
272272

273273
// XXX: Hack. This should return &'self mut but I don't know how to
274274
// make the borrowcheck happy
275+
#[cfg(stage0)]
275276
fn task_from_last_cleanup_job(&mut self) -> &mut Task {
276277
assert!(!self.cleanup_jobs.is_empty());
277278
let last_job: &'self mut CleanupJob = &mut self.cleanup_jobs[0];
@@ -285,6 +286,25 @@ pub impl Scheduler {
285286
// borrows
286287
return unsafe { transmute::<&Task, &mut Task>(last_task) };
287288
}
289+
290+
// XXX: Hack. This should return &'self mut but I don't know how to
291+
// make the borrowcheck happy
292+
#[cfg(stage1)]
293+
#[cfg(stage2)]
294+
#[cfg(stage3)]
295+
fn task_from_last_cleanup_job<'a>(&'a mut self) -> &mut Task {
296+
assert!(!self.cleanup_jobs.is_empty());
297+
let last_job: &'a mut CleanupJob = &mut self.cleanup_jobs[0];
298+
let last_task: &'a Task = match last_job {
299+
&RescheduleTask(~ref task) => task,
300+
&RecycleTask(~ref task) => task,
301+
&GiveTask(~ref task, _) => task,
302+
};
303+
// XXX: Pattern matching mutable pointers above doesn't work
304+
// because borrowck thinks the three patterns are conflicting
305+
// borrows
306+
return unsafe { transmute::<&Task, &mut Task>(last_task) };
307+
}
288308
}
289309

290310
static TASK_MIN_STACK_SIZE: uint = 10000000; // XXX: Too much stack
@@ -354,6 +374,7 @@ impl ThreadLocalScheduler {
354374
}
355375
}
356376

377+
#[cfg(stage0)]
357378
fn get_scheduler(&mut self) -> &'self mut Scheduler {
358379
unsafe {
359380
let key = match self { &ThreadLocalScheduler(key) => key };
@@ -370,6 +391,25 @@ impl ThreadLocalScheduler {
370391
}
371392
}
372393

394+
#[cfg(stage1)]
395+
#[cfg(stage2)]
396+
#[cfg(stage3)]
397+
fn get_scheduler<'a>(&'a mut self) -> &'a mut Scheduler {
398+
unsafe {
399+
let key = match self { &ThreadLocalScheduler(key) => key };
400+
let mut value: *mut c_void = tls::get(key);
401+
assert!(value.is_not_null());
402+
{
403+
let value_ptr = &mut value;
404+
let sched: &mut ~Scheduler = {
405+
transmute::<&mut *mut c_void, &mut ~Scheduler>(value_ptr)
406+
};
407+
let sched: &mut Scheduler = &mut **sched;
408+
return sched;
409+
}
410+
}
411+
}
412+
373413
fn take_scheduler(&mut self) -> ~Scheduler {
374414
unsafe {
375415
let key = match self { &ThreadLocalScheduler(key) => key };

‎src/libcore/rt/uvio.rs

+16
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,17 @@ impl EventLoop for UvEventLoop {
6767
}
6868
}
6969

70+
#[cfg(stage0)]
7071
fn io(&mut self) -> Option<&'self mut IoFactoryObject> {
7172
Some(&mut self.uvio)
7273
}
74+
75+
#[cfg(stage1)]
76+
#[cfg(stage2)]
77+
#[cfg(stage3)]
78+
fn io<'a>(&'a mut self) -> Option<&'a mut IoFactoryObject> {
79+
Some(&mut self.uvio)
80+
}
7381
}
7482

7583
#[test]
@@ -89,9 +97,17 @@ fn test_callback_run_once() {
8997
pub struct UvIoFactory(Loop);
9098

9199
pub impl UvIoFactory {
100+
#[cfg(stage0)]
92101
fn uv_loop(&mut self) -> &'self mut Loop {
93102
match self { &UvIoFactory(ref mut ptr) => ptr }
94103
}
104+
105+
#[cfg(stage1)]
106+
#[cfg(stage2)]
107+
#[cfg(stage3)]
108+
fn uv_loop<'a>(&'a mut self) -> &'a mut Loop {
109+
match self { &UvIoFactory(ref mut ptr) => ptr }
110+
}
95111
}
96112

97113
impl IoFactory for UvIoFactory {

‎src/libcore/task/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use result::Result;
3939
use comm::{stream, Chan, GenericChan, GenericPort, Port};
4040
use prelude::*;
4141
use result;
42-
use task::rt::{task_id, sched_id, rust_task};
42+
use task::rt::{task_id, sched_id};
4343
use util;
4444
use util::replace;
4545
use unstable::finally::Finally;

‎src/libcore/trie.rs

+96-1
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,20 @@ impl<T> Map<uint, T> for TrieMap<T> {
5656

5757
/// Visit all key-value pairs in order
5858
#[inline(always)]
59+
#[cfg(stage0)]
5960
fn each(&self, f: &fn(&uint, &'self T) -> bool) {
6061
self.root.each(f);
6162
}
6263

64+
/// Visit all key-value pairs in order
65+
#[inline(always)]
66+
#[cfg(stage1)]
67+
#[cfg(stage2)]
68+
#[cfg(stage3)]
69+
fn each<'a>(&'a self, f: &fn(&uint, &'a T) -> bool) {
70+
self.root.each(f);
71+
}
72+
6373
/// Visit all keys in order
6474
#[inline(always)]
6575
fn each_key(&self, f: &fn(&uint) -> bool) {
@@ -68,17 +78,28 @@ impl<T> Map<uint, T> for TrieMap<T> {
6878

6979
/// Visit all values in order
7080
#[inline(always)]
81+
#[cfg(stage0)]
7182
fn each_value(&self, f: &fn(&T) -> bool) {
7283
self.each(|_, v| f(v))
7384
}
7485

86+
/// Visit all values in order
87+
#[inline(always)]
88+
#[cfg(stage1)]
89+
#[cfg(stage2)]
90+
#[cfg(stage3)]
91+
fn each_value<'a>(&'a self, f: &fn(&'a T) -> bool) {
92+
self.each(|_, v| f(v))
93+
}
94+
7595
/// Iterate over the map and mutate the contained values
7696
#[inline(always)]
7797
fn mutate_values(&mut self, f: &fn(&uint, &mut T) -> bool) {
7898
self.root.mutate_values(f);
7999
}
80100

81101
/// Return a reference to the value corresponding to the key
102+
#[cfg(stage0)]
82103
#[inline(hint)]
83104
fn find(&self, key: &uint) -> Option<&'self T> {
84105
let mut node: &'self TrieNode<T> = &self.root;
@@ -99,12 +120,46 @@ impl<T> Map<uint, T> for TrieMap<T> {
99120
}
100121
}
101122

123+
/// Return a reference to the value corresponding to the key
124+
#[cfg(stage1)]
125+
#[cfg(stage2)]
126+
#[cfg(stage3)]
127+
#[inline(hint)]
128+
fn find<'a>(&'a self, key: &uint) -> Option<&'a T> {
129+
let mut node: &'a TrieNode<T> = &self.root;
130+
let mut idx = 0;
131+
loop {
132+
match node.children[chunk(*key, idx)] {
133+
Internal(ref x) => node = &**x,
134+
External(stored, ref value) => {
135+
if stored == *key {
136+
return Some(value)
137+
} else {
138+
return None
139+
}
140+
}
141+
Nothing => return None
142+
}
143+
idx += 1;
144+
}
145+
}
146+
102147
/// Return a mutable reference to the value corresponding to the key
148+
#[cfg(stage0)]
103149
#[inline(always)]
104150
fn find_mut(&mut self, key: &uint) -> Option<&'self mut T> {
105151
find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1)
106152
}
107153

154+
/// Return a mutable reference to the value corresponding to the key
155+
#[cfg(stage1)]
156+
#[cfg(stage2)]
157+
#[cfg(stage3)]
158+
#[inline(always)]
159+
fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut T> {
160+
find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1)
161+
}
162+
108163
/// Insert a key-value pair into the map. An existing value for a
109164
/// key is replaced by the new value. Return true if the key did
110165
/// not already exist in the map.
@@ -138,10 +193,20 @@ pub impl<T> TrieMap<T> {
138193

139194
/// Visit all key-value pairs in reverse order
140195
#[inline(always)]
196+
#[cfg(stage0)]
141197
fn each_reverse(&self, f: &fn(&uint, &'self T) -> bool) {
142198
self.root.each_reverse(f);
143199
}
144200

201+
/// Visit all key-value pairs in reverse order
202+
#[inline(always)]
203+
#[cfg(stage1)]
204+
#[cfg(stage2)]
205+
#[cfg(stage3)]
206+
fn each_reverse<'a>(&'a self, f: &fn(&uint, &'a T) -> bool) {
207+
self.root.each_reverse(f);
208+
}
209+
145210
/// Visit all keys in reverse order
146211
#[inline(always)]
147212
fn each_key_reverse(&self, f: &fn(&uint) -> bool) {
@@ -233,6 +298,7 @@ impl<T> TrieNode<T> {
233298
}
234299

235300
impl<T> TrieNode<T> {
301+
#[cfg(stage0)]
236302
fn each(&self, f: &fn(&uint, &'self T) -> bool) -> bool {
237303
for uint::range(0, self.children.len()) |idx| {
238304
match self.children[idx] {
@@ -244,6 +310,21 @@ impl<T> TrieNode<T> {
244310
true
245311
}
246312

313+
#[cfg(stage1)]
314+
#[cfg(stage2)]
315+
#[cfg(stage3)]
316+
fn each<'a>(&'a self, f: &fn(&uint, &'a T) -> bool) -> bool {
317+
for uint::range(0, self.children.len()) |idx| {
318+
match self.children[idx] {
319+
Internal(ref x) => if !x.each(f) { return false },
320+
External(k, ref v) => if !f(&k, v) { return false },
321+
Nothing => ()
322+
}
323+
}
324+
true
325+
}
326+
327+
#[cfg(stage0)]
247328
fn each_reverse(&self, f: &fn(&uint, &'self T) -> bool) -> bool {
248329
for uint::range_rev(self.children.len(), 0) |idx| {
249330
match self.children[idx - 1] {
@@ -255,7 +336,21 @@ impl<T> TrieNode<T> {
255336
true
256337
}
257338

258-
fn mutate_values(&mut self, f: &fn(&uint, &mut T) -> bool) -> bool {
339+
#[cfg(stage1)]
340+
#[cfg(stage2)]
341+
#[cfg(stage3)]
342+
fn each_reverse<'a>(&'a self, f: &fn(&uint, &'a T) -> bool) -> bool {
343+
for uint::range_rev(self.children.len(), 0) |idx| {
344+
match self.children[idx - 1] {
345+
Internal(ref x) => if !x.each_reverse(f) { return false },
346+
External(k, ref v) => if !f(&k, v) { return false },
347+
Nothing => ()
348+
}
349+
}
350+
true
351+
}
352+
353+
fn mutate_values<'a>(&'a mut self, f: &fn(&uint, &mut T) -> bool) -> bool {
259354
for vec::each_mut(self.children) |child| {
260355
match *child {
261356
Internal(ref mut x) => if !x.mutate_values(f) {

‎src/libcore/tuple.rs

+28
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ impl<T:Clone,U:Clone> Clone for (T, U) {
5656
}
5757
}
5858

59+
#[cfg(stage0)]
5960
pub trait ImmutableTuple<T, U> {
6061
fn first_ref(&self) -> &'self T;
6162
fn second_ref(&self) -> &'self U;
6263
}
6364

65+
#[cfg(stage0)]
6466
impl<T, U> ImmutableTuple<T, U> for (T, U) {
6567
#[inline(always)]
6668
fn first_ref(&self) -> &'self T {
@@ -76,6 +78,32 @@ impl<T, U> ImmutableTuple<T, U> for (T, U) {
7678
}
7779
}
7880

81+
#[cfg(stage1)]
82+
#[cfg(stage2)]
83+
#[cfg(stage3)]
84+
pub trait ImmutableTuple<T, U> {
85+
fn first_ref<'a>(&'a self) -> &'a T;
86+
fn second_ref<'a>(&'a self) -> &'a U;
87+
}
88+
89+
#[cfg(stage1)]
90+
#[cfg(stage2)]
91+
#[cfg(stage3)]
92+
impl<T, U> ImmutableTuple<T, U> for (T, U) {
93+
#[inline(always)]
94+
fn first_ref<'a>(&'a self) -> &'a T {
95+
match *self {
96+
(ref t, _) => t,
97+
}
98+
}
99+
#[inline(always)]
100+
fn second_ref<'a>(&'a self) -> &'a U {
101+
match *self {
102+
(_, ref u) => u,
103+
}
104+
}
105+
}
106+
79107
pub trait ExtendedTupleOps<A,B> {
80108
fn zip(&self) -> ~[(A, B)];
81109
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C];

‎src/libcore/vec.rs

+195
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,7 @@ impl<'self,T:Copy> CopyableVector<T> for &'self const [T] {
17631763
}
17641764
}
17651765

1766+
#[cfg(stage0)]
17661767
pub trait ImmutableVector<T> {
17671768
fn slice(&self, start: uint, end: uint) -> &'self [T];
17681769
fn head(&self) -> &'self T;
@@ -1785,6 +1786,7 @@ pub trait ImmutableVector<T> {
17851786
}
17861787

17871788
/// Extension methods for vectors
1789+
#[cfg(stage0)]
17881790
impl<'self,T> ImmutableVector<T> for &'self [T] {
17891791
/// Return a slice that points into another slice.
17901792
#[inline]
@@ -1893,6 +1895,142 @@ impl<'self,T> ImmutableVector<T> for &'self [T] {
18931895
}
18941896
}
18951897

1898+
#[cfg(stage1)]
1899+
#[cfg(stage2)]
1900+
#[cfg(stage3)]
1901+
pub trait ImmutableVector<'self, T> {
1902+
fn slice(&self, start: uint, end: uint) -> &'self [T];
1903+
fn head(&self) -> &'self T;
1904+
fn head_opt(&self) -> Option<&'self T>;
1905+
fn tail(&self) -> &'self [T];
1906+
fn tailn(&self, n: uint) -> &'self [T];
1907+
fn init(&self) -> &'self [T];
1908+
fn initn(&self, n: uint) -> &'self [T];
1909+
fn last(&self) -> &'self T;
1910+
fn last_opt(&self) -> Option<&'self T>;
1911+
fn each_reverse(&self, blk: &fn(&T) -> bool);
1912+
fn eachi_reverse(&self, blk: &fn(uint, &T) -> bool);
1913+
fn foldr<U: Copy>(&self, z: U, p: &fn(t: &T, u: U) -> U) -> U;
1914+
fn map<U>(&self, f: &fn(t: &T) -> U) -> ~[U];
1915+
fn mapi<U>(&self, f: &fn(uint, t: &T) -> U) -> ~[U];
1916+
fn map_r<U>(&self, f: &fn(x: &T) -> U) -> ~[U];
1917+
fn alli(&self, f: &fn(uint, t: &T) -> bool) -> bool;
1918+
fn flat_map<U>(&self, f: &fn(t: &T) -> ~[U]) -> ~[U];
1919+
fn filter_mapped<U:Copy>(&self, f: &fn(t: &T) -> Option<U>) -> ~[U];
1920+
}
1921+
1922+
/// Extension methods for vectors
1923+
#[cfg(stage1)]
1924+
#[cfg(stage2)]
1925+
#[cfg(stage3)]
1926+
impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
1927+
/// Return a slice that points into another slice.
1928+
#[inline]
1929+
fn slice(&self, start: uint, end: uint) -> &'self [T] {
1930+
slice(*self, start, end)
1931+
}
1932+
1933+
/// Returns the first element of a vector, failing if the vector is empty.
1934+
#[inline]
1935+
fn head(&self) -> &'self T { head(*self) }
1936+
1937+
/// Returns the first element of a vector
1938+
#[inline]
1939+
fn head_opt(&self) -> Option<&'self T> { head_opt(*self) }
1940+
1941+
/// Returns all but the first element of a vector
1942+
#[inline]
1943+
fn tail(&self) -> &'self [T] { tail(*self) }
1944+
1945+
/// Returns all but the first `n' elements of a vector
1946+
#[inline]
1947+
fn tailn(&self, n: uint) -> &'self [T] { tailn(*self, n) }
1948+
1949+
/// Returns all but the last elemnt of a vector
1950+
#[inline]
1951+
fn init(&self) -> &'self [T] { init(*self) }
1952+
1953+
/// Returns all but the last `n' elemnts of a vector
1954+
#[inline]
1955+
fn initn(&self, n: uint) -> &'self [T] { initn(*self, n) }
1956+
1957+
/// Returns the last element of a `v`, failing if the vector is empty.
1958+
#[inline]
1959+
fn last(&self) -> &'self T { last(*self) }
1960+
1961+
/// Returns the last element of a `v`, failing if the vector is empty.
1962+
#[inline]
1963+
fn last_opt(&self) -> Option<&'self T> { last_opt(*self) }
1964+
1965+
/// Iterates over a vector's elements in reverse.
1966+
#[inline]
1967+
fn each_reverse(&self, blk: &fn(&T) -> bool) {
1968+
each_reverse(*self, blk)
1969+
}
1970+
1971+
/// Iterates over a vector's elements and indices in reverse.
1972+
#[inline]
1973+
fn eachi_reverse(&self, blk: &fn(uint, &T) -> bool) {
1974+
eachi_reverse(*self, blk)
1975+
}
1976+
1977+
/// Reduce a vector from right to left
1978+
#[inline]
1979+
fn foldr<U:Copy>(&self, z: U, p: &fn(t: &T, u: U) -> U) -> U {
1980+
foldr(*self, z, p)
1981+
}
1982+
1983+
/// Apply a function to each element of a vector and return the results
1984+
#[inline]
1985+
fn map<U>(&self, f: &fn(t: &T) -> U) -> ~[U] { map(*self, f) }
1986+
1987+
/**
1988+
* Apply a function to the index and value of each element in the vector
1989+
* and return the results
1990+
*/
1991+
fn mapi<U>(&self, f: &fn(uint, t: &T) -> U) -> ~[U] {
1992+
mapi(*self, f)
1993+
}
1994+
1995+
#[inline]
1996+
fn map_r<U>(&self, f: &fn(x: &T) -> U) -> ~[U] {
1997+
let mut r = ~[];
1998+
let mut i = 0;
1999+
while i < self.len() {
2000+
r.push(f(&self[i]));
2001+
i += 1;
2002+
}
2003+
r
2004+
}
2005+
2006+
/**
2007+
* Returns true if the function returns true for all elements.
2008+
*
2009+
* If the vector is empty, true is returned.
2010+
*/
2011+
fn alli(&self, f: &fn(uint, t: &T) -> bool) -> bool {
2012+
alli(*self, f)
2013+
}
2014+
/**
2015+
* Apply a function to each element of a vector and return a concatenation
2016+
* of each result vector
2017+
*/
2018+
#[inline]
2019+
fn flat_map<U>(&self, f: &fn(t: &T) -> ~[U]) -> ~[U] {
2020+
flat_map(*self, f)
2021+
}
2022+
/**
2023+
* Apply a function to each element of a vector and return the results
2024+
*
2025+
* If function `f` returns `none` then that element is excluded from
2026+
* the resulting vector.
2027+
*/
2028+
#[inline]
2029+
fn filter_mapped<U:Copy>(&self, f: &fn(t: &T) -> Option<U>) -> ~[U] {
2030+
filter_mapped(*self, f)
2031+
}
2032+
}
2033+
18962034
pub trait ImmutableEqVector<T:Eq> {
18972035
fn position(&self, f: &fn(t: &T) -> bool) -> Option<uint>;
18982036
fn position_elem(&self, t: &T) -> Option<uint>;
@@ -2353,14 +2491,26 @@ pub mod bytes {
23532491
// ___________________________________________________________________________
23542492
// ITERATION TRAIT METHODS
23552493

2494+
#[cfg(stage0)]
23562495
impl<'self,A> iter::BaseIter<A> for &'self [A] {
23572496
#[inline(always)]
23582497
fn each(&self, blk: &fn(v: &'self A) -> bool) { each(*self, blk) }
23592498
#[inline(always)]
23602499
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
23612500
}
23622501

2502+
#[cfg(stage1)]
2503+
#[cfg(stage2)]
2504+
#[cfg(stage3)]
2505+
impl<'self,A> iter::BaseIter<A> for &'self [A] {
2506+
#[inline(always)]
2507+
fn each<'a>(&'a self, blk: &fn(v: &'a A) -> bool) { each(*self, blk) }
2508+
#[inline(always)]
2509+
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
2510+
}
2511+
23632512
// FIXME(#4148): This should be redundant
2513+
#[cfg(stage0)]
23642514
impl<A> iter::BaseIter<A> for ~[A] {
23652515
#[inline(always)]
23662516
fn each(&self, blk: &fn(v: &'self A) -> bool) { each(*self, blk) }
@@ -2369,28 +2519,73 @@ impl<A> iter::BaseIter<A> for ~[A] {
23692519
}
23702520

23712521
// FIXME(#4148): This should be redundant
2522+
#[cfg(stage1)]
2523+
#[cfg(stage2)]
2524+
#[cfg(stage3)]
2525+
impl<A> iter::BaseIter<A> for ~[A] {
2526+
#[inline(always)]
2527+
fn each<'a>(&'a self, blk: &fn(v: &'a A) -> bool) { each(*self, blk) }
2528+
#[inline(always)]
2529+
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
2530+
}
2531+
2532+
// FIXME(#4148): This should be redundant
2533+
#[cfg(stage0)]
23722534
impl<A> iter::BaseIter<A> for @[A] {
23732535
#[inline(always)]
23742536
fn each(&self, blk: &fn(v: &'self A) -> bool) { each(*self, blk) }
23752537
#[inline(always)]
23762538
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
23772539
}
23782540

2541+
// FIXME(#4148): This should be redundant
2542+
#[cfg(stage1)]
2543+
#[cfg(stage2)]
2544+
#[cfg(stage3)]
2545+
impl<A> iter::BaseIter<A> for @[A] {
2546+
#[inline(always)]
2547+
fn each<'a>(&'a self, blk: &fn(v: &'a A) -> bool) { each(*self, blk) }
2548+
#[inline(always)]
2549+
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
2550+
}
2551+
2552+
#[cfg(stage0)]
23792553
impl<'self,A> iter::MutableIter<A> for &'self mut [A] {
23802554
#[inline(always)]
23812555
fn each_mut(&mut self, blk: &fn(v: &'self mut A) -> bool) {
23822556
each_mut(*self, blk)
23832557
}
23842558
}
23852559

2560+
#[cfg(stage1)]
2561+
#[cfg(stage2)]
2562+
#[cfg(stage3)]
2563+
impl<'self,A> iter::MutableIter<A> for &'self mut [A] {
2564+
#[inline(always)]
2565+
fn each_mut<'a>(&'a mut self, blk: &fn(v: &'a mut A) -> bool) {
2566+
each_mut(*self, blk)
2567+
}
2568+
}
2569+
23862570
// FIXME(#4148): This should be redundant
2571+
#[cfg(stage0)]
23872572
impl<A> iter::MutableIter<A> for ~[A] {
23882573
#[inline(always)]
23892574
fn each_mut(&mut self, blk: &fn(v: &'self mut A) -> bool) {
23902575
each_mut(*self, blk)
23912576
}
23922577
}
23932578

2579+
#[cfg(stage1)]
2580+
#[cfg(stage2)]
2581+
#[cfg(stage3)]
2582+
impl<A> iter::MutableIter<A> for ~[A] {
2583+
#[inline(always)]
2584+
fn each_mut<'a>(&'a mut self, blk: &fn(v: &'a mut A) -> bool) {
2585+
each_mut(*self, blk)
2586+
}
2587+
}
2588+
23942589
// FIXME(#4148): This should be redundant
23952590
impl<A> iter::MutableIter<A> for @mut [A] {
23962591
#[inline(always)]

1 commit comments

Comments
 (1)

pcwalton commented on Apr 12, 2013

@pcwalton

r+

Please sign in to comment.