Skip to content

Commit 01d1670

Browse files
committed
---
yaml --- r: 160639 b: refs/heads/master c: 54c628c h: refs/heads/master i: 160637: 891ad0e 160635: 3ad9bf8 160631: 3f5c527 160623: 60fa724 160607: 50d00ce 160575: 17c830b 160511: 6ac63fa v: v3
1 parent c43e260 commit 01d1670

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2048
-1245
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: bbe1a9b9c18d6dce6140ef16cbc9c29ad586564d
2+
refs/heads/master: 54c628cb849ad53b66f0d738dc8c83529a9d08d2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: c9f6d696420107f82304b992cf623b806995fe18
55
refs/heads/try: 225de0d60f8ca8dcc62ab2fd8818ebbda4b58cfe

trunk/src/libcollections/binary_heap.rs

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ use core::mem::{zeroed, replace, swap};
160160
use core::ptr;
161161

162162
use slice;
163-
use vec::Vec;
164-
165-
// FIXME(conventions): implement into_iter
163+
use vec::{mod, Vec};
166164

167165
/// A priority queue implemented with a binary heap.
168166
///
@@ -243,6 +241,27 @@ impl<T: Ord> BinaryHeap<T> {
243241
Items { iter: self.data.iter() }
244242
}
245243

244+
/// Creates a consuming iterator, that is, one that moves each value out of
245+
/// the binary heap in arbitrary order. The binary heap cannot be used
246+
/// after calling this.
247+
///
248+
/// # Example
249+
///
250+
/// ```
251+
/// use std::collections::BinaryHeap;
252+
/// let pq = BinaryHeap::from_vec(vec![1i, 2, 3, 4]);
253+
///
254+
/// // Print 1, 2, 3, 4 in arbitrary order
255+
/// for x in pq.into_iter() {
256+
/// // x has type int, not &int
257+
/// println!("{}", x);
258+
/// }
259+
/// ```
260+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
261+
pub fn into_iter(self) -> MoveItems<T> {
262+
MoveItems { iter: self.data.into_iter() }
263+
}
264+
246265
/// Returns the greatest item in a queue, or `None` if it is empty.
247266
///
248267
/// # Example
@@ -548,6 +567,26 @@ impl<'a, T> Iterator<&'a T> for Items<'a, T> {
548567
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
549568
}
550569

570+
/// An iterator that moves out of a `BinaryHeap`.
571+
pub struct MoveItems<T> {
572+
iter: vec::MoveItems<T>,
573+
}
574+
575+
impl<T> Iterator<T> for MoveItems<T> {
576+
#[inline]
577+
fn next(&mut self) -> Option<T> { self.iter.next() }
578+
579+
#[inline]
580+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
581+
}
582+
583+
impl<T> DoubleEndedIterator<T> for MoveItems<T> {
584+
#[inline]
585+
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
586+
}
587+
588+
impl<T> ExactSize<T> for MoveItems<T> {}
589+
551590
impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
552591
fn from_iter<Iter: Iterator<T>>(mut iter: Iter) -> BinaryHeap<T> {
553592
let vec: Vec<T> = iter.collect();
@@ -586,6 +625,43 @@ mod tests {
586625
}
587626
}
588627

