use crate::{
codec::{Encode, Decode},
decoder::BufDecoder,
encoder::BufEncoder,
error::DecodeError
};
impl Encode for u8 {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u8(*self);
}
}
impl Decode for u8 {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
dec.read_u8()
}
}
impl Encode for i8 {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u8(*self as u8);
}
}
impl Decode for i8 {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
dec.read_u8().map(|b| b as i8)
}
}
impl Encode for u16 {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u16(*self);
}
}
impl Decode for u16 {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
dec.read_u16()
}
}
impl Encode for i16 {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u16(*self as u16);
}
}
impl Decode for i16 {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
dec.read_u16().map(|v| v as i16) }
}
impl Encode for u32 {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u32(*self);
}
}
impl Decode for u32 {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
dec.read_u32()
}
}
impl Encode for i32 {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u32(*self as u32);
}
}
impl Decode for i32 {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
dec.read_u32().map(|v| v as i32)
}
}
impl Encode for u64 {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u64(*self);
}
}
impl Decode for u64 {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
dec.read_u64()
}
}
impl Encode for i64 {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u64(*self as u64);
}
}
impl Decode for i64 {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
dec.read_u64().map(|v| v as i64)
}
}
impl Encode for f32 {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u32(self.to_bits());
}
}
impl Decode for f32 {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
dec.read_u32().map(f32::from_bits)
}
}
impl Encode for f64 {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u64(self.to_bits());
}
}
impl Decode for f64 {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
dec.read_u64().map(f64::from_bits)
}
}
impl Encode for bool {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u8(*self as u8)
}
}
impl Decode for bool {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
match dec.read_u8()? {
0 => Ok(false),
1 => Ok(true),
b => Err(DecodeError::InvalidBool(b))
}
}
}
impl Encode for char {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
enc.write_u32(*self as u32);
}
}
impl Decode for char {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
let n = dec.read_u32()?;
char::from_u32(n).ok_or(DecodeError::InvalidChar(n))
}
}
impl<T: Encode, const N: usize> Encode for [T; N] {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
for item in self {
item.encode(enc);
}
}
}
impl<T: Decode, const N: usize> Decode for [T; N] {
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
let mut arr: [core::mem::MaybeUninit<T>; N] =
unsafe { core::mem::MaybeUninit::uninit().assume_init() };
for slot in &mut arr {
slot.write(T::decode(dec)?);
}
Ok(arr.map(|s| unsafe { s.assume_init() }))
}
}
macro_rules! impl_tuple {
($($T:ident : $idx:tt),+) => {
impl<$($T: Encode),+> Encode for ($($T,)+) {
#[inline]
fn encode(&self, enc: &mut BufEncoder) {
$(self.$idx.encode(enc);)+
}
}
impl<$($T: Decode),+> Decode for ($($T,)+) {
#[inline]
fn decode(dec: &mut BufDecoder<'_>) -> Result<Self, DecodeError> {
Ok(($($T::decode(dec)?,)+))
}
}
};
}
impl_tuple!(A:0);
impl_tuple!(A:0, B:1);
impl_tuple!(A:0, B:1, C:2);
impl_tuple!(A:0, B:1, C:2, D:3);
impl_tuple!(A:0, B:1, C:2, D:3, E:4);
impl_tuple!(A:0, B:1, C:2, D:3, E:4, F:5);
impl_tuple!(A:0, B:1, C:2, D:3, E:4, F:5, G:6);
impl_tuple!(A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7);