Skip to main content

ReedSolomon

Struct ReedSolomon 

Source
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

Source

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.

Source

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.

Source

pub fn data_shard_count(&self) -> usize

Source

pub fn parity_shard_count(&self) -> usize

Source

pub fn total_shard_count(&self) -> usize

Source

pub fn encode<T: AsRef<[u8]> + AsMut<[u8]>>( &self, shards: &mut [T], ) -> Result<(), Error>

Source

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.

Source

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.

Source

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).

Source

pub fn verify<T: AsRef<[u8]>>(&self, shards: &[T]) -> Result<bool, Error>

Checks if the parity shards are correct.

Source

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.

Source

pub fn reconstruct_data<T: ReconstructShard>( &self, shards: &mut [T], ) -> Result<(), Error>

Reconstructs only missing data shards.

Trait Implementations§

Source§

impl Clone for ReedSolomon

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ReedSolomon

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for ReedSolomon

Source§

fn eq(&self, rhs: &ReedSolomon) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.