Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/de/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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];
Expand Down
15 changes: 14 additions & 1 deletion src/enc/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions tests/basic_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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]
Expand Down
Loading