Skip to content

Commit 8ee94d6

Browse files
committed
---
yaml --- r: 207926 b: refs/heads/snap-stage3 c: 82158c9 h: refs/heads/master v: v3
1 parent 435fae8 commit 8ee94d6

File tree

22 files changed

+232
-262
lines changed

22 files changed

+232
-262
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 38a97becdf3e6a6157f6f7ec2d98ade8d8edc193
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: f2a19b39fbb2c7fb7c6f0f52e7f65e6ed8bddec4
4+
refs/heads/snap-stage3: 82158c9d1cc97d03b7e3a48fda0ff8c1e9209041
55
refs/heads/try: 7b4ef47b7805a402d756fb8157101f64880a522f
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d

branches/snap-stage3/src/libcollections/string.rs

Lines changed: 119 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use rustc_unicode::str as unicode_str;
2626
use rustc_unicode::str::Utf16Item;
2727

2828
use borrow::{Cow, IntoCow};
29-
use str::{self, FromStr, Utf8Error};
29+
use range::RangeArgument;
30+
use str::{self, FromStr, Utf8Error, Chars};
3031
use vec::{DerefVec, Vec, as_vec};
3132

3233
/// A growable string stored as a UTF-8 encoded buffer.
@@ -695,6 +696,59 @@ impl String {
695696
pub fn clear(&mut self) {
696697
self.vec.clear()
697698
}
699+
700+
/// Create a draining iterator that removes the specified range in the string
701+
/// and yields the removed chars from start to end. The element range is
702+
/// removed even if the iterator is not consumed until the end.
703+
///
704+
/// # Panics
705+
///
706+
/// Panics if the starting point or end point are not on character boundaries,
707+
/// or if they are out of bounds.
708+
///
709+
/// # Examples
710+
///
711+
/// ```
712+
/// # #![feature(collections_drain)]
713+
///
714+
/// let mut s = String::from("α is alpha, β is beta");
715+
/// let beta_offset = s.find('β').unwrap_or(s.len());
716+
///
717+
/// // Remove the range up until the β from the string
718+
/// let t: String = s.drain(..beta_offset).collect();
719+
/// assert_eq!(t, "α is alpha, ");
720+
/// assert_eq!(s, "β is beta");
721+
///
722+
/// // A full range clears the string
723+
/// s.drain(..);
724+
/// assert_eq!(s, "");
725+
/// ```
726+
#[unstable(feature = "collections_drain",
727+
reason = "recently added, matches RFC")]
728+
pub fn drain<R>(&mut self, range: R) -> Drain where R: RangeArgument<usize> {
729+
// Memory safety
730+
//
731+
// The String version of Drain does not have the memory safety issues
732+
// of the vector version. The data is just plain bytes.
733+
// Because the range removal happens in Drop, if the Drain iterator is leaked,
734+
// the removal will not happen.
735+
let len = self.len();
736+
let start = *range.start().unwrap_or(&0);
737+
let end = *range.end().unwrap_or(&len);
738+
739+
// Take out two simultaneous borrows. The &mut String won't be accessed
740+
// until iteration is over, in Drop.
741+
let self_ptr = self as *mut _;
742+
// slicing does the appropriate bounds checks
743+
let chars_iter = self[start..end].chars();
744+
745+
Drain {
746+
start: start,
747+
end: end,
748+
iter: chars_iter,
749+
string: self_ptr,
750+
}
751+
}
698752
}
699753

700754
impl FromUtf8Error {
@@ -740,8 +794,7 @@ impl<'a> FromIterator<&'a str> for String {
740794
}
741795
}
742796

743-
#[unstable(feature = "collections",
744-
reason = "waiting on Extend stabilization")]
797+
#[stable(feature = "rust1", since = "1.0.0")]
745798
impl Extend<char> for String {
746799
fn extend<I: IntoIterator<Item=char>>(&mut self, iterable: I) {
747800
let iterator = iterable.into_iter();
@@ -753,8 +806,7 @@ impl Extend<char> for String {
753806
}
754807
}
755808

756-
#[unstable(feature = "collections",
757-
reason = "waiting on Extend stabilization")]
809+
#[stable(feature = "rust1", since = "1.0.0")]
758810
impl<'a> Extend<&'a str> for String {
759811
fn extend<I: IntoIterator<Item=&'a str>>(&mut self, iterable: I) {
760812
let iterator = iterable.into_iter();
@@ -869,8 +921,7 @@ impl hash::Hash for String {
869921
}
870922
}
871923