628+
#[test]
629+
fn test_move_iter() {
630+
let data = vec!(5i, 9, 3);
631+
let iterout = vec!(9i, 5, 3);
632+
let pq = BinaryHeap::from_vec(data);
633+
634+
let v: Vec<int> = pq.into_iter().collect();
635+
assert_eq!(v, iterout);
636+
}
637+
638+
#[test]
639+
fn test_move_iter_size_hint() {
640+
let data = vec!(5i, 9);
641+
let pq = BinaryHeap::from_vec(data);
642+
643+
let mut it = pq.into_iter();
644+
645+
assert_eq!(it.size_hint(), (2, Some(2)));
646+
assert_eq!(it.next(), Some(9i));
647+
648+
assert_eq!(it.size_hint(), (1, Some(1)));
649+
assert_eq!(it.next(), Some(5i));
650+
651+
assert_eq!(it.size_hint(), (0, Some(0)));
652+
assert_eq!(it.next(), None);
653+
}
654+
655+
#[test]
656+
fn test_move_iter_reverse() {
657+
let data = vec!(5i, 9, 3);
658+
let iterout = vec!(3i, 5, 9);
659+
let pq = BinaryHeap::from_vec(data);
660+
661+
let v: Vec<int> = pq.into_iter().rev().collect();
662+
assert_eq!(v, iterout);
663+
}
664+
589665
#[test]
590666
fn test_top_and_pop() {
591667
let data = vec!(2u, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1);

trunk/src/libcollections/str.rs

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl<S: Str> StrVector for [S] {
163163
}
164164
}
165165

