Implement Encode & Decode for Wrapping<T> types (#686)

* Implement Encode & Decode for Wrapping<T> types

* Implement Encode & Decode for Reverse<T> types
This commit is contained in:
mzachar 2023-12-15 07:45:27 +01:00 committed by GitHub
parent 8f3f84ae94
commit 67564d2bd4
3 changed files with 70 additions and 2 deletions

View File

@ -11,11 +11,12 @@ use core::{
cell::{Cell, RefCell},
num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, Wrapping,
},
ops::{Bound, Range, RangeInclusive},
time::Duration,
};
use std::cmp::Reverse;
impl Decode for bool {
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
@ -394,6 +395,29 @@ impl Decode for f64 {
}
impl_borrow_decode!(f64);
impl<T: Decode> Decode for Wrapping<T> {
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
Ok(Wrapping(T::decode(decoder)?))
}
}
impl<'de, T: BorrowDecode<'de>> BorrowDecode<'de> for Wrapping<T> {
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: &mut D) -> Result<Self, DecodeError> {
Ok(Wrapping(T::borrow_decode(decoder)?))
}
}
impl<T: Decode> Decode for Reverse<T> {
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
Ok(Reverse(T::decode(decoder)?))
}
}
impl<'de, T: BorrowDecode<'de>> BorrowDecode<'de> for Reverse<T> {
fn borrow_decode<D: BorrowDecoder<'de>>(decoder: &mut D) -> Result<Self, DecodeError> {
Ok(Reverse(T::borrow_decode(decoder)?))
}
}
impl Decode for char {
fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
let mut array = [0u8; 4];

View File

@ -8,11 +8,12 @@ use core::{
marker::PhantomData,
num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, Wrapping,
},
ops::{Bound, Range, RangeInclusive},
time::Duration,
};
use std::cmp::Reverse;
impl Encode for () {
fn encode<E: Encoder>(&self, _: &mut E) -> Result<(), EncodeError> {
@ -274,6 +275,18 @@ impl Encode for f64 {
}
}
impl<T: Encode> Encode for Wrapping<T> {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
self.0.encode(encoder)
}
}
impl<T: Encode> Encode for Reverse<T> {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
self.0.encode(encoder)
}
}
impl Encode for char {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
encode_utf8(encoder.writer(), *self)

View File

@ -4,6 +4,7 @@ use bincode::error::DecodeError;
use core::cell::{Cell, RefCell};
use core::ops::Bound;
use core::time::Duration;
use std::cmp::Reverse;
use std::num::*;
use utils::{the_same, the_same_with_comparer};
@ -113,6 +114,36 @@ fn test_numbers() {
the_same(NonZeroI128::new(12345));
the_same(NonZeroIsize::new(0));
the_same(NonZeroIsize::new(12345));
// Wrapping types
the_same(Wrapping(5u8));
the_same(Wrapping(5u16));
the_same(Wrapping(5u32));
the_same(Wrapping(5u64));
the_same(Wrapping(5u128));
the_same(Wrapping(5usize));
the_same(Wrapping(5i8));
the_same(Wrapping(5i16));
the_same(Wrapping(5i32));
the_same(Wrapping(5i64));
the_same(Wrapping(5i128));
the_same(Wrapping(5isize));
// Reverse types
the_same(Reverse(5u8));
the_same(Reverse(5u16));
the_same(Reverse(5u32));
the_same(Reverse(5u64));
the_same(Reverse(5u128));
the_same(Reverse(5usize));
the_same(Reverse(5i8));
the_same(Reverse(5i16));
the_same(Reverse(5i32));
the_same(Reverse(5i64));
the_same(Reverse(5i128));
the_same(Reverse(5isize));
}
#[test]