pub struct ReedSolomon { /* private fields */ }Expand description
Reed-Solomon erasure code encoder/decoder.
Operates over GF(2^8) with generating polynomial 29, compatible with the Moonlight streaming protocol.
§Example
use fec_rs::ReedSolomon;
let rs = ReedSolomon::new(4, 2).unwrap();
let mut shards: Vec<Vec<u8>> = vec![
vec![0, 1, 2, 3],
vec![4, 5, 6, 7],
vec![8, 9, 10, 11],
vec![12, 13, 14, 15],
vec![0, 0, 0, 0],
vec![0, 0, 0, 0],
];
rs.encode(&mut shards).unwrap();
assert!(rs.verify(&shards).unwrap());Implementations§
Source§impl ReedSolomon
impl ReedSolomon
Sourcepub fn new(data_shards: usize, parity_shards: usize) -> Result<Self, Error>
pub fn new(data_shards: usize, parity_shards: usize) -> Result<Self, Error>
Creates a new Reed-Solomon encoder/decoder.
§Errors
Returns an error if data_shards or parity_shards is 0,
or if data_shards + parity_shards > 256.
Sourcepub fn set_parity_matrix(&mut self, parity: &[u8]) -> Result<(), Error>
pub fn set_parity_matrix(&mut self, parity: &[u8]) -> Result<(), Error>
Directly set the parity rows of the encoding matrix.
parity must have exactly parity_shard_count * data_shard_count elements.
This is used for compatibility with the Moonlight audio protocol,
where the parity matrix values are hardcoded from OpenFEC.
pub fn data_shard_count(&self) -> usize
pub fn parity_shard_count(&self) -> usize
pub fn total_shard_count(&self) -> usize
pub fn encode<T: AsRef<[u8]> + AsMut<[u8]>>( &self, shards: &mut [T], ) -> Result<(), Error>
Sourcepub fn encode_sep<T: AsRef<[u8]>, U: AsRef<[u8]> + AsMut<[u8]>>(
&self,
data: &[T],
parity: &mut [U],
) -> Result<(), Error>
pub fn encode_sep<T: AsRef<[u8]>, U: AsRef<[u8]> + AsMut<[u8]>>( &self, data: &[T], parity: &mut [U], ) -> Result<(), Error>
Constructs the parity shards using separate data and parity references.
Sourcepub fn encode_single<T: AsRef<[u8]> + AsMut<[u8]>>(
&self,
i_data: usize,
shards: &mut [T],
) -> Result<(), Error>
pub fn encode_single<T: AsRef<[u8]> + AsMut<[u8]>>( &self, i_data: usize, shards: &mut [T], ) -> Result<(), Error>
Constructs the parity shards incrementally using a single data shard.
Must be called in order from i_data = 0 to data_shard_count - 1.
When i_data == 0, parity shards are overwritten. Otherwise, results
are XOR-accumulated.
Sourcepub fn encode_single_sep<U: AsRef<[u8]> + AsMut<[u8]>>(
&self,
i_data: usize,
single_data: &[u8],
parity: &mut [U],
) -> Result<(), Error>
pub fn encode_single_sep<U: AsRef<[u8]> + AsMut<[u8]>>( &self, i_data: usize, single_data: &[u8], parity: &mut [U], ) -> Result<(), Error>
Constructs the parity shards incrementally using a single data shard (separated).
Sourcepub fn verify<T: AsRef<[u8]>>(&self, shards: &[T]) -> Result<bool, Error>
pub fn verify<T: AsRef<[u8]>>(&self, shards: &[T]) -> Result<bool, Error>
Checks if the parity shards are correct.
Sourcepub fn reconstruct<T: ReconstructShard>(
&self,
shards: &mut [T],
) -> Result<(), Error>
pub fn reconstruct<T: ReconstructShard>( &self, shards: &mut [T], ) -> Result<(), Error>
Reconstructs all missing shards.
Shards that are None will be reconstructed. The number of present
shards must be >= data_shard_count.
Sourcepub fn reconstruct_data<T: ReconstructShard>(
&self,
shards: &mut [T],
) -> Result<(), Error>
pub fn reconstruct_data<T: ReconstructShard>( &self, shards: &mut [T], ) -> Result<(), Error>
Reconstructs only missing data shards.