ps_ecc/polynomial/implementations/
into_iterator.rs1use std::{array::IntoIter, iter::Take};
2
3use crate::{polynomial::constants::POLYNOMIAL_MAX_COEFFICIENTS, Polynomial};
4
5impl IntoIterator for Polynomial {
6 type Item = u8;
7 type IntoIter = Take<IntoIter<u8, POLYNOMIAL_MAX_COEFFICIENTS>>;
8
9 fn into_iter(self) -> Self::IntoIter {
10 self.coefficients
11 .into_iter()
12 .take(self.degree.saturating_add(1) as usize)
13 }
14}
15
16impl<'a> IntoIterator for &'a Polynomial {
17 type Item = &'a u8;
18 type IntoIter = std::slice::Iter<'a, u8>;
19
20 fn into_iter(self) -> Self::IntoIter {
21 self.iter()
22 }
23}
24
25#[cfg(test)]
26#[allow(clippy::expect_used)]
27mod tests {
28 use crate::Polynomial;
29
30 #[test]
31 fn iter_ref_polynomial() {
32 let poly = Polynomial::try_from(&[1u8, 2, 3][..]).expect("valid polynomial");
33 let coeffs: Vec<u8> = (&poly).into_iter().copied().collect();
34
35 assert_eq!(coeffs, vec![1, 2, 3]);
36 }
37
38 #[test]
39 fn iter_in_for_loop() {
40 let poly = Polynomial::try_from(&[4u8, 5, 6][..]).expect("valid polynomial");
41 let mut sum = 0u8;
42
43 for &coef in &poly {
44 sum ^= coef;
45 }
46
47 assert_eq!(sum, 4 ^ 5 ^ 6);
48 }
49
50 #[test]
51 fn iter_with_adapters() {
52 let poly = Polynomial::try_from(&[1u8, 2, 3, 4][..]).expect("valid polynomial");
53 let doubled: Vec<u8> = (&poly).into_iter().map(|&c| c.wrapping_mul(2)).collect();
54
55 assert_eq!(doubled, vec![2, 4, 6, 8]);
56 }
57
58 #[test]
59 fn iter_empty_polynomial() {
60 let poly = Polynomial::default();
61 let coeffs: Vec<u8> = (&poly).into_iter().copied().collect();
62
63 assert_eq!(coeffs, vec![0]);
64 }
65
66 #[test]
67 fn iter_method() {
68 let poly = Polynomial::try_from(&[1u8, 2, 3][..]).expect("valid polynomial");
69 let coeffs: Vec<u8> = poly.iter().copied().collect();
70
71 assert_eq!(coeffs, vec![1, 2, 3]);
72 }
73
74 #[test]
75 fn into_iter_owned() {
76 let poly = Polynomial::try_from(&[1u8, 2, 3][..]).expect("valid polynomial");
77 let coeffs: Vec<u8> = poly.into_iter().collect();
78
79 assert_eq!(coeffs, vec![1, 2, 3]);
80 }
81
82 #[test]
83 fn into_iter_owned_in_for_loop() {
84 let poly = Polynomial::try_from(&[4u8, 5, 6][..]).expect("valid polynomial");
85 let mut sum = 0u8;
86
87 for coef in poly {
88 sum ^= coef;
89 }
90
91 assert_eq!(sum, 4 ^ 5 ^ 6);
92 }
93
94 #[test]
95 fn into_iter_exact_size() {
96 let poly = Polynomial::try_from(&[1u8, 2, 3, 4, 5][..]).expect("valid polynomial");
97 let iter = poly.into_iter();
98
99 assert_eq!(iter.len(), 5);
100 }
101
102 #[test]
103 fn into_iter_max_degree() {
104 let coeffs = [1u8; 255];
105 let poly = Polynomial::try_from(&coeffs[..]).expect("valid polynomial");
106
107 assert_eq!(poly.into_iter().count(), 255);
108 }
109
110 #[test]
111 fn into_iter_rev() {
112 let poly = Polynomial::try_from(&[1u8, 2, 3][..]).expect("valid polynomial");
113 let reversed: Vec<u8> = poly.into_iter().rev().collect();
114
115 assert_eq!(reversed, vec![3, 2, 1]);
116 }
117
118 #[test]
119 fn into_iter_matches_iter() {
120 let poly = Polynomial::try_from(&[1u8, 2, 3, 4, 5][..]).expect("valid polynomial");
121 let from_iter: Vec<u8> = poly.iter().copied().collect();
122 let from_into_iter: Vec<u8> = poly.into_iter().collect();
123
124 assert_eq!(from_iter, from_into_iter);
125 }
126}