872-
#[unstable(feature = "collections",
873-
reason = "recent addition, needs more experience")]
924+
#[stable(feature = "rust1", since = "1.0.0")]
874925
impl<'a> Add<&'a str> for String {
875926
type Output = String;
876927

@@ -964,11 +1015,17 @@ pub fn as_string<'a>(x: &'a str) -> DerefString<'a> {
9641015
DerefString { x: as_vec(x.as_bytes()) }
9651016
}
9661017

967-
#[unstable(feature = "collections", reason = "associated error type may change")]
1018+
/// Error returned from `String::from_str`
1019+
#[unstable(feature = "str_parse_error", reason = "may want to be replaced with \
1020+
Void if it ever exists")]
1021+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
1022+
pub struct ParseError(());
1023+
1024+
#[stable(feature = "rust1", since = "1.0.0")]
9681025
impl FromStr for String {
969-
type Err = ();
1026+
type Err = ParseError;
9701027
#[inline]
971-
fn from_str(s: &str) -> Result<String, ()> {
1028+
fn from_str(s: &str) -> Result<String, ParseError> {
9721029
Ok(String::from_str(s))
9731030
}
9741031
}
@@ -1072,3 +1129,55 @@ impl fmt::Write for String {
10721129
Ok(())
10731130
}
10741131
}
1132+
1133+
/// A draining iterator for `String`.
1134+
#[unstable(feature = "collections_drain", reason = "recently added")]
1135+
pub struct Drain<'a> {
1136+
/// Will be used as &'a mut String in the destructor
1137+
string: *mut String,
1138+
/// Start of part to remove
1139+
start: usize,
1140+
/// End of part to remove
1141+
end: usize,
1142+
/// Current remaining range to remove
1143+
iter: Chars<'a>,
1144+
}
1145+
1146+
unsafe impl<'a> Sync for Drain<'a> {}
1147+
unsafe impl<'a> Send for Drain<'a> {}
1148+
1149+
#[unstable(feature = "collections_drain", reason = "recently added")]
1150+
impl<'a> Drop for Drain<'a> {
1151+
fn drop(&mut self) {
1152+
unsafe {
1153+
// Use Vec::drain. "Reaffirm" the bounds checks to avoid
1154+
// panic code being inserted again.
1155+
let self_vec = (*self.string).as_mut_vec();
1156+
if self.start <= self.end && self.end <= self_vec.len() {
1157+
self_vec.drain(self.start..self.end);
1158+
}
1159+
}
1160+
}
1161+
}
1162+
1163+
#[unstable(feature = "collections_drain", reason = "recently added")]
1164+
impl<'a> Iterator for Drain<'a> {
1165+
type Item = char;
1166+
1167+
#[inline]
1168+
fn next(&mut self) -> Option<char> {
1169+
self.iter.next()
1170+
}
1171+
1172+
fn size_hint(&self) -> (usize, Option<usize>) {
1173+
self.iter.size_hint()
1174+
}
1175+
}
1176+
1177+
#[unstable(feature = "collections_drain", reason = "recently added")]
1178+
impl<'a> DoubleEndedIterator for Drain<'a> {
1179+
#[inline]
1180+
fn next_back(&mut self) -> Option<char> {
1181+
self.iter.next_back()
1182+
}
1183+
}

