From 9420d03762989c43bbb1d0b770fdca5fc197b8a4 Mon Sep 17 00:00:00 2001 From: Victor Koenders Date: Sat, 16 Oct 2021 13:59:48 +0200 Subject: [PATCH] Added support for Range, RangeInclusive and Bound --- src/de/impls.rs | 42 ++++++++++++++++++++++++++++++++++ src/enc/impls.rs | 54 ++++++++++++++++++++++++++++++++++++++++---- tests/basic_types.rs | 6 +++++ 3 files changed, 97 insertions(+), 5 deletions(-) diff --git a/src/de/impls.rs b/src/de/impls.rs index 4d715f3..f7584c0 100644 --- a/src/de/impls.rs +++ b/src/de/impls.rs @@ -1,5 +1,6 @@ use core::{ cell::{Cell, RefCell}, + ops::{Bound, Range, RangeInclusive}, time::Duration, }; @@ -208,6 +209,47 @@ impl Decodable for Duration { } } +impl Decodable for Range +where + T: Decodable, +{ + fn decode(mut decoder: D) -> Result { + let min = T::decode(&mut decoder)?; + let max = T::decode(&mut decoder)?; + Ok(min..max) + } +} + +impl Decodable for RangeInclusive +where + T: Decodable, +{ + fn decode(mut decoder: D) -> Result { + let min = T::decode(&mut decoder)?; + let max = T::decode(&mut decoder)?; + Ok(RangeInclusive::new(min, max)) + } +} + +impl Decodable for Bound +where + T: Decodable, +{ + fn decode(mut decoder: D) -> Result { + 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::>(), + }), + } + } +} + impl<'a, 'de, T> Decode for &'a mut T where T: Decode, diff --git a/src/enc/impls.rs b/src/enc/impls.rs index 3f1c63f..a452eb9 100644 --- a/src/enc/impls.rs +++ b/src/enc/impls.rs @@ -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(&self, mut encoder: E) -> Result<(), EncodeError> { @@ -206,6 +206,50 @@ impl Encodeable for Duration { } } +impl Encodeable for Range +where + T: Encodeable, +{ + fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { + self.start.encode(&mut encoder)?; + self.end.encode(&mut encoder)?; + Ok(()) + } +} + +impl Encodeable for RangeInclusive +where + T: Encodeable, +{ + fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { + self.start().encode(&mut encoder)?; + self.end().encode(&mut encoder)?; + Ok(()) + } +} + +impl Encodeable for Bound +where + T: Encodeable, +{ + fn 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, diff --git a/tests/basic_types.rs b/tests/basic_types.rs index c3cb4e1..a3c8d21 100644 --- a/tests/basic_types.rs +++ b/tests/basic_types.rs @@ -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::::new(15)); the_same(Duration::new(5, 730023852)); + the_same(5u8..10u8); + the_same(5u8..=10u8); + the_same(Bound::::Unbounded); + the_same(Bound::::Included(105)); + the_same(Bound::::Excluded(5)); } #[test]