From f464132e7ed5e95ae88bbc25d90542a265c5f67a Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Thu, 25 Sep 2014 22:41:49 +1200 Subject: [PATCH] Remove the `_` suffix from slice methods. Deprecates slicing methods from ImmutableSlice/MutableSlice in favour of slicing syntax or the methods in Slice/SliceMut. Closes #17273. --- src/libcollections/trie.rs | 18 ++++ src/libcollections/vec.rs | 53 ++++++++++ src/libcore/ops.rs | 51 ++++++++-- src/libcore/slice.rs | 124 +++++++++++------------- src/libcoretest/iter.rs | 4 +- src/librustc/middle/typeck/check/mod.rs | 16 +-- src/test/run-pass/slice.rs | 16 +-- 7 files changed, 188 insertions(+), 94 deletions(-) diff --git a/src/libcollections/trie.rs b/src/libcollections/trie.rs index 04175173febe6..9bfc8e08d8d3c 100644 --- a/src/libcollections/trie.rs +++ b/src/libcollections/trie.rs @@ -389,6 +389,7 @@ macro_rules! bound { impl TrieMap { // If `upper` is true then returns upper_bound else returns lower_bound. + #[cfg(stage0)] #[inline] fn bound<'a>(&'a self, key: uint, upper: bool) -> Entries<'a, T> { bound!(Entries, self = self, @@ -396,6 +397,14 @@ impl TrieMap { slice_from = slice_from_, iter = iter, mutability = ) } + #[cfg(not(stage0))] + #[inline] + fn bound<'a>(&'a self, key: uint, upper: bool) -> Entries<'a, T> { + bound!(Entries, self = self, + key = key, is_upper = upper, + slice_from = slice_from, iter = iter, + mutability = ) + } /// Gets an iterator pointing to the first key-value pair whose key is not less than `key`. /// If all keys in the map are less than `key` an empty iterator is returned. @@ -431,6 +440,7 @@ impl TrieMap { self.bound(key, true) } // If `upper` is true then returns upper_bound else returns lower_bound. + #[cfg(stage0)] #[inline] fn bound_mut<'a>(&'a mut self, key: uint, upper: bool) -> MutEntries<'a, T> { bound!(MutEntries, self = self, @@ -438,6 +448,14 @@ impl TrieMap { slice_from = slice_from_mut_, iter = iter_mut, mutability = mut) } + #[cfg(not(stage0))] + #[inline] + fn bound_mut<'a>(&'a mut self, key: uint, upper: bool) -> MutEntries<'a, T> { + bound!(MutEntries, self = self, + key = key, is_upper = upper, + slice_from = slice_from_mut, iter = iter_mut, + mutability = mut) + } /// Deprecated: use `lower_bound_mut`. #[deprecated = "use lower_bound_mut"] diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index fec992056dc12..3eb0a0ccc1e89 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -23,6 +23,7 @@ use core::num; use core::ops; use core::ptr; use core::raw::Slice as RawSlice; +use core::slice::Slice as SliceSlice; use core::uint; use {Mutable, MutableSeq}; @@ -459,6 +460,36 @@ impl Index for Vec { } }*/ +// Annoying helper function because there are two Slice::as_slice functions in +// scope. +#[inline] +fn slice_to_slice<'a, T, U: Slice>(this: &'a U) -> &'a [T] { + this.as_slice() +} + + +#[cfg(not(stage0))] +impl ops::Slice for Vec { + #[inline] + fn as_slice<'a>(&'a self) -> &'a [T] { + slice_to_slice(self) + } + + #[inline] + fn slice_from<'a>(&'a self, start: &uint) -> &'a [T] { + slice_to_slice(self).slice_from(start) + } + + #[inline] + fn slice_to<'a>(&'a self, end: &uint) -> &'a [T] { + slice_to_slice(self).slice_to(end) + } + #[inline] + fn slice<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] { + slice_to_slice(self).slice(start, end) + } +} +#[cfg(stage0)] impl ops::Slice for Vec { #[inline] fn as_slice_<'a>(&'a self) -> &'a [T] { @@ -480,6 +511,28 @@ impl ops::Slice for Vec { } } +#[cfg(not(stage0))] +impl ops::SliceMut for Vec { + #[inline] + fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] { + self.as_mut_slice() + } + + #[inline] + fn slice_from_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] { + self.as_mut_slice().slice_from_mut(start) + } + + #[inline] + fn slice_to_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] { + self.as_mut_slice().slice_to_mut(end) + } + #[inline] + fn slice_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] { + self.as_mut_slice().slice_mut(start, end) + } +} +#[cfg(stage0)] impl ops::SliceMut for Vec { #[inline] fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] { diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index 718d311999552..15e5352f78298 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -688,19 +688,19 @@ pub trait IndexMut { * struct Foo; * * impl ::core::ops::Slice for Foo { - * fn as_slice_<'a>(&'a self) -> &'a Foo { + * fn as_slice<'a>(&'a self) -> &'a Foo { * println!("Slicing!"); * self * } - * fn slice_from_<'a>(&'a self, from: &Foo) -> &'a Foo { + * fn slice_from<'a>(&'a self, from: &Foo) -> &'a Foo { * println!("Slicing!"); * self * } - * fn slice_to_<'a>(&'a self, to: &Foo) -> &'a Foo { + * fn slice_to<'a>(&'a self, to: &Foo) -> &'a Foo { * println!("Slicing!"); * self * } - * fn slice_<'a>(&'a self, from: &Foo, to: &Foo) -> &'a Foo { + * fn slice<'a>(&'a self, from: &Foo, to: &Foo) -> &'a Foo { * println!("Slicing!"); * self * } @@ -711,7 +711,22 @@ pub trait IndexMut { * } * ``` */ -// FIXME(#17273) remove the postscript _s +#[cfg(not(stage0))] +#[lang="slice"] +pub trait Slice for Sized? { + /// The method for the slicing operation foo[] + fn as_slice<'a>(&'a self) -> &'a Result; + /// The method for the slicing operation foo[from..] + fn slice_from<'a>(&'a self, from: &Idx) -> &'a Result; + /// The method for the slicing operation foo[..to] + fn slice_to<'a>(&'a self, to: &Idx) -> &'a Result; + /// The method for the slicing operation foo[from..to] + fn slice<'a>(&'a self, from: &Idx, to: &Idx) -> &'a Result; +} +/** + * + */ +#[cfg(stage0)] #[lang="slice"] pub trait Slice for Sized? { /// The method for the slicing operation foo[] @@ -738,19 +753,19 @@ pub trait Slice for Sized? { * struct Foo; * * impl ::core::ops::SliceMut for Foo { - * fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo { + * fn as_mut_slice<'a>(&'a mut self) -> &'a mut Foo { * println!("Slicing!"); * self * } - * fn slice_from_mut_<'a>(&'a mut self, from: &Foo) -> &'a mut Foo { + * fn slice_from_mut<'a>(&'a mut self, from: &Foo) -> &'a mut Foo { * println!("Slicing!"); * self * } - * fn slice_to_mut_<'a>(&'a mut self, to: &Foo) -> &'a mut Foo { + * fn slice_to_mut<'a>(&'a mut self, to: &Foo) -> &'a mut Foo { * println!("Slicing!"); * self * } - * fn slice_mut_<'a>(&'a mut self, from: &Foo, to: &Foo) -> &'a mut Foo { + * fn slice_mut<'a>(&'a mut self, from: &Foo, to: &Foo) -> &'a mut Foo { * println!("Slicing!"); * self * } @@ -761,7 +776,22 @@ pub trait Slice for Sized? { * } * ``` */ -// FIXME(#17273) remove the postscript _s +#[cfg(not(stage0))] +#[lang="slice_mut"] +pub trait SliceMut for Sized? { + /// The method for the slicing operation foo[] + fn as_mut_slice<'a>(&'a mut self) -> &'a mut Result; + /// The method for the slicing operation foo[from..] + fn slice_from_mut<'a>(&'a mut self, from: &Idx) -> &'a mut Result; + /// The method for the slicing operation foo[..to] + fn slice_to_mut<'a>(&'a mut self, to: &Idx) -> &'a mut Result; + /// The method for the slicing operation foo[from..to] + fn slice_mut<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result; +} +/** + * + */ +#[cfg(stage0)] #[lang="slice_mut"] pub trait SliceMut for Sized? { /// The method for the slicing operation foo[] @@ -773,6 +803,7 @@ pub trait SliceMut for Sized? { /// The method for the slicing operation foo[from..to] fn slice_mut_<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result; } + /** * * The `Deref` trait is used to specify the functionality of dereferencing diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index a6bb4bd45e90f..027582a963e3d 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -61,32 +61,6 @@ use raw::Slice as RawSlice; /// Extension methods for immutable slices. #[unstable = "may merge with other traits; region parameter may disappear"] pub trait ImmutableSlice<'a, T> { - /// Returns a subslice spanning the interval [`start`, `end`). - /// - /// Fails when the end of the new slice lies beyond the end of the - /// original slice (i.e. when `end > self.len()`) or when `start > end`. - /// - /// Slicing with `start` equal to `end` yields an empty slice. - #[unstable] - //fn slice(&self, start: uint, end: uint) -> &'a [T]; - - /// Returns a subslice from `start` to the end of the slice. - /// - /// Fails when `start` is strictly greater than the length of the original slice. - /// - /// Slicing from `self.len()` yields an empty slice. - #[unstable] - // TODO - //fn slice_from(&self, start: uint) -> &'a [T]; - - /// Returns a subslice from the start of the slice to `end`. - /// - /// Fails when `end` is strictly greater than the length of the original slice. - /// - /// Slicing to `0` yields an empty slice. - #[unstable] - //fn slice_to(&self, end: uint) -> &'a [T]; - /// Divides one slice into two at an index. /// /// The first will contain all indices from `[0, mid)` (excluding @@ -455,6 +429,35 @@ impl<'a,T> ImmutableSlice<'a, T> for &'a [T] { } } +#[cfg(not(stage0))] +impl ops::Slice for [T] { + #[inline] + fn as_slice<'a>(&'a self) -> &'a [T] { + self + } + + #[inline] + fn slice_from<'a>(&'a self, start: &uint) -> &'a [T] { + self.slice(start, &self.len()) + } + + #[inline] + fn slice_to<'a>(&'a self, end: &uint) -> &'a [T] { + self.slice(&0, end) + } + #[inline] + fn slice<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] { + assert!(*start <= *end); + assert!(*end <= self.len()); + unsafe { + transmute(RawSlice { + data: self.as_ptr().offset(*start as int), + len: (*end - *start) + }) + } + } +} +#[cfg(stage0)] impl ops::Slice for [T] { #[inline] fn as_slice_<'a>(&'a self) -> &'a [T] { @@ -482,7 +485,36 @@ impl ops::Slice for [T] { } } } +#[cfg(not(stage0))] +impl ops::SliceMut for [T] { + #[inline] + fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] { + self + } + #[inline] + fn slice_from_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] { + let len = &self.len(); + self.slice_mut(start, len) + } + + #[inline] + fn slice_to_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] { + self.slice_mut(&0, end) + } + #[inline] + fn slice_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] { + assert!(*start <= *end); + assert!(*end <= self.len()); + unsafe { + transmute(RawSlice { + data: self.as_ptr().offset(*start as int), + len: (*end - *start) + }) + } + } +} +#[cfg(stage0)] impl ops::SliceMut for [T] { #[inline] fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] { @@ -524,46 +556,6 @@ pub trait MutableSlice<'a, T> { #[deprecated = "use slicing syntax"] fn as_mut_slice(self) -> &'a mut [T]; - /// Deprecated: use `slice_mut`. - #[deprecated = "use slicing syntax"] - //fn mut_slice(self, start: uint, end: uint) -> &'a mut [T] { - // self[mut start..end] - //} - - /// Returns a mutable subslice spanning the interval [`start`, `end`). - /// - /// Fails when the end of the new slice lies beyond the end of the - /// original slice (i.e. when `end > self.len()`) or when `start > end`. - /// - /// Slicing with `start` equal to `end` yields an empty slice. - //fn slice_mut(self, start: uint, end: uint) -> &'a mut [T]; - - /// Deprecated: use `slicing syntax`. - #[deprecated = "use slicing syntax"] - //fn mut_slice_from(self, start: uint) -> &'a mut [T] { - // self[mut start..] - //} - - /// Returns a mutable subslice from `start` to the end of the slice. - /// - /// Fails when `start` is strictly greater than the length of the original slice. - /// - /// Slicing from `self.len()` yields an empty slice. - //fn slice_from_mut(self, start: uint) -> &'a mut [T]; - - /// Deprecated: use `slicing syntax`. - #[deprecated = "use slicing syntax"] - //fn mut_slice_to(self, end: uint) -> &'a mut [T] { - // self[mut ..end] - //} - - /// Returns a mutable subslice from the start of the slice to `end`. - /// - /// Fails when `end` is strictly greater than the length of the original slice. - /// - /// Slicing to `0` yields an empty slice. - //fn slice_to_mut(self, end: uint) -> &'a mut [T]; - /// Deprecated: use `iter_mut`. #[deprecated = "use iter_mut"] fn mut_iter(self) -> MutItems<'a, T> { diff --git a/src/libcoretest/iter.rs b/src/libcoretest/iter.rs index f2410745e4a50..e84c894796736 100644 --- a/src/libcoretest/iter.rs +++ b/src/libcoretest/iter.rs @@ -373,7 +373,7 @@ fn test_all() { assert!(v.iter().all(|&x| x < 10)); assert!(!v.iter().all(|&x| x % 2 == 0)); assert!(!v.iter().all(|&x| x > 100)); - assert!(v.slice_(&0, &0).iter().all(|_| fail!())); + assert!(v.slice(&0, &0).iter().all(|_| fail!())); } #[test] @@ -382,7 +382,7 @@ fn test_any() { assert!(v.iter().any(|&x| x < 10)); assert!(v.iter().any(|&x| x % 2 == 0)); assert!(!v.iter().any(|&x| x > 100)); - assert!(!v.slice_(&0, &0).iter().any(|_| fail!())); + assert!(!v.slice(&0, &0).iter().any(|_| fail!())); } #[test] diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index 15720678e636c..1023f19e8827a 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -2302,10 +2302,10 @@ fn try_overloaded_slice(fcx: &FnCtxt, match fcx.tcx().lang_items.slice_mut_trait() { Some(trait_did) => { let method_name = match (start_expr, end_expr) { - (&Some(_), &Some(_)) => "slice_mut_", - (&Some(_), &None) => "slice_from_mut_", - (&None, &Some(_)) => "slice_to_mut_", - (&None, &None) => "as_mut_slice_", + (&Some(_), &Some(_)) => "slice_mut", + (&Some(_), &None) => "slice_from_mut", + (&None, &Some(_)) => "slice_to_mut", + (&None, &None) => "as_mut_slice", }; method::lookup_in_trait(fcx, @@ -2327,10 +2327,10 @@ fn try_overloaded_slice(fcx: &FnCtxt, match fcx.tcx().lang_items.slice_trait() { Some(trait_did) => { let method_name = match (start_expr, end_expr) { - (&Some(_), &Some(_)) => "slice_", - (&Some(_), &None) => "slice_from_", - (&None, &Some(_)) => "slice_to_", - (&None, &None) => "as_slice_", + (&Some(_), &Some(_)) => "slice", + (&Some(_), &None) => "slice_from", + (&None, &Some(_)) => "slice_to", + (&None, &None) => "as_slice", }; method::lookup_in_trait(fcx, diff --git a/src/test/run-pass/slice.rs b/src/test/run-pass/slice.rs index 39feb075add55..661ff055dc289 100644 --- a/src/test/run-pass/slice.rs +++ b/src/test/run-pass/slice.rs @@ -18,38 +18,38 @@ static mut COUNT: uint = 0; struct Foo; impl Slice for Foo { - fn as_slice_<'a>(&'a self) -> &'a Foo { + fn as_slice<'a>(&'a self) -> &'a Foo { unsafe { COUNT += 1; } self } - fn slice_from_<'a>(&'a self, _from: &Foo) -> &'a Foo { + fn slice_from<'a>(&'a self, _from: &Foo) -> &'a Foo { unsafe { COUNT += 1; } self } - fn slice_to_<'a>(&'a self, _to: &Foo) -> &'a Foo { + fn slice_to<'a>(&'a self, _to: &Foo) -> &'a Foo { unsafe { COUNT += 1; } self } - fn slice_<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo { + fn slice<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo { unsafe { COUNT += 1; } self } } impl SliceMut for Foo { - fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo { + fn as_mut_slice<'a>(&'a mut self) -> &'a mut Foo { unsafe { COUNT += 1; } self } - fn slice_from_mut_<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo { + fn slice_from_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo { unsafe { COUNT += 1; } self } - fn slice_to_mut_<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo { + fn slice_to_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo { unsafe { COUNT += 1; } self } - fn slice_mut_<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo { + fn slice_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo { unsafe { COUNT += 1; } self }