Skip to content

Commit 3b0550c

Browse files
committed
Rename slicing methods
1 parent cd21e4a commit 3b0550c

File tree

11 files changed

+225
-24
lines changed

11 files changed

+225
-24
lines changed

src/libcollections/string.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,7 @@ impl<S: Str> Add<S, String> for String {
928928
}
929929
}
930930

931+
#[cfg(stage0)]
931932
impl ops::Slice<uint, str> for String {
932933
#[inline]
933934
fn as_slice_<'a>(&'a self) -> &'a str {
@@ -949,6 +950,28 @@ impl ops::Slice<uint, str> for String {
949950
self[][*from..*to]
950951
}
951952
}
953+
#[cfg(not(stage0))]
954+
impl ops::Slice<uint, str> for String {
955+
#[inline]
956+
fn as_slice_<'a>(&'a self) -> &'a str {
957+
self.as_slice()
958+
}
959+
960+
#[inline]
961+
fn slice_from_or_fail<'a>(&'a self, from: &uint) -> &'a str {
962+
self[][*from..]
963+
}
964+
965+
#[inline]
966+
fn slice_to_or_fail<'a>(&'a self, to: &uint) -> &'a str {
967+
self[][..*to]
968+
}
969+
970+
#[inline]
971+
fn slice_or_fail<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
972+
self[][*from..*to]
973+
}
974+
}
952975

953976
/// Unsafe operations
954977
#[unstable = "waiting on raw module conventions"]

src/libcollections/trie.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,22 @@ macro_rules! bound {
389389

390390
impl<T> TrieMap<T> {
391391
// If `upper` is true then returns upper_bound else returns lower_bound.
392+
#[cfg(stage0)]
392393
#[inline]
393394
fn bound<'a>(&'a self, key: uint, upper: bool) -> Entries<'a, T> {
394395
bound!(Entries, self = self,
395396
key = key, is_upper = upper,
396397
slice_from = slice_from_, iter = iter,
397398
mutability = )
398399
}
400+
#[cfg(not(stage0))]
401+
#[inline]
402+
fn bound<'a>(&'a self, key: uint, upper: bool) -> Entries<'a, T> {
403+
bound!(Entries, self = self,
404+
key = key, is_upper = upper,
405+
slice_from = slice_from_or_fail, iter = iter,
406+
mutability = )
407+
}
399408

400409
/// Gets an iterator pointing to the first key-value pair whose key is not less than `key`.
401410
/// If all keys in the map are less than `key` an empty iterator is returned.
@@ -431,13 +440,22 @@ impl<T> TrieMap<T> {
431440
self.bound(key, true)
432441
}
433442
// If `upper` is true then returns upper_bound else returns lower_bound.
443+
#[cfg(stage0)]
434444
#[inline]
435445
fn bound_mut<'a>(&'a mut self, key: uint, upper: bool) -> MutEntries<'a, T> {
436446
bound!(MutEntries, self = self,
437447
key = key, is_upper = upper,
438448
slice_from = slice_from_mut_, iter = iter_mut,
439449
mutability = mut)
440450
}
451+
#[cfg(not(stage0))]
452+
#[inline]
453+
fn bound_mut<'a>(&'a mut self, key: uint, upper: bool) -> MutEntries<'a, T> {
454+
bound!(MutEntries, self = self,
455+
key = key, is_upper = upper,
456+
slice_from = slice_from_or_fail_mut, iter = iter_mut,
457+
mutability = mut)
458+
}
441459

442460
/// Deprecated: use `lower_bound_mut`.
443461
#[deprecated = "use lower_bound_mut"]

src/libcollections/vec.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ impl<T> Index<uint,T> for Vec<T> {
460460
}
461461
}*/
462462

