Skip to main content

ps_ecc/polynomial/implementations/index/
u8.rs

1use std::ops::Index;
2
3use crate::Polynomial;
4
5impl Index<u8> for Polynomial {
6    type Output = u8;
7
8    fn index(&self, index: u8) -> &Self::Output {
9        &self[index as usize]
10    }
11}
12
13#[cfg(test)]
14#[allow(clippy::expect_used)]
15mod tests {
16    use crate::Polynomial;
17
18    #[test]
19    fn index_u8_within_bounds() {
20        let p = Polynomial::from_slice(&[10, 20, 30]).expect("valid polynomial");
21
22        assert_eq!(p[0u8], 10);
23        assert_eq!(p[1u8], 20);
24        assert_eq!(p[2u8], 30);
25    }
26
27    #[test]
28    fn index_u8_out_of_bounds_returns_zero() {
29        let p = Polynomial::from_slice(&[1]).expect("valid polynomial");
30
31        assert_eq!(p[1u8], 0);
32        assert_eq!(p[u8::MAX], 0);
33    }
34}