branches/snap-stage3/src/libcollections/vec.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> {
12991299
// Common trait implementations for Vec
13001300
////////////////////////////////////////////////////////////////////////////////
13011301

1302-
#[unstable(feature = "collections")]
1302+
#[stable(feature = "rust1", since = "1.0.0")]
13031303
impl<T:Clone> Clone for Vec<T> {
13041304
#[cfg(not(test))]
13051305
fn clone(&self) -> Vec<T> { <[T]>::to_vec(&**self) }
@@ -1554,7 +1554,7 @@ impl<'a, T> IntoIterator for &'a mut Vec<T> {
15541554
}
15551555
}
15561556

1557-
#[unstable(feature = "collections", reason = "waiting on Extend stability")]
1557+
#[stable(feature = "rust1", since = "1.0.0")]
15581558
impl<T> Extend<T> for Vec<T> {
15591559
#[inline]
15601560
fn extend<I: IntoIterator<Item=T>>(&mut self, iterable: I) {
@@ -1614,8 +1614,7 @@ impl<T: Ord> Ord for Vec<T> {
16141614
}
16151615
}
16161616

1617-
#[unstable(feature = "collections",
1618-
reason = "recent addition, needs more experience")]
1617+
#[stable(feature = "rust1", since = "1.0.0")]
16191618
impl<'a, T: Clone> Add<&'a [T]> for Vec<T> {
16201619
type Output = Vec<T>;
16211620

@@ -1694,7 +1693,7 @@ impl<'a> From<&'a str> for Vec<u8> {
16941693
// Clone-on-write
16951694
////////////////////////////////////////////////////////////////////////////////
16961695

1697-
#[unstable(feature = "collections")]
1696+
#[stable(feature = "rust1", since = "1.0.0")]
16981697
impl<'a, T> FromIterator<T> for Cow<'a, [T]> where T: Clone {
16991698
fn from_iter<I: IntoIterator<Item=T>>(it: I) -> Cow<'a, [T]> {
17001699
Cow::Owned(FromIterator::from_iter(it))

branches/snap-stage3/src/libcollectionstest/string.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,23 @@ fn test_from_iterator() {
348348
assert_eq!(s, d);
349349
}
350350

351+
#[test]
352+
fn test_drain() {
353+
let mut s = String::from("αβγ");
354+
assert_eq!(s.drain(2..4).collect::<String>(), "β");
355+
assert_eq!(s, "αγ");
356+
357+
let mut t = String::from("abcd");
358+
t.drain(..0);
359+
assert_eq!(t, "abcd");
360+
t.drain(..1);
361+
assert_eq!(t, "bcd");
362+
t.drain(3..);
363+
assert_eq!(t, "bcd");
364+
t.drain(..);
365+
assert_eq!(t, "");
366+
}
367+
351368
#[bench]
352369
fn bench_with_capacity(b: &mut Bencher) {
353370
b.iter(|| {

branches/snap-stage3/src/libcore/iter.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,10 @@ pub trait Iterator {
626626
/// # Examples
627627
///
628628
/// ```
629-
/// # #![feature(core)]
630629
/// let a = [1, 2, 3, 4, 5];
631630
/// let mut it = a.iter();
632631
/// assert!(it.any(|x| *x == 3));
633-
/// assert_eq!(&it[..], [4, 5]);
634-
///
632+
/// assert_eq!(it.collect::<Vec<_>>(), [&4, &5]);
635633
/// ```
636634
#[inline]
637635
#[stable(feature = "rust1", since = "1.0.0")]
@@ -654,11 +652,10 @@ pub trait Iterator {
654652
/// # Examples
655653
///
656654
/// ```
657-
/// # #![feature(core)]
658655
/// let a = [1, 2, 3, 4, 5];
659656
/// let mut it = a.iter();
660657
/// assert_eq!(it.find(|&x| *x == 3).unwrap(), &3);
661-
/// assert_eq!(&it[..], [4, 5]);
658+
/// assert_eq!(it.collect::<Vec<_>>(), [&4, &5]);
662659
#[inline]
663660
#[stable(feature = "rust1", since = "1.0.0")]
664661
fn find<P>(&mut self, mut predicate: P) -> Option<Self::Item> where
@@ -678,11 +675,10 @@ pub trait Iterator {
678675
/// # Examples
679676
///
680677
/// ```
681-
/// # #![feature(core)]
682678
/// let a = [1, 2, 3, 4, 5];
683679
/// let mut it = a.iter();
684680
/// assert_eq!(it.position(|x| *x == 3).unwrap(), 2);
685-
/// assert_eq!(&it[..], [4, 5]);
681+
/// assert_eq!(it.collect::<Vec<_>>(), [&4, &5]);
686682
#[inline]
687683
#[stable(feature = "rust1", since = "1.0.0")]
688684
fn position<P>(&mut self, mut predicate: P) -> Option<usize> where
@@ -708,11 +704,10 @@ pub trait Iterator {
708704
/// # Examples
709705
///
710706
/// ```
711-
/// # #![feature(core)]
712707
/// let a = [1, 2, 2, 4, 5];
713708
/// let mut it = a.iter();
714709
/// assert_eq!(it.rposition(|x| *x == 2).unwrap(), 2);
715-
/// assert_eq!(&it[..], [1, 2]);
710+
/// assert_eq!(it.collect::<Vec<_>>(), [&1, &2]);
716711
#[inline]
717712
#[stable(feature = "rust1", since = "1.0.0")]
718713
fn rposition<P>(&mut self, mut predicate: P) -> Option<usize> where

branches/snap-stage3/src/libcore/num/float_macros.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ macro_rules! from_str_radix_float_impl {
3535
}
3636

3737
let (is_positive, src) = match src.slice_shift_char() {
38-
None => return Err(PFE { kind: Empty }),
39-
Some(('-', "")) => return Err(PFE { kind: Empty }),
38+
None => return Err(PFE { __kind: Empty }),
39+
Some(('-', "")) => return Err(PFE { __kind: Empty }),
4040
Some(('-', src)) => (false, src),
4141
Some((_, _)) => (true, src),
4242
};
@@ -88,7 +88,7 @@ macro_rules! from_str_radix_float_impl {
8888
break; // start of fractional part
8989
},
9090
_ => {
91-
return Err(PFE { kind: Invalid });
91+
return Err(PFE { __kind: Invalid });
9292
},
9393
},
9494
}
@@ -122,7 +122,7 @@ macro_rules! from_str_radix_float_impl {
122122
break; // start of exponent
123123
},
124124
_ => {
125-
return Err(PFE { kind: Invalid });
125+
return Err(PFE { __kind: Invalid });
126126
},
127127
},
128128
}
@@ -135,7 +135,7 @@ macro_rules! from_str_radix_float_impl {
135135
let base = match c {
136136
'E' | 'e' if radix == 10 => 10.0,
137137
'P' | 'p' if radix == 16 => 2.0,
138-
_ => return Err(PFE { kind: Invalid }),
138+
_ => return Err(PFE { __kind: Invalid }),
139139
};
140140

141141
// Parse the exponent as decimal integer
@@ -144,13 +144,13 @@ macro_rules! from_str_radix_float_impl {
144144
Some(('-', src)) => (false, src.parse::<usize>()),
145145
Some(('+', src)) => (true, src.parse::<usize>()),
146146
Some((_, _)) => (true, src.parse::<usize>()),
147-
None => return Err(PFE { kind: Invalid }),
147+
None => return Err(PFE { __kind: Invalid }),
148148
};
149149

150150
match (is_positive, exp) {
151151
(true, Ok(exp)) => base.powi(exp as i32),
152152
(false, Ok(exp)) => 1.0 / base.powi(exp as i32),
153-
(_, Err(_)) => return Err(PFE { kind: Invalid }),
153+
(_, Err(_)) => return Err(PFE { __kind: Invalid }),
154154
}
155155
},
156156
None => 1.0, // no exponent

branches/snap-stage3/src/libcore/num/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,11 @@ impl fmt::Display for ParseIntError {
15241524

15251525
/// An error which can be returned when parsing a float.
15261526
#[derive(Debug, Clone, PartialEq)]
1527-
pub struct ParseFloatError { pub kind: FloatErrorKind }
1527+
#[stable(feature = "rust1", since = "1.0.0")]
1528+
pub struct ParseFloatError {
1529+
#[doc(hidden)]
1530+
pub __kind: FloatErrorKind
1531+
}
15281532

15291533
#[derive(Debug, Clone, PartialEq)]
15301534
pub enum FloatErrorKind {
@@ -1533,9 +1537,9 @@ pub enum FloatErrorKind {
15331537
}
15341538

15351539
impl ParseFloatError {
1536-
#[unstable(feature = "core", reason = "available through Error trait")]
1537-
pub fn description(&self) -> &str {
1538-
match self.kind {
1540+
#[doc(hidden)]
1541+
pub fn __description(&self) -> &str {
1542+
match self.__kind {
15391543
FloatErrorKind::Empty => "cannot parse float from empty string",
15401544
FloatErrorKind::Invalid => "invalid float literal",
15411545
}
@@ -1545,6 +1549,6 @@ impl ParseFloatError {
15451549
#[stable(feature = "rust1", since = "1.0.0")]
15461550
impl fmt::Display for ParseFloatError {
15471551
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1548-
self.description().fmt(f)
1552+
self.__description().fmt(f)
15491553
}
15501554
}

0 commit comments

Comments
 (0)