Skip to content

Commit ecc9007

Browse files
author
bors-servo
authored
Auto merge of #56 - Vurich:master, r=jdm
Add from_buf method If you have an `A` on the stack already this allows you to create a `SmallVec<A>` with zero copying. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-smallvec/56) <!-- Reviewable:end -->
2 parents 7bda532 + 534532d commit ecc9007

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,25 @@ impl<A: Array> SmallVec<A> {
289289
}
290290
}
291291

292+
/// Constructs a new `SmallVec` on the stack from an `A` without
293+
/// copying elements.
294+
///
295+
/// ```rust
296+
/// use smallvec::SmallVec;
297+
///
298+
/// let buf = [1, 2, 3, 4, 5];
299+
/// let small_vec: SmallVec<_> = SmallVec::from_buf(buf);
300+
///
301+
/// assert_eq!(&*small_vec, &[1, 2, 3, 4, 5]);
302+
/// ```
303+
#[inline]
304+
pub fn from_buf(buf: A) -> SmallVec<A> {
305+
SmallVec {
306+
len: A::size(),
307+
data: SmallVecData::Inline { array: buf },
308+
}
309+
}
310+
292311
/// Sets the length of a vector.
293312
///
294313
/// This will explicitly set the size of the vector, without actually

0 commit comments

Comments
 (0)