mod sealed {
pub trait Sealed {}
}
pub trait Variant: sealed::Sealed {
#[cfg(not(feature = "cipher"))]
type Counter: Copy;
#[cfg(feature = "cipher")]
type Counter: cipher::StreamCipherCounter;
fn get_block_pos(row: &[u32]) -> Self::Counter;
fn set_block_pos(row: &mut [u32], pos: Self::Counter);
fn remaining_blocks(block_pos: Self::Counter) -> Option<usize>;
}
#[derive(Clone, Copy, Debug)]
pub enum Ietf {}
impl sealed::Sealed for Ietf {}
impl Variant for Ietf {
type Counter = u32;
#[inline(always)]
fn get_block_pos(row: &[u32]) -> u32 {
row[0]
}
#[inline(always)]
fn set_block_pos(row: &mut [u32], pos: u32) {
row[0] = pos;
}
#[inline(always)]
fn remaining_blocks(block_pos: u32) -> Option<usize> {
let remaining = u32::MAX - block_pos;
remaining.try_into().ok()
}
}
#[cfg(any(feature = "legacy", feature = "rng"))]
#[derive(Clone, Copy, Debug)]
pub enum Legacy {}
#[cfg(any(feature = "legacy", feature = "rng"))]
impl sealed::Sealed for Legacy {}
#[cfg(any(feature = "legacy", feature = "rng"))]
impl Variant for Legacy {
type Counter = u64;
#[inline(always)]
fn get_block_pos(row: &[u32]) -> u64 {
(u64::from(row[1]) << 32) | u64::from(row[0])
}
#[inline(always)]
fn set_block_pos(row: &mut [u32], pos: u64) {
row[0] = (pos & 0xFFFF_FFFF) as u32;
row[1] = (pos >> 32) as u32;
}
#[inline(always)]
fn remaining_blocks(block_pos: u64) -> Option<usize> {
let remaining = u64::MAX - block_pos;
remaining.try_into().ok()
}
}