malachite_base/unsigned_vector/conversion/into_iterator.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::num::basic::unsigneds::PrimitiveUnsigned;
10use crate::unsigned_vector::UnsignedVector;
11use alloc::vec;
12use core::slice;
13
14impl<T: PrimitiveUnsigned> IntoIterator for UnsignedVector<T> {
15 type Item = T;
16 type IntoIter = vec::IntoIter<T>;
17
18 /// Converts an [`UnsignedVector`] into an iterator over its elements, taking the vector by
19 /// value.
20 ///
21 /// The elements are produced in order, starting with the element at index 0.
22 ///
23 /// # Worst-case complexity
24 /// Constant time and additional memory.
25 ///
26 /// # Examples
27 /// ```
28 /// use core::str::FromStr;
29 /// use malachite_base::unsigned_vector::UnsignedVector;
30 ///
31 /// let v = UnsignedVector::<u8>::from_str("(1, 2, 3)").unwrap();
32 /// assert_eq!(
33 /// v.into_iter().map(|x| x.to_string()).collect::<Vec<_>>(),
34 /// ["1", "2", "3"]
35 /// );
36 /// ```
37 #[inline]
38 fn into_iter(self) -> vec::IntoIter<T> {
39 self.elements.into_iter()
40 }
41}
42
43impl<'a, T: PrimitiveUnsigned> IntoIterator for &'a UnsignedVector<T> {
44 type Item = &'a T;
45 type IntoIter = slice::Iter<'a, T>;
46
47 /// Converts a reference to an [`UnsignedVector`] into an iterator over references to its
48 /// elements.
49 ///
50 /// The elements are produced in order, starting with the element at index 0. This is what `for
51 /// x in &v` uses.
52 ///
53 /// # Worst-case complexity
54 /// Constant time and additional memory.
55 ///
56 /// # Examples
57 /// ```
58 /// use core::str::FromStr;
59 /// use malachite_base::unsigned_vector::UnsignedVector;
60 ///
61 /// let v = UnsignedVector::<u8>::from_str("(1, 2, 3)").unwrap();
62 /// let mut strings = Vec::new();
63 /// for x in &v {
64 /// strings.push(x.to_string());
65 /// }
66 /// assert_eq!(strings, ["1", "2", "3"]);
67 /// ```
68 #[inline]
69 fn into_iter(self) -> slice::Iter<'a, T> {
70 self.elements.iter()
71 }
72}
73
74impl<'a, T: PrimitiveUnsigned> IntoIterator for &'a mut UnsignedVector<T> {
75 type Item = &'a mut T;
76 type IntoIter = slice::IterMut<'a, T>;
77
78 /// Converts a mutable reference to an [`UnsignedVector`] into an iterator over mutable
79 /// references to its elements.
80 ///
81 /// The elements are produced in order, starting with the element at index 0. This is what `for
82 /// x in &mut v` uses; since every list of elements is a valid vector, any element may be
83 /// changed.
84 ///
85 /// # Worst-case complexity
86 /// Constant time and additional memory.
87 ///
88 /// # Examples
89 /// ```
90 /// use core::str::FromStr;
91 /// use malachite_base::unsigned_vector::UnsignedVector;
92 ///
93 /// let mut v = UnsignedVector::<u8>::from_str("(1, 2, 3)").unwrap();
94 /// for x in &mut v {
95 /// *x += 1;
96 /// }
97 /// assert_eq!(v.to_string(), "(2, 3, 4)");
98 /// ```
99 #[inline]
100 fn into_iter(self) -> slice::IterMut<'a, T> {
101 self.elements.iter_mut()
102 }
103}