use bytemuck::{Pod, Zeroable};
use num_enum::{IntoPrimitive, TryFromPrimitive};
#[cfg(feature = "wincode")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "wincode")]
use wincode_derive::{SchemaRead, SchemaWrite};
#[repr(u64)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum EncodingType {
#[default]
Unknown = 0,
Basic = 1,
Clay = 2,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Pod, Zeroable)]
#[cfg_attr(feature = "wincode", derive(Serialize, Deserialize, SchemaRead, SchemaWrite))]
pub struct EncodingProfile {
pub encoding: u64,
pub params: u64,
}
impl EncodingProfile {
#[inline]
pub fn encoding_type(&self) -> Option<EncodingType> {
EncodingType::try_from(self.encoding).ok()
}
#[inline]
pub fn is_clay(&self) -> bool {
matches!(self.encoding_type(), Some(EncodingType::Clay))
}
#[inline]
pub fn is_basic(&self) -> bool {
matches!(self.encoding_type(), Some(EncodingType::Basic))
}
#[inline]
pub const fn clay(params: ClayParams) -> Self {
Self {
encoding: EncodingType::Clay as u64,
params: params.as_u64(),
}
}
pub fn clay_default() -> Self {
Self::clay(ClayParams::default())
}
#[inline]
pub const fn clay_params(&self) -> ClayParams {
ClayParams::from_u64(self.params)
}
#[inline]
pub const fn basic(params: RSParams) -> Self {
Self {
encoding: EncodingType::Basic as u64,
params: params.as_u64(),
}
}
pub fn basic_default() -> Self {
Self::basic(RSParams::default())
}
#[inline]
pub const fn rs_params(&self) -> RSParams {
RSParams::from_u64(self.params)
}
#[inline]
pub fn k(&self) -> u8 {
match self.encoding_type() {
Some(EncodingType::Clay) => self.clay_params().k(),
Some(EncodingType::Basic) => self.rs_params().k(),
Some(EncodingType::Unknown) | None => panic!("cannot get k from Unknown encoding"),
}
}
#[inline]
pub fn m(&self) -> u8 {
match self.encoding_type() {
Some(EncodingType::Clay) => self.clay_params().m(),
Some(EncodingType::Basic) => self.rs_params().m(),
Some(EncodingType::Unknown) | None => panic!("cannot get m from Unknown encoding"),
}
}
#[inline]
pub fn n(&self) -> u8 {
self.k() + self.m()
}
pub fn pack(&self) -> [u8; 16] {
let mut out = [0u8; 16];
out[..8].copy_from_slice(&self.encoding.to_le_bytes());
out[8..16].copy_from_slice(&self.params.to_le_bytes());
out
}
pub fn unpack(data: [u8; 16]) -> Self {
Self {
encoding: u64::from_le_bytes(data[..8].try_into().unwrap()),
params: u64::from_le_bytes(data[8..16].try_into().unwrap()),
}
}
#[inline]
pub const fn unknown() -> Self {
Self {
encoding: EncodingType::Unknown as u64,
params: 0,
}
}
}
impl Default for EncodingProfile {
fn default() -> Self {
Self::clay_default()
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Pod, Zeroable)]
pub struct ClayParams {
packed: u64,
}
impl ClayParams {
#[inline]
pub const fn new(n: u8, k: u8, d: u8) -> Self {
Self {
packed: (n as u64) | ((k as u64) << 8) | ((d as u64) << 16),
}
}
#[inline]
pub const fn n(&self) -> u8 {
(self.packed & 0xFF) as u8
}
#[inline]
pub const fn k(&self) -> u8 {
((self.packed >> 8) & 0xFF) as u8
}
#[inline]
pub const fn d(&self) -> u8 {
((self.packed >> 16) & 0xFF) as u8
}
#[inline]
pub const fn m(&self) -> u8 {
self.n().saturating_sub(self.k())
}
#[inline]
pub const fn alpha(&self) -> u64 {
let q = (self.d() - self.k() + 1) as u64;
let n = self.n() as u64;
let t = (n + q - 1) / q;
let mut out = 1u64;
let mut i = 0;
while i < t {
out *= q;
i += 1;
}
out
}
#[inline]
pub const fn stripe_alignment(&self) -> u64 {
self.k() as u64 * self.alpha() * 2
}
#[inline]
pub const fn as_u64(&self) -> u64 {
self.packed
}
#[inline]
pub const fn from_u64(v: u64) -> Self {
Self { packed: v }
}
}
impl ClayParams {
pub const DEFAULT: Self = Self::new(20, 7, 16);
}
impl Default for ClayParams {
fn default() -> Self {
Self::DEFAULT
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Pod, Zeroable)]
pub struct RSParams {
packed: u64,
}
impl RSParams {
#[inline]
pub const fn new(k: u8, m: u8) -> Self {
let n = k + m;
Self {
packed: (n as u64) | ((k as u64) << 8),
}
}
#[inline]
pub const fn n(&self) -> u8 {
(self.packed & 0xFF) as u8
}
#[inline]
pub const fn k(&self) -> u8 {
((self.packed >> 8) & 0xFF) as u8
}
#[inline]
pub const fn m(&self) -> u8 {
self.n().saturating_sub(self.k())
}
#[inline]
pub const fn as_u64(&self) -> u64 {
self.packed
}
#[inline]
pub const fn from_u64(v: u64) -> Self {
Self { packed: v }
}
}
impl Default for RSParams {
fn default() -> Self {
Self::new(10, 10)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_clay_params_new() {
let params = ClayParams::new(20, 10, 19);
assert_eq!(params.n(), 20);
assert_eq!(params.k(), 10);
assert_eq!(params.d(), 19);
assert_eq!(params.m(), 10);
}
#[test]
fn test_clay_params_default() {
let params = ClayParams::default();
assert_eq!(params.n(), 20);
assert_eq!(params.k(), 7);
assert_eq!(params.d(), 16);
assert_eq!(params.m(), 13);
}
#[test]
fn test_clay_params_roundtrip() {
let params = ClayParams::new(20, 10, 19);
let packed = params.as_u64();
let recovered = ClayParams::from_u64(packed);
assert_eq!(params, recovered);
}
#[test]
fn test_encoding_profile_clay() {
let params = ClayParams::new(20, 10, 19);
let profile = EncodingProfile::clay(params);
assert!(profile.is_clay());
assert!(!profile.is_basic());
assert_eq!(profile.encoding_type(), Some(EncodingType::Clay));
assert_eq!(profile.clay_params(), params);
}
#[test]
fn test_encoding_profile_clay_default() {
let profile = EncodingProfile::clay_default();
assert!(profile.is_clay());
assert_eq!(profile.clay_params(), ClayParams::default());
}
#[test]
fn test_encoding_profile_basic() {
let params = RSParams::new(10, 10);
let profile = EncodingProfile::basic(params);
assert!(profile.is_basic());
assert!(!profile.is_clay());
assert_eq!(profile.encoding_type(), Some(EncodingType::Basic));
assert_eq!(profile.rs_params(), params);
}
#[test]
fn test_encoding_profile_basic_default() {
let profile = EncodingProfile::basic_default();
assert!(profile.is_basic());
assert_eq!(profile.rs_params(), RSParams::default());
assert_eq!(profile.k(), 10);
assert_eq!(profile.m(), 10);
assert_eq!(profile.n(), 20);
}
#[test]
fn test_rs_params_new() {
let params = RSParams::new(10, 10);
assert_eq!(params.n(), 20);
assert_eq!(params.k(), 10);
assert_eq!(params.m(), 10);
}
#[test]
fn test_rs_params_default() {
let params = RSParams::default();
assert_eq!(params.n(), 20);
assert_eq!(params.k(), 10);
assert_eq!(params.m(), 10);
}
#[test]
fn test_rs_params_roundtrip() {
let params = RSParams::new(8, 4);
let packed = params.as_u64();
let recovered = RSParams::from_u64(packed);
assert_eq!(params, recovered);
}
#[test]
fn test_rs_params_size() {
assert_eq!(std::mem::size_of::<RSParams>(), 8);
}
#[test]
fn test_profile_k_m_n_clay() {
let profile = EncodingProfile::clay_default();
assert_eq!(profile.k(), 7);
assert_eq!(profile.m(), 13);
assert_eq!(profile.n(), 20);
}
#[test]
fn test_profile_k_m_n_basic() {
let profile = EncodingProfile::basic(RSParams::new(8, 4));
assert_eq!(profile.k(), 8);
assert_eq!(profile.m(), 4);
assert_eq!(profile.n(), 12);
}
#[test]
#[should_panic(expected = "cannot get k from Unknown encoding")]
fn test_profile_k_unknown_panics() {
let profile = EncodingProfile::unknown();
let _ = profile.k();
}
#[test]
fn test_encoding_profile_unknown() {
let profile = EncodingProfile::unknown();
assert_eq!(profile.encoding_type(), Some(EncodingType::Unknown));
}
#[test]
fn test_encoding_profile_zeroed() {
let profile = EncodingProfile::zeroed();
assert_eq!(profile.encoding_type(), Some(EncodingType::Unknown));
}
#[test]
fn test_encoding_profile_size() {
assert_eq!(std::mem::size_of::<EncodingProfile>(), 16);
}
#[test]
fn test_clay_params_size() {
assert_eq!(std::mem::size_of::<ClayParams>(), 8);
}
#[test]
fn test_encoding_type_values() {
assert_eq!(EncodingType::Unknown as u64, 0);
assert_eq!(EncodingType::Basic as u64, 1);
assert_eq!(EncodingType::Clay as u64, 2);
}
#[test]
fn test_different_clay_params() {
let params1 = ClayParams::new(20, 6, 19);
assert_eq!(params1.k(), 6);
assert_eq!(params1.m(), 14);
let params2 = ClayParams::new(20, 14, 19);
assert_eq!(params2.k(), 14);
assert_eq!(params2.m(), 6);
assert_ne!(params1, params2);
}
}