Skip to content
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

New modifications #155

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 18 additions & 18 deletions lib.rs
Expand Up @@ -39,7 +39,7 @@ use core::{
fmt,
hash::{Hash, Hasher},
iter::{IntoIterator, FromIterator, repeat},
mem::{ManuallyDrop, self},
mem::{MaybeUninit, self},
ops,
ptr::{self, NonNull},
slice,
Expand Down Expand Up @@ -213,26 +213,26 @@ impl<'a, T: 'a> Drop for Drain<'a,T> {

#[cfg(feature = "union")]
union SmallVecData<A: Array> {
inline: ManuallyDrop<A>,
inline: MaybeUninit<A>,
heap: (NonNull<A::Item>, usize),
}

#[cfg(feature = "union")]
impl<A: Array> SmallVecData<A> {
#[inline]
unsafe fn inline(&self) -> &A {
&self.inline
&*self.inline.as_ptr()
}
#[inline]
unsafe fn inline_mut(&mut self) -> &mut A {
&mut self.inline
&mut *self.inline.as_mut_ptr()
}
#[inline]
fn from_inline(inline: A) -> SmallVecData<A> {
SmallVecData { inline: ManuallyDrop::new(inline) }
fn from_inline(inline: MaybeUninit<A>) -> SmallVecData<A> {
SmallVecData { inline }
}
#[inline]
unsafe fn into_inline(self) -> A { ManuallyDrop::into_inner(self.inline) }
unsafe fn into_inline(self) -> A { self.inline.assume_init() }
#[inline]
unsafe fn heap(&self) -> (*mut A::Item, usize) {
(self.heap.0.as_ptr(), self.heap.1)
Expand All @@ -249,7 +249,7 @@ impl<A: Array> SmallVecData<A> {

#[cfg(not(feature = "union"))]
enum SmallVecData<A: Array> {
Inline(ManuallyDrop<A>),
Inline(MaybeUninit<A>),
Heap((NonNull<A::Item>, usize)),
}

Expand All @@ -258,25 +258,25 @@ impl<A: Array> SmallVecData<A> {
#[inline]
unsafe fn inline(&self) -> &A {
match *self {
SmallVecData::Inline(ref a) => a,
SmallVecData::Inline(ref a) => &*a.as_ptr(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe it's unsound to return an &A that points to a possibly-uninitialized A. This method should be changed to return a raw pointer. (The callers of this method only need raw pointers anyways.)

_ => debug_unreachable!(),
}
}
#[inline]
unsafe fn inline_mut(&mut self) -> &mut A {
match *self {
SmallVecData::Inline(ref mut a) => a,
SmallVecData::Inline(ref mut a) => &mut *a.as_mut_ptr(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Like inline above, this method should be changed to return a raw pointer.

_ => debug_unreachable!(),
}
}
#[inline]
fn from_inline(inline: A) -> SmallVecData<A> {
SmallVecData::Inline(ManuallyDrop::new(inline))
fn from_inline(inline: MaybeUninit<A>) -> SmallVecData<A> {
SmallVecData::Inline(inline)
}
#[inline]
unsafe fn into_inline(self) -> A {
match self {
SmallVecData::Inline(a) => ManuallyDrop::into_inner(a),
SmallVecData::Inline(a) => a.assume_init(),
_ => debug_unreachable!(),
}
}
Expand Down Expand Up @@ -421,7 +421,7 @@ impl<A: Array> SmallVec<A> {
pub fn from_buf(buf: A) -> SmallVec<A> {
SmallVec {
capacity: A::size(),
data: SmallVecData::from_inline(buf),
data: SmallVecData::from_inline(MaybeUninit::new(buf)),
}
}

Expand Down Expand Up @@ -461,7 +461,7 @@ impl<A: Array> SmallVec<A> {
pub unsafe fn from_buf_and_len_unchecked(buf: A, len: usize) -> SmallVec<A> {
SmallVec {
capacity: len,
data: SmallVecData::from_inline(buf),
data: SmallVecData::from_inline(MaybeUninit::new(buf)),
}
}

Expand Down Expand Up @@ -979,8 +979,9 @@ impl<A: Array> SmallVec<A> where A::Item: Copy {
SmallVec {
capacity: len,
data: SmallVecData::from_inline(unsafe {
let mut data: A = mem::uninitialized();
ptr::copy_nonoverlapping(slice.as_ptr(), data.as_mut_ptr(), len);
let mut data = MaybeUninit::<A>::uninit();
let slice_mut = &mut *data.as_mut_ptr();
ptr::copy_nonoverlapping(slice.as_ptr(), slice_mut.as_mut_ptr(), len);
data
})
}
Expand All @@ -994,7 +995,6 @@ impl<A: Array> SmallVec<A> where A::Item: Copy {
}
}
}

/// Copy elements from a slice into the vector at position `index`, shifting any following
/// elements toward the back.
///
Expand Down