mirror of https://git.sr.ht/~stygianentity/bincode
Added support for Range<T>, RangeInclusive<T> and Bound<T>
This commit is contained in:
parent
26d7683719
commit
9420d03762
|
|
@ -1,5 +1,6 @@
|
|||
use core::{
|
||||
cell::{Cell, RefCell},
|
||||
ops::{Bound, Range, RangeInclusive},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
|
|
@ -208,6 +209,47 @@ impl Decodable for Duration {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Decodable for Range<T>
|
||||
where
|
||||
T: Decodable,
|
||||
{
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
let min = T::decode(&mut decoder)?;
|
||||
let max = T::decode(&mut decoder)?;
|
||||
Ok(min..max)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Decodable for RangeInclusive<T>
|
||||
where
|
||||
T: Decodable,
|
||||
{
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
let min = T::decode(&mut decoder)?;
|
||||
let max = T::decode(&mut decoder)?;
|
||||
Ok(RangeInclusive::new(min, max))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Decodable for Bound<T>
|
||||
where
|
||||
T: Decodable,
|
||||
{
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
match u32::decode(&mut decoder)? {
|
||||
0 => Ok(Bound::Unbounded),
|
||||
1 => Ok(Bound::Included(T::decode(decoder)?)),
|
||||
2 => Ok(Bound::Excluded(T::decode(decoder)?)),
|
||||
x => Err(DecodeError::UnexpectedVariant {
|
||||
min: 0,
|
||||
max: 2,
|
||||
found: x,
|
||||
type_name: core::any::type_name::<Bound<T>>(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'de, T> Decode for &'a mut T
|
||||
where
|
||||
T: Decode,
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
use core::{
|
||||
cell::{Cell, RefCell},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use super::{Encode, Encodeable};
|
||||
use crate::error::EncodeError;
|
||||
use core::{
|
||||
cell::{Cell, RefCell},
|
||||
ops::{Bound, Range, RangeInclusive},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
impl Encodeable for bool {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
|
|
@ -206,6 +206,50 @@ impl Encodeable for Duration {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Encodeable for Range<T>
|
||||
where
|
||||
T: Encodeable,
|
||||
{
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
self.start.encode(&mut encoder)?;
|
||||
self.end.encode(&mut encoder)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Encodeable for RangeInclusive<T>
|
||||
where
|
||||
T: Encodeable,
|
||||
{
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
self.start().encode(&mut encoder)?;
|
||||
self.end().encode(&mut encoder)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Encodeable for Bound<T>
|
||||
where
|
||||
T: Encodeable,
|
||||
{
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
match self {
|
||||
Self::Unbounded => {
|
||||
0u32.encode(encoder)?;
|
||||
}
|
||||
Self::Included(val) => {
|
||||
1u32.encode(&mut encoder)?;
|
||||
val.encode(encoder)?;
|
||||
}
|
||||
Self::Excluded(val) => {
|
||||
2u32.encode(&mut encoder)?;
|
||||
val.encode(encoder)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Encodeable for &'a T
|
||||
where
|
||||
T: Encodeable,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
mod utils;
|
||||
|
||||
use core::cell::{Cell, RefCell};
|
||||
use core::ops::Bound;
|
||||
use core::time::Duration;
|
||||
use utils::the_same;
|
||||
|
||||
|
|
@ -76,6 +77,11 @@ fn test_numbers() {
|
|||
the_same(RefCell::<u32>::new(15));
|
||||
|
||||
the_same(Duration::new(5, 730023852));
|
||||
the_same(5u8..10u8);
|
||||
the_same(5u8..=10u8);
|
||||
the_same(Bound::<usize>::Unbounded);
|
||||
the_same(Bound::<usize>::Included(105));
|
||||
the_same(Bound::<usize>::Excluded(5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Reference in New Issue