diff --git a/src/vec1.rs b/src/vec1.rs index fd64d57..0967f5b 100644 --- a/src/vec1.rs +++ b/src/vec1.rs @@ -137,10 +137,20 @@ impl Vec1 { } pub fn from_one_with_capacity(item: T, capacity: usize) -> Self { - let mut items = Vec::with_capacity(capacity); - items.push(item); - // SAFETY: `items` must contain `item` and therefore is non-empty here, because `push` - // either pushes the item or panics. + Vec1::from_iter1_with_capacity([item], capacity) + } + + pub fn from_iter1_with_capacity(items: U, capacity: usize) -> Self + where + U: IntoIterator1, + { + let items = { + let mut xs = Vec::with_capacity(capacity); + xs.extend(items); + xs + }; + // SAFETY: The input iterator `items` is non-empty and `extend` either pushes one or more + // items or panics, so `items` must be non-empty here. unsafe { Vec1::from_vec_unchecked(items) } } diff --git a/src/vec_deque1.rs b/src/vec_deque1.rs index 961c268..df7e19e 100644 --- a/src/vec_deque1.rs +++ b/src/vec_deque1.rs @@ -100,10 +100,20 @@ impl VecDeque1 { } pub fn from_one_with_capacity(item: T, capacity: usize) -> Self { - let mut items = VecDeque::with_capacity(capacity); - items.push_back(item); - // SAFETY: `items` must contain `item` and therefore is non-empty here, because `push_back` - // either pushes the item or panics. + VecDeque1::from_iter1_with_capacity([item], capacity) + } + + pub fn from_iter1_with_capacity(items: U, capacity: usize) -> Self + where + U: IntoIterator1, + { + let items = { + let mut xs = VecDeque::with_capacity(capacity); + xs.extend(items); + xs + }; + // SAFETY: The input iterator `items` is non-empty and `extend` either pushes one or more + // items or panics, so `items` must be non-empty here. unsafe { VecDeque1::from_vec_deque_unchecked(items) } }