Skip to content

Add method Vec<T>::as_mut_ptr() #12945

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

Merged
merged 1 commit into from
Mar 17, 2014
Merged
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
11 changes: 7 additions & 4 deletions src/libstd/vec_ng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,7 @@ impl<T> Vec<T> {
unsafe { // infallible
// The spot to put the new value
{
let slice = self.as_mut_slice();
let p = slice.as_mut_ptr().offset(index as int);
let p = self.as_mut_ptr().offset(index as int);
// Shift everything over to make space. (Duplicating the
// `index`th element into two consecutive places.)
ptr::copy_memory(p.offset(1), &*p, len - index);
Expand All @@ -434,9 +433,8 @@ impl<T> Vec<T> {
unsafe { // infallible
let ret;
{
let slice = self.as_mut_slice();
// the place we are taking from.
let ptr = slice.as_mut_ptr().offset(index as int);
let ptr = self.as_mut_ptr().offset(index as int);
// copy it out, unsafely having a copy of the value on
// the stack and in the vector at the same time.
ret = Some(ptr::read(ptr as *T));
Expand Down Expand Up @@ -499,6 +497,11 @@ impl<T> Vec<T> {
pub fn as_ptr(&self) -> *T {
self.as_slice().as_ptr()
}

#[inline]
pub fn as_mut_ptr(&mut self) -> *mut T {
self.as_mut_slice().as_mut_ptr()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this just be self.ptr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably could, but I think I prefer this way in case specifics of the implementation change. When compiled, it should be optimized as if the method were written as self.ptr, right?

}
}

impl<T> Mutable for Vec<T> {
Expand Down