use core::convert::Infallible;
use core::marker::PhantomData;
use core::num::NonZeroUsize;
use qubit_codec::BigEndian;
use qubit_codec::ByteOrderSpec;
use qubit_codec::Codec;
use qubit_codec::DecodeFailure;
use qubit_codec::LittleEndian;
use qubit_codec::NativeEndian;
use qubit_utils::UncheckedSlice;
use qubit_utils::nonzero;
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct BinaryCodec<T, O> {
marker: PhantomData<fn() -> (T, O)>,
}
impl<O> BinaryCodec<u8, O>
where
O: ByteOrderSpec,
{
pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
#[must_use]
#[inline]
pub unsafe fn decode(input: &[u8], input_index: usize) -> (u8, NonZeroUsize) {
debug_assert!(input_index + Self::MIN_UNITS_PER_VALUE <= input.len());
(
unsafe { UncheckedSlice::read(input, input_index) },
nonzero(Self::MIN_UNITS_PER_VALUE),
)
}
#[inline]
pub unsafe fn encode(value: u8, output: &mut [u8], output_index: usize) -> usize {
debug_assert!(output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len());
unsafe {
UncheckedSlice::write(output, output_index, value);
}
Self::MAX_ENCODE_UNITS_PER_VALUE
}
}
impl<O> Codec for BinaryCodec<u8, O>
where
O: ByteOrderSpec,
{
type Value = u8;
type Unit = u8;
type DecodeError = Infallible;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = 1;
const MAX_ENCODE_UNITS_PER_VALUE: usize = 1;
const MAX_DECODE_UNITS_PER_VALUE: usize = 1;
#[inline]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<(u8, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline]
unsafe fn encode(
&mut self,
value: &u8,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError> {
unsafe {
Self::encode(*value, output, output_index);
}
Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
}
}
impl<O> BinaryCodec<i8, O>
where
O: ByteOrderSpec,
{
pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
#[must_use]
#[inline]
pub unsafe fn decode(input: &[u8], input_index: usize) -> (i8, NonZeroUsize) {
debug_assert!(input_index + Self::MIN_UNITS_PER_VALUE <= input.len());
(
unsafe { UncheckedSlice::read(input, input_index) } as i8,
nonzero(Self::MIN_UNITS_PER_VALUE),
)
}
#[inline]
pub unsafe fn encode(value: i8, output: &mut [u8], output_index: usize) -> usize {
debug_assert!(output_index + Self::MAX_ENCODE_UNITS_PER_VALUE <= output.len());
unsafe {
UncheckedSlice::write(output, output_index, value as u8);
}
Self::MAX_ENCODE_UNITS_PER_VALUE
}
}
impl<O> Codec for BinaryCodec<i8, O>
where
O: ByteOrderSpec,
{
type Value = i8;
type Unit = u8;
type DecodeError = Infallible;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = 1;
const MAX_ENCODE_UNITS_PER_VALUE: usize = 1;
const MAX_DECODE_UNITS_PER_VALUE: usize = 1;
#[inline]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<(i8, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline]
unsafe fn encode(
&mut self,
value: &i8,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError> {
unsafe {
Self::encode(*value, output, output_index);
}
Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
}
}
macro_rules! impl_integer_binary_codec {
($ty:ty, $len:expr) => {
impl BinaryCodec<$ty, BigEndian> {
pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
#[must_use]
#[inline]
pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
debug_assert!(
input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE,
"BinaryCodec decode requires sufficient readable bytes",
);
let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
(<$ty>::from_be(raw), nonzero(Self::MIN_UNITS_PER_VALUE))
}
#[inline]
pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);
let raw = value.to_be();
unsafe {
UncheckedSlice::write_ne_unaligned::<$ty>(output, output_index, raw);
}
Self::MAX_ENCODE_UNITS_PER_VALUE
}
}
impl Codec for BinaryCodec<$ty, BigEndian> {
type Value = $ty;
type Unit = u8;
type DecodeError = Infallible;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = $len;
const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
#[inline]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline]
unsafe fn encode(
&mut self,
value: &$ty,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError> {
unsafe {
Self::encode(*value, output, output_index);
}
Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
}
}
impl BinaryCodec<$ty, LittleEndian> {
pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
#[must_use]
#[inline]
pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);
let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
(<$ty>::from_le(raw), nonzero(Self::MIN_UNITS_PER_VALUE))
}
#[inline]
pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
debug_assert!(
output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE,
"BinaryCodec encode requires sufficient writable bytes",
);
let raw = value.to_le();
unsafe {
UncheckedSlice::write_ne_unaligned::<$ty>(output, output_index, raw);
}
Self::MAX_ENCODE_UNITS_PER_VALUE
}
}
impl Codec for BinaryCodec<$ty, LittleEndian> {
type Value = $ty;
type Unit = u8;
type DecodeError = Infallible;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = $len;
const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
#[inline]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline]
unsafe fn encode(
&mut self,
value: &$ty,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError> {
unsafe {
Self::encode(*value, output, output_index);
}
Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
}
}
};
}
macro_rules! impl_float_binary_codec {
($ty:ty, $bits:ty, $len:expr) => {
impl BinaryCodec<$ty, BigEndian> {
pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
#[must_use]
#[inline]
pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
debug_assert!(
input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE,
"BinaryCodec decode requires sufficient readable bytes",
);
let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
(
<$ty>::from_bits(<$bits>::from_be(raw)),
nonzero(Self::MIN_UNITS_PER_VALUE),
)
}
#[inline]
pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);
let raw = value.to_bits().to_be();
unsafe {
UncheckedSlice::write_ne_unaligned::<$bits>(output, output_index, raw);
}
Self::MAX_ENCODE_UNITS_PER_VALUE
}
}
impl Codec for BinaryCodec<$ty, BigEndian> {
type Value = $ty;
type Unit = u8;
type DecodeError = Infallible;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = $len;
const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
#[inline]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline]
unsafe fn encode(
&mut self,
value: &$ty,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError> {
unsafe {
Self::encode(*value, output, output_index);
}
Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
}
}
impl BinaryCodec<$ty, LittleEndian> {
pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
#[must_use]
#[inline]
pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);
let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
(
<$ty>::from_bits(<$bits>::from_le(raw)),
nonzero(Self::MIN_UNITS_PER_VALUE),
)
}
#[inline]
pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
debug_assert!(
output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE,
"BinaryCodec encode requires sufficient writable bytes",
);
let raw = value.to_bits().to_le();
unsafe {
UncheckedSlice::write_ne_unaligned::<$bits>(output, output_index, raw);
}
Self::MAX_ENCODE_UNITS_PER_VALUE
}
}
impl Codec for BinaryCodec<$ty, LittleEndian> {
type Value = $ty;
type Unit = u8;
type DecodeError = Infallible;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = $len;
const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
#[inline]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline]
unsafe fn encode(
&mut self,
value: &$ty,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError> {
unsafe {
Self::encode(*value, output, output_index);
}
Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
}
}
};
}
macro_rules! impl_native_integer_binary_codec {
($ty:ty, $len:expr) => {
impl BinaryCodec<$ty, NativeEndian> {
pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
#[must_use]
#[inline]
pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
debug_assert!(
input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE,
"BinaryCodec decode requires sufficient readable bytes",
);
let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
(raw, nonzero(Self::MIN_UNITS_PER_VALUE))
}
#[inline]
pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
debug_assert!(output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE);
unsafe { UncheckedSlice::write_ne_unaligned(output, output_index, value) };
Self::MAX_ENCODE_UNITS_PER_VALUE
}
}
impl Codec for BinaryCodec<$ty, NativeEndian> {
type Value = $ty;
type Unit = u8;
type DecodeError = Infallible;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = $len;
const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
#[inline]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline]
unsafe fn encode(
&mut self,
value: &$ty,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError> {
unsafe { Self::encode(*value, output, output_index) };
Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
}
}
};
}
macro_rules! impl_native_float_binary_codec {
($ty:ty, $bits:ty, $len:expr) => {
impl BinaryCodec<$ty, NativeEndian> {
pub const MIN_UNITS_PER_VALUE: usize = <Self as Codec>::MIN_UNITS_PER_VALUE;
pub const MAX_ENCODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_ENCODE_UNITS_PER_VALUE;
pub const MAX_DECODE_UNITS_PER_VALUE: usize = <Self as Codec>::MAX_DECODE_UNITS_PER_VALUE;
#[must_use]
#[inline]
pub unsafe fn decode(input: &[u8], input_index: usize) -> ($ty, NonZeroUsize) {
debug_assert!(input.len().saturating_sub(input_index) >= Self::MIN_UNITS_PER_VALUE);
let raw = unsafe { UncheckedSlice::read_ne_unaligned(input, input_index) };
(<$ty>::from_bits(raw), nonzero(Self::MIN_UNITS_PER_VALUE))
}
#[inline]
pub unsafe fn encode(value: $ty, output: &mut [u8], output_index: usize) -> usize {
debug_assert!(
output.len().saturating_sub(output_index) >= Self::MAX_ENCODE_UNITS_PER_VALUE,
"BinaryCodec encode requires sufficient writable bytes",
);
unsafe { UncheckedSlice::write_ne_unaligned(output, output_index, value.to_bits()) };
Self::MAX_ENCODE_UNITS_PER_VALUE
}
}
impl Codec for BinaryCodec<$ty, NativeEndian> {
type Value = $ty;
type Unit = u8;
type DecodeError = Infallible;
type EncodeError = Infallible;
const MIN_UNITS_PER_VALUE: usize = $len;
const MAX_ENCODE_UNITS_PER_VALUE: usize = $len;
const MAX_DECODE_UNITS_PER_VALUE: usize = $len;
#[inline]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<($ty, NonZeroUsize), DecodeFailure<Self::DecodeError>> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline]
unsafe fn encode(
&mut self,
value: &$ty,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError> {
unsafe { Self::encode(*value, output, output_index) };
Ok(Self::MAX_ENCODE_UNITS_PER_VALUE)
}
}
};
}
impl_integer_binary_codec!(u16, 2);
impl_integer_binary_codec!(u32, 4);
impl_integer_binary_codec!(u64, 8);
impl_integer_binary_codec!(u128, 16);
impl_integer_binary_codec!(i16, 2);
impl_integer_binary_codec!(i32, 4);
impl_integer_binary_codec!(i64, 8);
impl_integer_binary_codec!(i128, 16);
impl_float_binary_codec!(f32, u32, 4);
impl_float_binary_codec!(f64, u64, 8);
impl_native_integer_binary_codec!(u16, 2);
impl_native_integer_binary_codec!(u32, 4);
impl_native_integer_binary_codec!(u64, 8);
impl_native_integer_binary_codec!(u128, 16);
impl_native_integer_binary_codec!(i16, 2);
impl_native_integer_binary_codec!(i32, 4);
impl_native_integer_binary_codec!(i64, 8);
impl_native_integer_binary_codec!(i128, 16);
impl_native_float_binary_codec!(f32, u32, 4);
impl_native_float_binary_codec!(f64, u64, 8);