166-
impl<S: Str> StrVector for Vec<S> {
166+
impl<S: Str, T: AsSlice<S>> StrVector for T {
167167
#[inline]
168168
fn concat(&self) -> String {
169169
self.as_slice().concat()
@@ -929,54 +929,93 @@ mod tests {
929929
assert_eq!("ะเทศไท", "ประเทศไทย中华Việt Nam".slice_chars(2, 8));
930930
}
931931

932-
#[test]
933-
fn test_concat() {
934-
fn t(v: &[String], s: &str) {
935-
assert_eq!(v.concat().as_slice(), s);
932+
struct S {
933+
x: [String, .. 2]
934+
}
935+
936+
impl AsSlice<String> for S {
937+
fn as_slice<'a> (&'a self) -> &'a [String] {
938+
&self.x
939+
}
940+
}
941+
942+
fn s(x: &str) -> String { x.into_string() }
943+
944+
macro_rules! test_concat {
945+
($expected: expr, $string: expr) => {
946+
{
947+
let s = $string.concat();
948+
assert_eq!($expected, s.as_slice());
949+
}
936950
}
937-
t(&[String::from_str("you"), String::from_str("know"),
938-
String::from_str("I'm"),
939-
String::from_str("no"), String::from_str("good")],
940-
"youknowI'mnogood");
941-
let v: &[String] = &[];
942-
t(v, "");
943-
t(&[String::from_str("hi")], "hi");
944951
}
945952

946953
#[test]
947-
fn test_connect() {
948-
fn t(v: &[String], sep: &str, s: &str) {
949-
assert_eq!(v.connect(sep).as_slice(), s);
954+
fn test_concat_for_different_types() {
955+
test_concat!("ab", ["a", "b"]);
956+
test_concat!("ab", [s("a"), s("b")]);
957+
test_concat!("ab", vec!["a", "b"]);
958+
test_concat!("ab", vec!["a", "b"].as_slice());
959+
test_concat!("ab", vec![s("a"), s("b")]);
960+
961+
let mut v0 = ["a", "b"];
962+
let mut v1 = [s("a"), s("b")];
963+
unsafe {
964+
use std::c_vec::CVec;
965+
966+
test_concat!("ab", CVec::new(v0.as_mut_ptr(), v0.len()));
967+
test_concat!("ab", CVec::new(v1.as_mut_ptr(), v1.len()));
950968
}
951-
t(&[String::from_str("you"), String::from_str("know"),
952-
String::from_str("I'm"),
953-
String::from_str("no"), String::from_str("good")],
954-
" ", "you know I'm no good");
955-
let v: &[String] = &[];
956-
t(v, " ", "");
957-
t(&[String::from_str("hi")], " ", "hi");
969+
970+
test_concat!("ab", S { x: [s("a"), s("b")] });
958971
}
959972

960973
#[test]
961-
fn test_concat_slices() {
962-
fn t(v: &[&str], s: &str) {
963-
assert_eq!(v.concat().as_slice(), s);
974+
fn test_concat_for_different_lengths() {
975+
let empty: &[&str] = &[];
976+
test_concat!("", empty);
977+
test_concat!("a", ["a"]);
978+
test_concat!("ab", ["a", "b"]);
979+
test_concat!("abc", ["", "a", "bc"]);
980+
}
981+
982+
macro_rules! test_connect {
983+
($expected: expr, $string: expr, $delim: expr) => {
984+
{
985+
let s = $string.connect($delim);
986+
assert_eq!($expected, s.as_slice());
987+
}
964988
}
965-
t(&["you", "know", "I'm", "no", "good"], "youknowI'mnogood");
966-
let v: &[&str] = &[];
967-
t(v, "");
968-
t(&["hi"], "hi");
969989
}
970990

971991
#[test]
972-
fn test_connect_slices() {
973-
fn t(v: &[&str], sep: &str, s: &str) {
974-
assert_eq!(v.connect(sep).as_slice(), s);
992+
fn test_connect_for_different_types() {
993+
test_connect!("a-b", ["a", "b"], "-");
994+
let hyphen = "-".into_string();
995+
test_connect!("a-b", [s("a"), s("b")], hyphen.as_slice());
996+
test_connect!("a-b", vec!["a", "b"], hyphen.as_slice());
997+
test_connect!("a-b", vec!["a", "b"].as_slice(), "-");
998+
test_connect!("a-b", vec![s("a"), s("b")], "-");
999+
1000+
let mut v0 = ["a", "b"];
1001+
let mut v1 = [s("a"), s("b")];
1002+
unsafe {
1003+
use std::c_vec::CVec;
1004+
1005+
test_connect!("a-b", CVec::new(v0.as_mut_ptr(), v0.len()), "-");
1006+
test_connect!("a-b", CVec::new(v1.as_mut_ptr(), v1.len()), hyphen.as_slice());
9751007
}
976-
t(&["you", "know", "I'm", "no", "good"],
977-
" ", "you know I'm no good");
978-
t(&[], " ", "");
979-
t(&["hi"], " ", "hi");
1008+
1009+
test_connect!("a-b", S { x: [s("a"), s("b")] }, "-");
1010+
}
1011+
1012+
#[test]
1013+
fn test_connect_for_different_lengths() {
1014+
let empty: &[&str] = &[];
1015+
test_connect!("", empty, "-");
1016+
test_connect!("a", ["a"], "-");
1017+
test_connect!("a-b", ["a", "b"], "-");
1018+
test_connect!("-a-bc", ["", "a", "bc"], "-");
9801019
}
9811020

9821021
#[test]

trunk/src/libcore/any.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ use intrinsics::TypeId;
8888
#[stable]
8989
pub trait Any: 'static {
9090
/// Get the `TypeId` of `self`
91-
#[stable]
91+
#[experimental = "this method will likely be replaced by an associated static"]
9292
fn get_type_id(&self) -> TypeId;
9393
}
9494

trunk/src/libcore/str.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,14 +1277,19 @@ pub mod traits {
12771277
}
12781278

12791279
/// Any string that can be represented as a slice
1280-
pub trait Str {
1280+
pub trait Str for Sized? {
12811281
/// Work with `self` as a slice.
12821282
fn as_slice<'a>(&'a self) -> &'a str;
12831283
}
12841284

1285-
impl<'a> Str for &'a str {
1285+
impl Str for str {
12861286
#[inline]
1287-
fn as_slice<'a>(&'a self) -> &'a str { *self }
1287+
fn as_slice<'a>(&'a self) -> &'a str { self }
1288+
}
1289+
1290+
impl<'a, Sized? S> Str for &'a S where S: Str {
1291+
#[inline]
1292+
fn as_slice(&self) -> &str { Str::as_slice(*self) }
12881293
}
12891294

12901295
/// Methods for string slices

trunk/src/liblog/lib.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171

172172
extern crate regex;
173173

174-
use regex::Regex;
174+
use std::cell::RefCell;
175175
use std::fmt;
176176
use std::io::LineBufferedWriter;
177177
use std::io;
@@ -181,6 +181,8 @@ use std::rt;
181181
use std::slice;
182182
use std::sync::{Once, ONCE_INIT};
183183

184+
use regex::Regex;
185+
184186
use directive::LOG_LEVEL_NAMES;
185187

186188
pub mod macros;
@@ -213,7 +215,9 @@ pub const WARN: u32 = 2;
213215
/// Error log level
214216
pub const ERROR: u32 = 1;
215217

216-
local_data_key!(local_logger: Box<Logger + Send>)
218+
thread_local!(static LOCAL_LOGGER: RefCell<Option<Box<Logger + Send>>> = {
219+
RefCell::new(None)
220+
})
217221

218222
/// A trait used to represent an interface to a task-local logger. Each task
219223
/// can have its own custom logger which can respond to logging messages
@@ -283,7 +287,9 @@ pub fn log(level: u32, loc: &'static LogLocation, args: &fmt::Arguments) {
283287
// Completely remove the local logger from TLS in case anyone attempts to
284288
// frob the slot while we're doing the logging. This will destroy any logger
285289
// set during logging.
286-
let mut logger = local_logger.replace(None).unwrap_or_else(|| {
290+
let mut logger = LOCAL_LOGGER.with(|s| {
291+
s.borrow_mut().take()
292+
}).unwrap_or_else(|| {
287293
box DefaultLogger { handle: io::stderr() } as Box<Logger + Send>
288294
});
289295
logger.log(&LogRecord {
@@ -293,7 +299,7 @@ pub fn log(level: u32, loc: &'static LogLocation, args: &fmt::Arguments) {
293299
module_path: loc.module_path,
294300
line: loc.line,
295301
});
296-
local_logger.replace(Some(logger));
302+
set_logger(logger);
297303
}
298304

299305
/// Getter for the global log level. This is a function so that it can be called
@@ -305,7 +311,10 @@ pub fn log_level() -> u32 { unsafe { LOG_LEVEL } }
305311
/// Replaces the task-local logger with the specified logger, returning the old
306312
/// logger.
307313
pub fn set_logger(logger: Box<Logger + Send>) -> Option<Box<Logger + Send>> {
308-
local_logger.replace(Some(logger))
314+
let mut l = Some(logger);
315+
LOCAL_LOGGER.with(|slot| {
316+
mem::replace(&mut *slot.borrow_mut(), l.take())
317+
})
309318
}
310319

311320
/// A LogRecord is created by the logging macros, and passed as the only

trunk/src/librustc/util/common.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#![allow(non_camel_case_types)]
1212

13-
use std::cell::RefCell;
13+
use std::cell::{RefCell, Cell};
1414
use std::collections::HashMap;
1515
use std::fmt::Show;
1616
use std::hash::{Hash, Hasher};
@@ -26,11 +26,14 @@ use syntax::visit::Visitor;
2626
pub struct ErrorReported;
2727

2828
pub fn time<T, U>(do_it: bool, what: &str, u: U, f: |U| -> T) -> T {
29-
local_data_key!(depth: uint);
29+
thread_local!(static DEPTH: Cell<uint> = Cell::new(0));
3030
if !do_it { return f(u); }
3131

32-
let old = depth.get().map(|d| *d).unwrap_or(0);
33-
depth.replace(Some(old + 1));
32+
let old = DEPTH.with(|slot| {
33+
let r = slot.get();
34+
slot.set(r + 1);
35+
r
36+
});
3437

3538
let mut u = Some(u);
3639
let mut rv = None;
@@ -41,7 +44,7 @@ pub fn time<T, U>(do_it: bool, what: &str, u: U, f: |U| -> T) -> T {
4144

4245
println!("{}time: {}.{:03} \t{}", " ".repeat(old),
4346
dur.num_seconds(), dur.num_milliseconds() % 1000, what);
44-
depth.replace(Some(old));
47+
DEPTH.with(|slot| slot.set(old));
4548

4649
rv
4750
}

0 commit comments

Comments
 (0)