Skip to content

Commit cfa1f0c

Browse files
committed
+docs -debug_unreachable +drop_in_place
1 parent 35b43c0 commit cfa1f0c

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ path = "lib.rs"
2121

2222
[dependencies]
2323
unreachable = "1.0.0"
24-
debug_unreachable = "0.1"
2524
serde = { version = "1", optional = true }
2625

2726
[dev_dependencies]

lib.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ extern crate serde;
4545
extern crate unreachable;
4646
use unreachable::UncheckedOptionExt;
4747

48-
#[cfg(not(feature = "union"))]
49-
#[macro_use]
50-
extern crate debug_unreachable;
51-
5248
#[cfg(not(feature = "std"))]
5349
mod 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
/// ```
372381
pub 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

Comments
 (0)