463+
#[cfg(stage0)]
463464
impl<T> ops::Slice<uint, [T]> for Vec<T> {
464465
#[inline]
465466
fn as_slice_<'a>(&'a self) -> &'a [T] {
@@ -480,7 +481,29 @@ impl<T> ops::Slice<uint, [T]> for Vec<T> {
480481
self.as_slice().slice_(start, end)
481482
}
482483
}
484+
#[cfg(not(stage0))]
485+
impl<T> ops::Slice<uint, [T]> for Vec<T> {
486+
#[inline]
487+
fn as_slice_<'a>(&'a self) -> &'a [T] {
488+
self.as_slice()
489+
}
490+
491+
#[inline]
492+
fn slice_from_or_fail<'a>(&'a self, start: &uint) -> &'a [T] {
493+
self.as_slice().slice_from_or_fail(start)
494+
}
483495

496+
#[inline]
497+
fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] {
498+
self.as_slice().slice_to_or_fail(end)
499+
}
500+
#[inline]
501+
fn slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
502+
self.as_slice().slice_or_fail(start, end)
503+
}
504+
}
505+
506+
#[cfg(stage0)]
484507
impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
485508
#[inline]
486509
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
@@ -501,6 +524,27 @@ impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
501524
self.as_mut_slice().slice_mut_(start, end)
502525
}
503526
}
527+
#[cfg(not(stage0))]
528+
impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
529+
#[inline]
530+
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
531+
self.as_mut_slice()
532+
}
533+
534+
#[inline]
535+
fn slice_from_or_fail_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] {
536+
self.as_mut_slice().slice_from_or_fail_mut(start)
537+
}
538+
539+
#[inline]
540+
fn slice_to_or_fail_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] {
541+
self.as_mut_slice().slice_to_or_fail_mut(end)
542+
}
543+
#[inline]
544+
fn slice_or_fail_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] {
545+
self.as_mut_slice().slice_or_fail_mut(start, end)
546+
}
547+
}
504548

505549
#[experimental = "waiting on FromIterator stability"]
506550
impl<T> FromIterator<T> for Vec<T> {
@@ -1181,7 +1225,7 @@ impl<T> Vec<T> {
11811225
}
11821226

11831227
/// Deprecated: use `slice_mut`.
1184-
#[deprecated = "use slice_from"]
1228+
#[deprecated = "use slice_mut"]
11851229
pub fn mut_slice<'a>(&'a mut self, start: uint, end: uint)
11861230
-> &'a mut [T] {
11871231
self[mut start..end]

