reusable_memory/borrow/
manual_specialization.rs1use super::ReusableMemoryBorrow;
4
5impl<'mem, T> ReusableMemoryBorrow<'mem, T> {
6 pub fn push_from_iter<I: Iterator<Item = T>>(&mut self, mut iter: I) -> Result<(), I> {
14 while self.len() < self.capacity().get() {
15 match iter.next() {
16 Some(value) => self.push(value).unwrap(),
17 None => return Ok(())
18 }
19 }
20
21 Err(iter)
22 }
23
24 pub fn push_from_iter_peeking<I: Iterator<Item = T>>(
29 &mut self, mut iter: I
30 ) -> Result<(), std::iter::Peekable<I>> {
31 while self.len() < self.capacity().get() {
32 match iter.next() {
33 Some(value) => self.push(value).unwrap(),
34 None => return Ok(())
35 }
36 }
37
38 let mut iter = iter.peekable();
39 if iter.peek().is_none() {
40 return Ok(())
41 }
42
43 Err(iter)
44 }
45
46 pub fn push_from_iter_size_hint<I: Iterator<Item = T>>(&mut self, iter: I) -> Result<(), I> {
47 let hint = iter.size_hint();
48 let hinted_max = hint.1.unwrap_or(hint.0);
49
50 if hinted_max > self.capacity().get() - self.len() {
51 return Err(iter)
52 }
53
54 self.push_from_iter(iter)
55 }
56
57 pub fn push_from_exact_iter<I: ExactSizeIterator<Item = T>>(
61 &mut self, iter: I
62 ) -> Result<(), I> {
63 if self.len() + iter.len() > self.capacity.get() {
64 return Err(iter)
65 }
66
67 for elem in iter {
68 self.push(elem).unwrap()
69 }
70
71 Ok(())
72 }
73}