Skip to content

Add iterator constructors and accessors from/to raw pointers #37921

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
56 changes: 56 additions & 0 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,22 @@ unsafe impl<'a, T: Sync> Sync for Iter<'a, T> {}
unsafe impl<'a, T: Sync> Send for Iter<'a, T> {}

impl<'a, T> Iter<'a, T> {
/// Create a slice iterator from a `begin`, `end` pair of pointers.
///
/// # Safety
///
/// `begin` and `end` must be non-null, not mutably aliased, and point in
/// bounds of or one past the end of the same object. This produces an
/// iterator of arbitrary lifetime, and must be used with care.
#[unstable(feature = "slice_iter_raw_parts", issue = "0")]
pub unsafe fn from_raw_parts(begin: *const T, end: *const T) -> Self {
Iter {
ptr: begin,
end: end,
_marker: marker::PhantomData,
}
}

/// View the underlying data as a subslice of the original data.
///
/// This has the same lifetime as the original slice, and so the
Expand Down Expand Up @@ -965,6 +981,18 @@ impl<'a, T> Iter<'a, T> {
make_slice!(self.ptr, self.end)
}

/// Return start pointer of the iterator
#[unstable(feature = "slice_iter_raw_parts", issue = "0")]
pub fn start(&self) -> *const T {
self.ptr
}

/// Return end pointer of the iterator
#[unstable(feature = "slice_iter_raw_parts", issue = "0")]
pub fn end(&self) -> *const T {
self.end
}

// Helper function for Iter::nth
fn iter_nth(&mut self, n: usize) -> Option<&'a T> {
match self.as_slice().get(n) {
Expand Down Expand Up @@ -1049,6 +1077,22 @@ unsafe impl<'a, T: Sync> Sync for IterMut<'a, T> {}
unsafe impl<'a, T: Send> Send for IterMut<'a, T> {}

impl<'a, T> IterMut<'a, T> {
/// Create an iterator from a pair of `begin`, `end` pointers.
///
/// # Safety
///
/// `begin` and `end` must be non-null, not aliased, and point in bounds of
/// or one past the end of the same object. This produces an iterator of
/// arbitrary lifetime, and must be used with care.
#[unstable(feature = "slice_iter_raw_parts", issue = "0")]
pub unsafe fn from_raw_parts(begin: *mut T, end: *mut T) -> Self {
IterMut {
ptr: begin,
end: end,
_marker: marker::PhantomData,
}
}

/// View the underlying data as a subslice of the original data.
///
/// To avoid creating `&mut` references that alias, this is forced
Expand Down Expand Up @@ -1089,6 +1133,18 @@ impl<'a, T> IterMut<'a, T> {
make_mut_slice!(self.ptr, self.end)
}

/// Return start pointer of the iterator
#[unstable(feature = "slice_iter_raw_parts", issue = "0")]
pub fn start(&self) -> *mut T {
self.ptr
}

/// Return end pointer of the iterator
#[unstable(feature = "slice_iter_raw_parts", issue = "0")]
pub fn end(&self) -> *mut T {
self.end
}

// Helper function for IterMut::nth
fn iter_nth(&mut self, n: usize) -> Option<&'a mut T> {
match make_mut_slice!(self.ptr, self.end).get_mut(n) {
Expand Down