reed_solomon_16/rate.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
//! Advanced encoding/decoding using chosen [`Engine`] and [`Rate`].
//!
//! **This is an advanced module which is not needed for [simple usage] or [basic usage].**
//!
//! This module is relevant if you want to
//! - encode/decode using other [`Engine`] than [`DefaultEngine`].
//! - re-use working space of one encoder/decoder in another.
//! - understand/benchmark/test high or low rate directly.
//!
//! # Rates
//!
//! See [algorithm > Rate] for details about high/low rate.
//!
//! - [`DefaultRate`], [`DefaultRateEncoder`], [`DefaultRateDecoder`]
//! - Encoding/decoding using high or low rate as appropriate.
//! - These are basically same as [`ReedSolomonEncoder`]
//! and [`ReedSolomonDecoder`] except with slightly different API
//! which allows specifying [`Engine`] and working space.
//! - [`HighRate`], [`HighRateEncoder`], [`HighRateDecoder`]
//! - Encoding/decoding using only high rate.
//! - [`LowRate`], [`LowRateEncoder`], [`LowRateDecoder`]
//! - Encoding/decoding using only low rate.
//!
//! [simple usage]: crate#simple-usage
//! [basic usage]: crate#basic-usage
//! [algorithm > Rate]: crate::algorithm#rate
//! [`ReedSolomonEncoder`]: crate::ReedSolomonEncoder
//! [`ReedSolomonDecoder`]: crate::ReedSolomonDecoder
//! [`DefaultEngine`]: crate::engine::DefaultEngine
use crate::{engine::Engine, DecoderResult, EncoderResult, Error};
pub use self::{
decoder_work::DecoderWork,
encoder_work::EncoderWork,
rate_default::{DefaultRate, DefaultRateDecoder, DefaultRateEncoder},
rate_high::{HighRate, HighRateDecoder, HighRateEncoder},
rate_low::{LowRate, LowRateDecoder, LowRateEncoder},
};
mod decoder_work;
mod encoder_work;
mod rate_default;
mod rate_high;
mod rate_low;
// ======================================================================
// Rate - PUBLIC
/// Reed-Solomon encoder/decoder generator using specific rate.
pub trait Rate<E: Engine> {
// ============================================================
// REQUIRED
/// Encoder of this rate.
type RateEncoder: RateEncoder<E>;
/// Decoder of this rate.
type RateDecoder: RateDecoder<E>;
/// Returns `true` if given `original_count` / `recovery_count`
/// combination is supported.
fn supports(original_count: usize, recovery_count: usize) -> bool;
// ============================================================
// PROVIDED
/// Creates new encoder. This is same as [`RateEncoder::new`].
fn encoder(
original_count: usize,
recovery_count: usize,
shard_bytes: usize,
engine: E,
work: Option<EncoderWork>,
) -> Result<Self::RateEncoder, Error> {
Self::RateEncoder::new(original_count, recovery_count, shard_bytes, engine, work)
}
/// Creates new decoder. This is same as [`RateDecoder::new`].
fn decoder(
original_count: usize,
recovery_count: usize,
shard_bytes: usize,
engine: E,
work: Option<DecoderWork>,
) -> Result<Self::RateDecoder, Error> {
Self::RateDecoder::new(original_count, recovery_count, shard_bytes, engine, work)
}
/// Returns `Ok(())` if given `original_count` / `recovery_count`
/// combination is supported and given `shard_bytes` is valid.
fn validate(
original_count: usize,
recovery_count: usize,
shard_bytes: usize,
) -> Result<(), Error> {
if !Self::supports(original_count, recovery_count) {
Err(Error::UnsupportedShardCount {
original_count,
recovery_count,
})
} else if shard_bytes == 0 || shard_bytes & 63 != 0 {
Err(Error::InvalidShardSize { shard_bytes })
} else {
Ok(())
}
}
}
// ======================================================================
// RateEncoder - PUBLIC
/// Reed-Solomon encoder using specific rate.
pub trait RateEncoder<E: Engine>
where
Self: Sized,
{
// ============================================================
// REQUIRED
/// Rate of this encoder.
type Rate: Rate<E>;
/// Like [`ReedSolomonEncoder::add_original_shard`](crate::ReedSolomonEncoder::add_original_shard).
fn add_original_shard<T: AsRef<[u8]>>(&mut self, original_shard: T) -> Result<(), Error>;
/// Like [`ReedSolomonEncoder::encode`](crate::ReedSolomonEncoder::encode).
fn encode(&mut self) -> Result<EncoderResult, Error>;
/// Consumes this encoder returning its [`Engine`] and [`EncoderWork`]
/// so that they can be re-used by another encoder.
fn into_parts(self) -> (E, EncoderWork);
/// Like [`ReedSolomonEncoder::new`](crate::ReedSolomonEncoder::new)
/// with [`Engine`] to use and optional working space to be re-used.
fn new(
original_count: usize,
recovery_count: usize,
shard_bytes: usize,
engine: E,
work: Option<EncoderWork>,
) -> Result<Self, Error>;
/// Like [`ReedSolomonEncoder::reset`](crate::ReedSolomonEncoder::reset).
fn reset(
&mut self,
original_count: usize,
recovery_count: usize,
shard_bytes: usize,
) -> Result<(), Error>;
// ============================================================
// PROVIDED
/// Returns `true` if given `original_count` / `recovery_count`
/// combination is supported.
///
/// This is same as [`Rate::supports`].
fn supports(original_count: usize, recovery_count: usize) -> bool {
Self::Rate::supports(original_count, recovery_count)
}
/// Returns `Ok(())` if given `original_count` / `recovery_count`
/// combination is supported and given `shard_bytes` is valid.
///
/// This is same as [`Rate::validate`].
fn validate(
original_count: usize,
recovery_count: usize,
shard_bytes: usize,
) -> Result<(), Error> {
Self::Rate::validate(original_count, recovery_count, shard_bytes)
}
}
// ======================================================================
// RateDecoder - PUBLIC
/// Reed-Solomon decoder using specific rate.
pub trait RateDecoder<E: Engine>
where
Self: Sized,
{
// ============================================================
// REQUIRED
/// Rate of this decoder.
type Rate: Rate<E>;
/// Like [`ReedSolomonDecoder::add_original_shard`](crate::ReedSolomonDecoder::add_original_shard).
fn add_original_shard<T: AsRef<[u8]>>(
&mut self,
index: usize,
original_shard: T,
) -> Result<(), Error>;
/// Like [`ReedSolomonDecoder::add_recovery_shard`](crate::ReedSolomonDecoder::add_recovery_shard).
fn add_recovery_shard<T: AsRef<[u8]>>(
&mut self,
index: usize,
recovery_shard: T,
) -> Result<(), Error>;
/// Like [`ReedSolomonDecoder::decode`](crate::ReedSolomonDecoder::decode).
fn decode(&mut self) -> Result<DecoderResult, Error>;
/// Consumes this decoder returning its [`Engine`] and [`DecoderWork`]
/// so that they can be re-used by another decoder.
fn into_parts(self) -> (E, DecoderWork);
/// Like [`ReedSolomonDecoder::new`](crate::ReedSolomonDecoder::new)
/// with [`Engine`] to use and optional working space to be re-used.
fn new(
original_count: usize,
recovery_count: usize,
shard_bytes: usize,
engine: E,
work: Option<DecoderWork>,
) -> Result<Self, Error>;
/// Like [`ReedSolomonDecoder::reset`](crate::ReedSolomonDecoder::reset).
fn reset(
&mut self,
original_count: usize,
recovery_count: usize,
shard_bytes: usize,
) -> Result<(), Error>;
// ============================================================
// PROVIDED
/// Returns `true` if given `original_count` / `recovery_count`
/// combination is supported.
///
/// This is same as [`Rate::supports`].
fn supports(original_count: usize, recovery_count: usize) -> bool {
Self::Rate::supports(original_count, recovery_count)
}
/// Returns `Ok(())` if given `original_count` / `recovery_count`
/// combination is supported and given `shard_bytes` is valid.
///
/// This is same as [`Rate::validate`].
fn validate(
original_count: usize,
recovery_count: usize,
shard_bytes: usize,
) -> Result<(), Error> {
Self::Rate::validate(original_count, recovery_count, shard_bytes)
}
}