src/libcore/ops.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,15 +692,15 @@ pub trait IndexMut<Index, Result> {
692692
* println!("Slicing!");
693693
* self
694694
* }
695-
* fn slice_from_<'a>(&'a self, from: &Foo) -> &'a Foo {
695+
* fn slice_from_or_fail<'a>(&'a self, from: &Foo) -> &'a Foo {
696696
* println!("Slicing!");
697697
* self
698698
* }
699-
* fn slice_to_<'a>(&'a self, to: &Foo) -> &'a Foo {
699+
* fn slice_to_or_fail<'a>(&'a self, to: &Foo) -> &'a Foo {
700700
* println!("Slicing!");
701701
* self
702702
* }
703-
* fn slice_<'a>(&'a self, from: &Foo, to: &Foo) -> &'a Foo {
703+
* fn slice_or_fail<'a>(&'a self, from: &Foo, to: &Foo) -> &'a Foo {
704704
* println!("Slicing!");
705705
* self
706706
* }
@@ -711,7 +711,22 @@ pub trait IndexMut<Index, Result> {
711711
* }
712712
* ```
713713
*/
714-
// FIXME(#17273) remove the postscript _s
714+
#[cfg(not(stage0))]
715+
#[lang="slice"]
716+
pub trait Slice<Idx, Sized? Result> for Sized? {
717+
/// The method for the slicing operation foo[]
718+
fn as_slice_<'a>(&'a self) -> &'a Result;
719+
/// The method for the slicing operation foo[from..]
720+
fn slice_from_or_fail<'a>(&'a self, from: &Idx) -> &'a Result;
721+
/// The method for the slicing operation foo[..to]
722+
fn slice_to_or_fail<'a>(&'a self, to: &Idx) -> &'a Result;
723+
/// The method for the slicing operation foo[from..to]
724+
fn slice_or_fail<'a>(&'a self, from: &Idx, to: &Idx) -> &'a Result;
725+
}
726+
#[cfg(stage0)]
727+
/**
728+
*
729+
*/
715730
#[lang="slice"]
716731
pub trait Slice<Idx, Sized? Result> for Sized? {
717732
/// The method for the slicing operation foo[]
@@ -742,15 +757,15 @@ pub trait Slice<Idx, Sized? Result> for Sized? {
742757
* println!("Slicing!");
743758
* self
744759
* }
745-
* fn slice_from_mut_<'a>(&'a mut self, from: &Foo) -> &'a mut Foo {
760+
* fn slice_from_or_fail_mut<'a>(&'a mut self, from: &Foo) -> &'a mut Foo {
746761
* println!("Slicing!");
747762
* self
748763
* }
749-
* fn slice_to_mut_<'a>(&'a mut self, to: &Foo) -> &'a mut Foo {
764+
* fn slice_to_or_fail_mut<'a>(&'a mut self, to: &Foo) -> &'a mut Foo {
750765
* println!("Slicing!");
751766
* self
752767
* }
753-
* fn slice_mut_<'a>(&'a mut self, from: &Foo, to: &Foo) -> &'a mut Foo {
768+
* fn slice_or_fail_mut<'a>(&'a mut self, from: &Foo, to: &Foo) -> &'a mut Foo {
754769
* println!("Slicing!");
755770
* self
756771
* }
@@ -761,7 +776,22 @@ pub trait Slice<Idx, Sized? Result> for Sized? {
761776
* }
762777
* ```
763778
*/
764-
// FIXME(#17273) remove the postscript _s
779+
#[cfg(not(stage0))]
780+
#[lang="slice_mut"]
781+
pub trait SliceMut<Idx, Sized? Result> for Sized? {
782+
/// The method for the slicing operation foo[]
783+
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Result;
784+
/// The method for the slicing operation foo[from..]
785+
fn slice_from_or_fail_mut<'a>(&'a mut self, from: &Idx) -> &'a mut Result;
786+
/// The method for the slicing operation foo[..to]
787+
fn slice_to_or_fail_mut<'a>(&'a mut self, to: &Idx) -> &'a mut Result;
788+
/// The method for the slicing operation foo[from..to]
789+
fn slice_or_fail_mut<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result;
790+
}
791+
#[cfg(stage0)]
792+
/**
793+
*
794+
*/
765795
#[lang="slice_mut"]
766796
pub trait SliceMut<Idx, Sized? Result> for Sized? {
767797
/// The method for the slicing operation foo[mut]

src/libcore/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub use ops::{BitAnd, BitOr, BitXor};
3535
pub use ops::{Drop, Deref, DerefMut};
3636
pub use ops::{Shl, Shr};
3737
pub use ops::{Index, IndexMut};
38+
pub use ops::{Slice, SliceMut};
3839
pub use ops::{Fn, FnMut, FnOnce};
3940
pub use option::{Option, Some, None};
4041
pub use result::{Result, Ok, Err};

src/libcore/slice.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,37 @@ impl<'a,T> ImmutableSlice<'a, T> for &'a [T] {
486486
}
487487
}
488488

489+
490+
491+
#[cfg(not(stage0))]
492+
impl<T> ops::Slice<uint, [T]> for [T] {
493+
#[inline]
494+
fn as_slice_<'a>(&'a self) -> &'a [T] {
495+
self
496+
}
497+
498+
#[inline]
499+
fn slice_from_or_fail<'a>(&'a self, start: &uint) -> &'a [T] {
500+
self.slice_or_fail(start, &self.len())
501+
}
502+
503+
#[inline]
504+
fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] {
505+
self.slice_or_fail(&0, end)
506+
}
507+
#[inline]
508+
fn slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
509+
assert!(*start <= *end);
510+
assert!(*end <= self.len());
511+
unsafe {
512+
transmute(RawSlice {
513+
data: self.as_ptr().offset(*start as int),
514+
len: (*end - *start)
515+
})
516+
}
517+
}
518+
}
519+
#[cfg(stage0)]
489520
impl<T> ops::Slice<uint, [T]> for [T] {
490521
#[inline]
491522
fn as_slice_<'a>(&'a self) -> &'a [T] {
@@ -514,6 +545,36 @@ impl<T> ops::Slice<uint, [T]> for [T] {
514545
}
515546
}
516547

