Skip to main content

ps_ecc/reed_solomon/methods/
apply_corrections.rs

1use crate::ReedSolomon;
2
3impl ReedSolomon {
4    /// XORs `corrections` onto `target`, element-wise.
5    ///
6    /// The two slices are zipped, so the tail of the longer one is
7    /// ignored; a zero correction byte leaves its target byte unchanged.
8    pub fn apply_corrections(target: &mut [u8], corrections: impl AsRef<[u8]>) {
9        target
10            .iter_mut()
11            .zip(corrections.as_ref().iter())
12            .for_each(|(target, correction)| *target ^= *correction);
13    }
14}
15
16#[cfg(test)]
17mod tests {
18    use ps_buffer::Buffer;
19
20    use crate::ReedSolomon;
21
22    type TestError = Box<dyn std::error::Error>;
23
24    #[test]
25    fn test_apply_corrections() -> Result<(), TestError> {
26        let mut target = Buffer::from_slice([1, 2, 3, 4])?;
27        let corrections = [0, 3, 0, 5];
28
29        ReedSolomon::apply_corrections(&mut target, corrections);
30
31        assert_eq!(target.as_slice(), &[1, 2 ^ 3, 3, 4 ^ 5]);
32
33        Ok(())
34    }
35
36    #[test]
37    fn test_apply_corrections_empty_target() -> Result<(), TestError> {
38        let mut target = Buffer::from_slice([])?;
39        let corrections = [];
40
41        ReedSolomon::apply_corrections(&mut target, corrections);
42
43        assert_eq!(target.as_slice(), &[]);
44
45        Ok(())
46    }
47}