reed_solomon_simd/
encoder_result.rs

1use crate::rate::EncoderWork;
2
3// ======================================================================
4// EncoderResult - PUBLIC
5
6/// Result of encoding. Contains the generated recovery shards.
7///
8/// This struct is created by [`ReedSolomonEncoder::encode`]
9/// and [`RateEncoder::encode`].
10///
11/// [`RateEncoder::encode`]: crate::rate::RateEncoder::encode
12/// [`ReedSolomonEncoder::encode`]: crate::ReedSolomonEncoder::encode
13pub struct EncoderResult<'a> {
14    work: &'a mut EncoderWork,
15}
16
17impl EncoderResult<'_> {
18    /// Returns recovery shard with given `index`
19    /// or `None` if `index >= recovery_count`.
20    ///
21    /// Recovery shards have indexes `0..recovery_count`
22    /// and these same indexes must be used when decoding.
23    pub fn recovery(&self, index: usize) -> Option<&[u8]> {
24        self.work.recovery(index)
25    }
26
27    /// Returns iterator over all recovery shards ordered by their indexes.
28    ///
29    /// Recovery shards have indexes `0..recovery_count`
30    /// and these same indexes must be used when decoding.
31    pub fn recovery_iter(&self) -> Recovery {
32        Recovery::new(self.work)
33    }
34}
35
36// ======================================================================
37// EncoderResult - CRATE
38
39impl<'a> EncoderResult<'a> {
40    pub(crate) fn new(work: &'a mut EncoderWork) -> Self {
41        Self { work }
42    }
43}
44
45// ======================================================================
46// EncoderResult - IMPL DROP
47
48impl Drop for EncoderResult<'_> {
49    fn drop(&mut self) {
50        self.work.reset_received();
51    }
52}
53
54// ======================================================================
55// Recovery - PUBLIC
56
57/// Iterator over generated recovery shards.
58///
59/// This struct is created by [`EncoderResult::recovery_iter`].
60pub struct Recovery<'a> {
61    ended: bool,
62    next_index: usize,
63    work: &'a EncoderWork,
64}
65
66// ======================================================================
67// Recovery - IMPL Iterator
68
69impl<'a> Iterator for Recovery<'a> {
70    type Item = &'a [u8];
71    fn next(&mut self) -> Option<&'a [u8]> {
72        if self.ended {
73            None
74        } else if let Some(next) = self.work.recovery(self.next_index) {
75            self.next_index += 1;
76            Some(next)
77        } else {
78            self.ended = true;
79            None
80        }
81    }
82}
83
84// ======================================================================
85// Recovery - CRATE
86
87impl<'a> Recovery<'a> {
88    pub(crate) fn new(work: &'a EncoderWork) -> Self {
89        Self {
90            ended: false,
91            next_index: 0,
92            work,
93        }
94    }
95}
96
97// ======================================================================
98// TESTS
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103    use crate::{test_util, ReedSolomonEncoder};
104
105    #[test]
106    // EncoderResult::recovery
107    // EncoderResult::recovery_iter
108    // Recovery
109    fn encoder_result() {
110        let original = test_util::generate_original(2, 1024, 123);
111        let mut encoder = ReedSolomonEncoder::new(2, 3, 1024).unwrap();
112
113        for original in &original {
114            encoder.add_original_shard(original).unwrap();
115        }
116
117        let result: EncoderResult = encoder.encode().unwrap();
118
119        let mut all = Vec::new();
120        all.push(result.recovery(0).unwrap());
121        all.push(result.recovery(1).unwrap());
122        all.push(result.recovery(2).unwrap());
123        assert!(result.recovery(3).is_none());
124        test_util::assert_hash(all, test_util::LOW_2_3);
125
126        let mut iter: Recovery = result.recovery_iter();
127        let mut all = Vec::new();
128        all.push(iter.next().unwrap());
129        all.push(iter.next().unwrap());
130        all.push(iter.next().unwrap());
131        assert!(iter.next().is_none());
132        test_util::assert_hash(all, test_util::LOW_2_3);
133    }
134}