use core::{
convert::Infallible,
marker::PhantomData,
};
use qubit_codec::{
BigEndian,
ByteOrderSpec,
Codec,
LittleEndian,
NativeEndian,
};
use qubit_io::UncheckedSlice;
#[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(always)]
pub unsafe fn decode(
input: &[u8],
input_index: usize,
) -> (u8, core::num::NonZeroUsize) {
debug_assert!(input_index + Self::MIN_UNITS_PER_VALUE <= input.len());
(
unsafe { qubit_io::UncheckedSlice::read(input, input_index) },
qubit_codec::nz!(Self::MIN_UNITS_PER_VALUE),
)
}
#[inline(always)]
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 {
qubit_io::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(always)]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<
(u8, core::num::NonZeroUsize),
qubit_codec::DecodeFailure<Self::DecodeError>,
> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline(always)]
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(always)]
pub unsafe fn decode(
input: &[u8],
input_index: usize,
) -> (i8, core::num::NonZeroUsize) {
debug_assert!(input_index + Self::MIN_UNITS_PER_VALUE <= input.len());
(
unsafe { UncheckedSlice::read(input, input_index) } as i8,
qubit_codec::nz!(Self::MIN_UNITS_PER_VALUE),
)
}
#[inline(always)]
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 {
qubit_io::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(always)]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<
(i8, core::num::NonZeroUsize),
qubit_codec::DecodeFailure<Self::DecodeError>,
> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline(always)]
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(always)]
pub unsafe fn decode(
input: &[u8],
input_index: usize,
) -> ($ty, core::num::NonZeroUsize) {
let raw = unsafe {
UncheckedSlice::read_ne_unaligned(input, input_index)
};
(
<$ty>::from_be(raw),
qubit_codec::nz!(Self::MIN_UNITS_PER_VALUE),
)
}
#[inline(always)]
pub unsafe fn encode(
value: $ty,
output: &mut [u8],
output_index: usize,
) -> usize {
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(always)]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<
($ty, core::num::NonZeroUsize),
qubit_codec::DecodeFailure<Self::DecodeError>,
> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline(always)]
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(always)]
pub unsafe fn decode(
input: &[u8],
input_index: usize,
) -> ($ty, core::num::NonZeroUsize) {
let raw = unsafe {
UncheckedSlice::read_ne_unaligned(input, input_index)
};
(
<$ty>::from_le(raw),
qubit_codec::nz!(Self::MIN_UNITS_PER_VALUE),
)
}
#[inline(always)]
pub unsafe fn encode(
value: $ty,
output: &mut [u8],
output_index: usize,
) -> usize {
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(always)]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<
($ty, core::num::NonZeroUsize),
qubit_codec::DecodeFailure<Self::DecodeError>,
> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline(always)]
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(always)]
pub unsafe fn decode(
input: &[u8],
input_index: usize,
) -> ($ty, core::num::NonZeroUsize) {
let raw = unsafe {
UncheckedSlice::read_ne_unaligned(input, input_index)
};
(
<$ty>::from_bits(<$bits>::from_be(raw)),
qubit_codec::nz!(Self::MIN_UNITS_PER_VALUE),
)
}
#[inline(always)]
pub unsafe fn encode(
value: $ty,
output: &mut [u8],
output_index: usize,
) -> usize {
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(always)]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<
($ty, core::num::NonZeroUsize),
qubit_codec::DecodeFailure<Self::DecodeError>,
> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline(always)]
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(always)]
pub unsafe fn decode(
input: &[u8],
input_index: usize,
) -> ($ty, core::num::NonZeroUsize) {
let raw = unsafe {
UncheckedSlice::read_ne_unaligned(input, input_index)
};
(
<$ty>::from_bits(<$bits>::from_le(raw)),
qubit_codec::nz!(Self::MIN_UNITS_PER_VALUE),
)
}
#[inline(always)]
pub unsafe fn encode(
value: $ty,
output: &mut [u8],
output_index: usize,
) -> usize {
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(always)]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<
($ty, core::num::NonZeroUsize),
qubit_codec::DecodeFailure<Self::DecodeError>,
> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline(always)]
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(always)]
pub unsafe fn decode(
input: &[u8],
input_index: usize,
) -> ($ty, core::num::NonZeroUsize) {
let raw = unsafe {
UncheckedSlice::read_ne_unaligned(input, input_index)
};
(raw, qubit_codec::nz!(Self::MIN_UNITS_PER_VALUE))
}
#[inline(always)]
pub unsafe fn encode(
value: $ty,
output: &mut [u8],
output_index: usize,
) -> usize {
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(always)]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<
($ty, core::num::NonZeroUsize),
qubit_codec::DecodeFailure<Self::DecodeError>,
> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline(always)]
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(always)]
pub unsafe fn decode(
input: &[u8],
input_index: usize,
) -> ($ty, core::num::NonZeroUsize) {
let raw = unsafe {
UncheckedSlice::read_ne_unaligned(input, input_index)
};
(
<$ty>::from_bits(raw),
qubit_codec::nz!(Self::MIN_UNITS_PER_VALUE),
)
}
#[inline(always)]
pub unsafe fn encode(
value: $ty,
output: &mut [u8],
output_index: usize,
) -> usize {
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(always)]
unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<
($ty, core::num::NonZeroUsize),
qubit_codec::DecodeFailure<Self::DecodeError>,
> {
Ok(unsafe { Self::decode(input, input_index) })
}
#[inline(always)]
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);