copy_stack_vec/vec/array/try_from_iter.rs
1// This file is part of copy-stack-vec.
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4// Crate imports
5use crate::vec::CopyStackVec;
6
7impl<T: Copy + Default, const N: usize> CopyStackVec<T, N> {
8 /// Tries to construct from an iterator, erroring with [`Error::Full`] if it would overflow.
9 ///
10 /// Semantics:
11 /// - Elements are pushed in iterator order.
12 /// - On the first element that would exceed capacity `N`, this returns `Err(Error::Full)`.
13 /// - Any elements pushed before the overflow are discarded; the returned `Err` does *not*
14 /// include the partially filled vector.
15 /// - The source iterator may be left partially consumed (it stops at the first overflow).
16 #[inline]
17 pub fn try_from_iter<I: IntoIterator<Item = T>>(iter: I) -> Result<Self, crate::Error> {
18 let mut v = Self::default();
19 for item in iter {
20 v.push(item)?; // returns Err(Full) on overflow → we bail out immediately
21 }
22 Ok(v)
23 }
24}