Skip to content

add slice_at #18066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ pub trait ImmutableSlice<'a, T> {
#[unstable = "waiting on final error conventions/slicing syntax"]
fn slice_to(&self, end: uint) -> &'a [T];

/// Returns a subslice spanning the interval [`start`, `start + len`).
///
/// Fails when the end of the new slice lies beyond the end of the
/// original slice (i.e. when `start + len > self.len()`).
#[experimental]
fn slice_at(&self, start: uint, len: uint) -> &'a [T];

/// Divides one slice into two at an index.
///
/// The first will contain all indices from `[0, mid)` (excluding
Expand Down Expand Up @@ -315,6 +322,11 @@ impl<'a,T> ImmutableSlice<'a, T> for &'a [T] {
self.slice(0, end)
}

#[inline]
fn slice_at(&self, start: uint, len: uint) -> &'a [T] {
self.slice(start, start + len)
}

#[inline]
fn split_at(&self, mid: uint) -> (&'a [T], &'a [T]) {
((*self)[..mid], (*self)[mid..])
Expand Down Expand Up @@ -606,6 +618,13 @@ pub trait MutableSlice<'a, T> {
self.iter_mut()
}

/// Returns a mutable subslice spanning the interval [`start`, `start + len`).
///
/// Fails when the end of the new slice lies beyond the end of the
/// original slice (i.e. when `start + len > self.len()`).
#[experimental]
fn slice_at_mut(self, start: uint, len: uint) -> &'a mut [T];

/// Returns an iterator that allows modifying each value
#[unstable = "waiting on iterator type name conventions"]
fn iter_mut(self) -> MutItems<'a, T>;
Expand Down Expand Up @@ -841,6 +860,11 @@ impl<'a,T> MutableSlice<'a, T> for &'a mut [T] {
self[mut ..end]
}

#[inline]
fn slice_at_mut(self, start: uint, len: uint) -> &'a mut [T] {
self[mut start..(start + len)]
}

#[inline]
fn split_at_mut(self, mid: uint) -> (&'a mut [T], &'a mut [T]) {
unsafe {
Expand Down