548+
#[cfg(not(stage0))]
549+
impl<T> ops::SliceMut<uint, [T]> for [T] {
550+
#[inline]
551+
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
552+
self
553+
}
554+
555+
#[inline]
556+
fn slice_from_or_fail_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] {
557+
let len = &self.len();
558+
self.slice_or_fail_mut(start, len)
559+
}
560+
561+
#[inline]
562+
fn slice_to_or_fail_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] {
563+
self.slice_or_fail_mut(&0, end)
564+
}
565+
#[inline]
566+
fn slice_or_fail_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] {
567+
assert!(*start <= *end);
568+
assert!(*end <= self.len());
569+
unsafe {
570+
transmute(RawSlice {
571+
data: self.as_ptr().offset(*start as int),
572+
len: (*end - *start)
573+
})
574+
}
575+
}
576+
}
577+
#[cfg(stage0)]
517578
impl<T> ops::SliceMut<uint, [T]> for [T] {
518579
#[inline]
519580
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
@@ -556,7 +617,7 @@ pub trait MutableSlice<'a, T> {
556617
fn as_mut_slice(self) -> &'a mut [T];
557618

558619
/// Deprecated: use `slice_mut`.
559-
#[deprecated = "slice_mut"]
620+
#[deprecated = "use slice_mut"]
560621
fn mut_slice(self, start: uint, end: uint) -> &'a mut [T] {
561622
self.slice_mut(start, end)
562623
}

src/libcore/str.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,7 @@ pub mod traits {
11641164
fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
11651165
}
11661166

1167+
#[cfg(stage0)]
11671168
impl ops::Slice<uint, str> for str {
11681169
#[inline]
11691170
fn as_slice_<'a>(&'a self) -> &'a str {
@@ -1185,6 +1186,28 @@ pub mod traits {
11851186
self.slice(*from, *to)
11861187
}
11871188
}
1189+
#[cfg(not(stage0))]
1190+
impl ops::Slice<uint, str> for str {
1191+
#[inline]
1192+
fn as_slice_<'a>(&'a self) -> &'a str {
1193+
self
1194+
}
1195+
1196+
#[inline]
1197+
fn slice_from_or_fail<'a>(&'a self, from: &uint) -> &'a str {
1198+
self.slice_from(*from)
1199+
}
1200+
1201+
#[inline]
1202+
fn slice_to_or_fail<'a>(&'a self, to: &uint) -> &'a str {
1203+
self.slice_to(*to)
1204+
}
1205+
1206+
#[inline]
1207+
fn slice_or_fail<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
1208+
self.slice(*from, *to)
1209+
}
1210+
}
11881211
}
11891212

11901213
/// Any string that can be represented as a slice

src/libcoretest/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ fn test_all() {
373373
assert!(v.iter().all(|&x| x < 10));
374374
assert!(!v.iter().all(|&x| x % 2 == 0));
375375
assert!(!v.iter().all(|&x| x > 100));
376-
assert!(v.slice_(&0, &0).iter().all(|_| fail!()));
376+
assert!(v.slice_or_fail(&0, &0).iter().all(|_| fail!()));
377377
}
378378

379379
#[test]
@@ -382,7 +382,7 @@ fn test_any() {
382382
assert!(v.iter().any(|&x| x < 10));
383383
assert!(v.iter().any(|&x| x % 2 == 0));
384384
assert!(!v.iter().any(|&x| x > 100));
385-
assert!(!v.slice_(&0, &0).iter().any(|_| fail!()));
385+
assert!(!v.slice_or_fail(&0, &0).iter().any(|_| fail!()));
386386
}
387387

388388
#[test]

0 commit comments

Comments
 (0)