Skip to content

Implement placement-in protocol for Vec #32366

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
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#![feature(nonzero)]
#![feature(pattern)]
#![feature(placement_in)]
#![feature(placement_in_syntax)]
#![feature(placement_new_protocol)]
#![feature(shared)]
#![feature(slice_patterns)]
Expand Down
72 changes: 61 additions & 11 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use core::hash::{self, Hash};
use core::intrinsics::{arith_offset, assume};
use core::iter::FromIterator;
use core::mem;
use core::ops::{Index, IndexMut};
use core::ops::{InPlace, Index, IndexMut, Place, Placer};
use core::ops;
use core::ptr;
use core::slice;
Expand Down Expand Up @@ -699,16 +699,7 @@ impl<T> Vec<T> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push(&mut self, value: T) {
// This will panic or abort if we would allocate > isize::MAX bytes
// or if the length increment would overflow for zero-sized types.
if self.len == self.buf.cap() {
self.buf.double();
}
unsafe {
let end = self.as_mut_ptr().offset(self.len as isize);
ptr::write(end, value);
self.len += 1;
}
self <- value;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd avoid using placement in stable push before doing proper performance measurements.

Copy link
Member

Choose a reason for hiding this comment

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

I had some benchmarks implemented in nagisa@7b1b36d but they’re not enough at all to properly test all the cases (e.g. large-sized T).

}

/// Removes the last element from a vector and returns it, or `None` if it
Expand Down Expand Up @@ -1759,3 +1750,62 @@ impl<'a, T> Drop for Drain<'a, T> {

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> ExactSizeIterator for Drain<'a, T> {}

#[unstable(feature = "collection_placement",
reason = "placement protocol is subject to change",
issue = "30172")]
impl<'a, T> Placer<T> for &'a mut Vec<T> {
type Place = BackPlace<'a, T>;

fn make_place(self) -> BackPlace<'a, T> {
// This will panic or abort if we would allocate > isize::MAX bytes
// or if the length increment would overflow for zero-sized types.
if self.len == self.buf.cap() {
self.buf.double();
}

BackPlace { vec: self }
}
}

/// A place for insertion at the back of a `Vec`.
///
/// # Examples
///
/// ```
/// #![feature(placement_in_syntax)]
///
/// let mut vec = vec![1, 2];
/// &mut vec <- 3;
/// &mut vec <- 4;
/// assert_eq!(&vec, &[1, 2, 3, 4]);
/// ```
#[must_use = "places do nothing unless written to with `<-` syntax"]
#[unstable(feature = "collection_placement",
reason = "struct name and placement protocol are subject to change",
issue = "30172")]
pub struct BackPlace<'a, T: 'a> {
vec: &'a mut Vec<T>,
}

#[unstable(feature = "collection_placement",
reason = "placement protocol is subject to change",
issue = "30172")]
impl<'a, T> Place<T> for BackPlace<'a, T> {
fn pointer(&mut self) -> *mut T {
unsafe { self.vec.as_mut_ptr().offset(self.vec.len as isize) }
}
}

#[unstable(feature = "collection_placement",
reason = "placement protocol is subject to change",
issue = "30172")]
impl<'a, T> InPlace<T> for BackPlace<'a, T> {
type Owner = &'a mut T;

unsafe fn finalize(mut self) -> &'a mut T {
let ptr = self.pointer();
self.vec.len += 1;
&mut *ptr
}
}
3 changes: 3 additions & 0 deletions src/libcollectionstest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
#![feature(iter_arith)]
#![feature(map_entry_keys)]
#![feature(pattern)]
#![feature(placement_in_syntax)]
#![feature(rand)]
#![feature(recover)]
#![feature(set_recovery)]
#![feature(std_panic)]
#![feature(step_by)]
#![feature(str_char)]
#![feature(str_escape)]
Expand Down
18 changes: 18 additions & 0 deletions src/libcollectionstest/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use std::borrow::Cow;
use std::iter::{FromIterator, repeat};
use std::mem::size_of;
use std::panic;

use test::Bencher;

Expand Down Expand Up @@ -495,6 +496,23 @@ fn test_cow_from() {
}
}

#[test]
fn test_placement() {
let mut vec = vec![1];
assert_eq!(&mut vec <- 2, &2);
assert_eq!(vec.len(), 2);
assert_eq!(&mut vec <- 3, &3);
assert_eq!(vec.len(), 3);
assert_eq!(&vec, &[1, 2, 3]);
}

#[test]
fn test_placement_panic() {
let mut vec = vec![1, 2, 3];
let _ = panic::recover(panic::AssertRecoverSafe(|| { &mut vec <- panic!(); }));
assert_eq!(vec.len(), 3);
}

#[bench]
fn bench_new(b: &mut Bencher) {
b.iter(|| {
Expand Down