Skip to content

Commit aa3ba67

Browse files
committed
ADD: from_header_elements() constructor consuming its input
from_header_slice() clones the elements from a slice, in many cases this wont be optimal. from_header_elements() fixes this by consuming elements for a HeaderVec from a IntoIterator
1 parent d7a0d1a commit aa3ba67

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,25 @@ impl<H, T> HeaderVec<H, T> {
115115
this
116116
}
117117

118+
/// Creates a new `HeaderVec` with the given header from owned elements.
119+
/// This functions consumes elements from a `IntoIterator<Item = T>` and creates
120+
/// a `HeaderVec` from these. See [`from_header_slice()`] which creates a `HeaderVec`
121+
/// by cloning elements from a slice.
122+
///
123+
/// # Example
124+
///
125+
/// ```
126+
/// # use header_vec::HeaderVec;
127+
/// let hv = HeaderVec::from_header_elements(42, [1, 2, 3]);
128+
/// assert_eq!(hv.as_slice(), [1, 2, 3]);
129+
/// ```
130+
pub fn from_header_elements(header: H, elements: impl IntoIterator<Item = T>) -> Self {
131+
let iter = elements.into_iter();
132+
let mut hv = HeaderVec::with_capacity(iter.size_hint().0, header);
133+
hv.extend(iter);
134+
hv
135+
}
136+
118137
/// Get the length of the vector from a mutable reference. When one has a `&mut
119138
/// HeaderVec`, this is the method is always exact and can be slightly faster than the non
120139
/// mutable `len()`.
@@ -653,6 +672,8 @@ impl<H, T> HeaderVec<H, T> {
653672

654673
impl<H, T: Clone> HeaderVec<H, T> {
655674
/// Creates a new `HeaderVec` with the given header from some data.
675+
/// The data cloned from a `AsRef<[T]>`, see [`from_header_elements()`] for
676+
/// constructing a `HeaderVec` from owned elements.
656677
pub fn from_header_slice(header: H, slice: impl AsRef<[T]>) -> Self {
657678
let slice = slice.as_ref();
658679
let mut hv = Self::with_capacity(slice.len(), header);

0 commit comments

Comments
 (0)