@@ -45,10 +45,6 @@ extern crate serde;
4545extern crate unreachable;
4646use unreachable:: UncheckedOptionExt ;
4747
48- #[ cfg( not( feature = "union" ) ) ]
49- #[ macro_use]
50- extern crate debug_unreachable;
51-
5248#[ cfg( not( feature = "std" ) ) ]
5349mod std {
5450 pub use core:: * ;
@@ -123,6 +119,19 @@ macro_rules! smallvec {
123119 } ) ;
124120}
125121
122+ /// `panic!()` in debug builds, optimization hint in release.
123+ #[ cfg( not( feature = "union" ) ) ]
124+ macro_rules! debug_unreachable {
125+ ( ) => { debug_unreachable!( "entered unreachable code" ) } ;
126+ ( $e: expr) => {
127+ if cfg!( not( debug_assertions) ) {
128+ unreachable:: unreachable( ) ;
129+ } else {
130+ panic!( $e) ;
131+ }
132+ }
133+ }
134+
126135/// Common operations implemented by both `Vec` and `SmallVec`.
127136///
128137/// This can be used to write generic code that works with both `Vec` and `SmallVec`.
@@ -370,6 +379,9 @@ unsafe impl<A: Array + Sync> Sync for SmallVecData<A> {}
370379/// assert!(v.spilled());
371380/// ```
372381pub struct SmallVec < A : Array > {
382+ // The capacity field is used to determine which of the storage variants is active:
383+ // If capacity <= A::size() then the inline variant is used and capacity holds the current length of the vector (number of elements actually in use).
384+ // If capacity > A::size() then the heap variant is used and capacity holds the size of the memory allocation.
373385 capacity : usize ,
374386 data : SmallVecData < A > ,
375387}
@@ -406,8 +418,9 @@ impl<A: Array> SmallVec<A> {
406418 v
407419 }
408420
409- /// Construct a new `SmallVec` from a `Vec<A::Item>` without copying
410- /// elements.
421+ /// Construct a new `SmallVec` from a `Vec<A::Item>`.
422+ ///
423+ /// Elements will be copied to the inline buffer if vec.capacity() <= A::size().
411424 ///
412425 /// ```rust
413426 /// use smallvec::SmallVec;
@@ -495,6 +508,8 @@ impl<A: Array> SmallVec<A> {
495508 self . triple ( ) . 2
496509 }
497510
511+ /// Returns a tuple with (data ptr, len, capacity)
512+ /// Useful to get all SmallVec properties with a single check of the current storage variant.
498513 #[ inline]
499514 fn triple ( & self ) -> ( * const A :: Item , usize , usize ) {
500515 unsafe {
@@ -507,6 +522,7 @@ impl<A: Array> SmallVec<A> {
507522 }
508523 }
509524
525+ /// Returns a tuple with (data ptr, len ptr, capacity)
510526 #[ inline]
511527 fn triple_mut ( & mut self ) -> ( * mut A :: Item , & mut usize , usize ) {
512528 unsafe {
@@ -666,7 +682,7 @@ impl<A: Array> SmallVec<A> {
666682 while len < * len_ptr {
667683 let last_index = * len_ptr - 1 ;
668684 * len_ptr = last_index;
669- ptr:: read ( ptr. offset ( last_index as isize ) ) ;
685+ ptr:: drop_in_place ( ptr. offset ( last_index as isize ) ) ;
670686 }
671687 }
672688 }
@@ -1168,9 +1184,9 @@ impl<A: Array> Drop for SmallVec<A> {
11681184 let ( ptr, len) = self . data . heap ( ) ;
11691185 Vec :: from_raw_parts ( ptr, len, self . capacity ) ;
11701186 } else {
1171- let ptr = self . as_ptr ( ) ;
1187+ let ptr = self . as_mut_ptr ( ) ;
11721188 for i in 0 ..self . len ( ) {
1173- ptr:: read ( ptr. offset ( i as isize ) ) ;
1189+ ptr:: drop_in_place ( ptr. offset ( i as isize ) ) ;
11741190 }
11751191 }
11761192 }
0 commit comments