From a0469e08ef78389bf98c368993983f4b9356d43b Mon Sep 17 00:00:00 2001 From: Victor Koenders Date: Sat, 16 Oct 2021 14:14:15 +0200 Subject: [PATCH] Added support for NonZero* types --- src/de/impls.rs | 145 ++++++++++++++++++++++++++++------ src/enc/impls.rs | 76 ++++++++++++++++++ src/error.rs | 16 +++- src/varint/decode_unsigned.rs | 2 +- tests/basic_types.rs | 28 +++++++ 5 files changed, 242 insertions(+), 25 deletions(-) diff --git a/src/de/impls.rs b/src/de/impls.rs index f7584c0..1b6bfc1 100644 --- a/src/de/impls.rs +++ b/src/de/impls.rs @@ -1,13 +1,16 @@ +use super::{BorrowDecodable, BorrowDecode, Decodable, Decode}; +use crate::error::{DecodeError, IntegerType}; use core::{ cell::{Cell, RefCell}, + num::{ + NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128, + NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, + }, ops::{Bound, Range, RangeInclusive}, time::Duration, }; -use super::{BorrowDecodable, BorrowDecode, Decodable, Decode}; -use crate::error::DecodeError; - -impl<'de> Decodable for bool { +impl Decodable for bool { fn decode(mut decoder: D) -> Result { match decoder.decode_u8()? { 0 => Ok(false), @@ -17,91 +20,187 @@ impl<'de> Decodable for bool { } } -impl<'de> Decodable for u8 { +impl Decodable for u8 { fn decode(mut decoder: D) -> Result { decoder.decode_u8() } } -impl<'de> Decodable for u16 { +impl Decodable for NonZeroU8 { + fn decode(mut decoder: D) -> Result { + NonZeroU8::new(decoder.decode_u8()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::U8, + }) + } +} + +impl Decodable for u16 { fn decode(mut decoder: D) -> Result { decoder.decode_u16() } } -impl<'de> Decodable for u32 { +impl Decodable for NonZeroU16 { + fn decode(mut decoder: D) -> Result { + NonZeroU16::new(decoder.decode_u16()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::U16, + }) + } +} + +impl Decodable for u32 { fn decode(mut decoder: D) -> Result { decoder.decode_u32() } } -impl<'de> Decodable for u64 { +impl Decodable for NonZeroU32 { + fn decode(mut decoder: D) -> Result { + NonZeroU32::new(decoder.decode_u32()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::U32, + }) + } +} + +impl Decodable for u64 { fn decode(mut decoder: D) -> Result { decoder.decode_u64() } } -impl<'de> Decodable for u128 { +impl Decodable for NonZeroU64 { + fn decode(mut decoder: D) -> Result { + NonZeroU64::new(decoder.decode_u64()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::U64, + }) + } +} + +impl Decodable for u128 { fn decode(mut decoder: D) -> Result { decoder.decode_u128() } } -impl<'de> Decodable for usize { +impl Decodable for NonZeroU128 { + fn decode(mut decoder: D) -> Result { + NonZeroU128::new(decoder.decode_u128()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::U128, + }) + } +} + +impl Decodable for usize { fn decode(mut decoder: D) -> Result { decoder.decode_usize() } } -impl<'de> Decodable for i8 { +impl Decodable for NonZeroUsize { + fn decode(mut decoder: D) -> Result { + NonZeroUsize::new(decoder.decode_usize()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::Usize, + }) + } +} + +impl Decodable for i8 { fn decode(mut decoder: D) -> Result { decoder.decode_i8() } } -impl<'de> Decodable for i16 { +impl Decodable for NonZeroI8 { + fn decode(mut decoder: D) -> Result { + NonZeroI8::new(decoder.decode_i8()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::I8, + }) + } +} + +impl Decodable for i16 { fn decode(mut decoder: D) -> Result { decoder.decode_i16() } } -impl<'de> Decodable for i32 { +impl Decodable for NonZeroI16 { + fn decode(mut decoder: D) -> Result { + NonZeroI16::new(decoder.decode_i16()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::I16, + }) + } +} + +impl Decodable for i32 { fn decode(mut decoder: D) -> Result { decoder.decode_i32() } } -impl<'de> Decodable for i64 { +impl Decodable for NonZeroI32 { + fn decode(mut decoder: D) -> Result { + NonZeroI32::new(decoder.decode_i32()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::I32, + }) + } +} + +impl Decodable for i64 { fn decode(mut decoder: D) -> Result { decoder.decode_i64() } } -impl<'de> Decodable for i128 { +impl Decodable for NonZeroI64 { + fn decode(mut decoder: D) -> Result { + NonZeroI64::new(decoder.decode_i64()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::I64, + }) + } +} + +impl Decodable for i128 { fn decode(mut decoder: D) -> Result { decoder.decode_i128() } } -impl<'de> Decodable for isize { +impl Decodable for NonZeroI128 { + fn decode(mut decoder: D) -> Result { + NonZeroI128::new(decoder.decode_i128()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::I128, + }) + } +} + +impl Decodable for isize { fn decode(mut decoder: D) -> Result { decoder.decode_isize() } } -impl<'de> Decodable for f32 { +impl Decodable for NonZeroIsize { + fn decode(mut decoder: D) -> Result { + NonZeroIsize::new(decoder.decode_isize()?).ok_or(DecodeError::NonZeroTypeIsZero { + non_zero_type: IntegerType::Isize, + }) + } +} + +impl Decodable for f32 { fn decode(mut decoder: D) -> Result { decoder.decode_f32() } } -impl<'de> Decodable for f64 { +impl Decodable for f64 { fn decode(mut decoder: D) -> Result { decoder.decode_f64() } } -impl<'de> Decodable for char { +impl Decodable for char { fn decode(mut decoder: D) -> Result { decoder.decode_char() } @@ -121,19 +220,19 @@ impl<'a, 'de: 'a> BorrowDecodable<'de> for &'a str { } } -impl<'de, const N: usize> Decodable for [u8; N] { +impl Decodable for [u8; N] { fn decode(mut decoder: D) -> Result { decoder.decode_array() } } -impl<'de, T> Decodable for core::marker::PhantomData { +impl Decodable for core::marker::PhantomData { fn decode(_: D) -> Result { Ok(core::marker::PhantomData) } } -impl<'de, T> Decodable for Option +impl Decodable for Option where T: Decodable, { @@ -155,7 +254,7 @@ where } } -impl<'de, T, U> Decodable for Result +impl Decodable for Result where T: Decodable, U: Decodable, diff --git a/src/enc/impls.rs b/src/enc/impls.rs index a452eb9..3307474 100644 --- a/src/enc/impls.rs +++ b/src/enc/impls.rs @@ -2,6 +2,10 @@ use super::{Encode, Encodeable}; use crate::error::EncodeError; use core::{ cell::{Cell, RefCell}, + num::{ + NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128, + NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, + }, ops::{Bound, Range, RangeInclusive}, time::Duration, }; @@ -18,72 +22,144 @@ impl Encodeable for u8 { } } +impl Encodeable for NonZeroU8 { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for u16 { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_u16(*self) } } +impl Encodeable for NonZeroU16 { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for u32 { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_u32(*self) } } +impl Encodeable for NonZeroU32 { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for u64 { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_u64(*self) } } +impl Encodeable for NonZeroU64 { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for u128 { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_u128(*self) } } +impl Encodeable for NonZeroU128 { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for usize { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_usize(*self) } } +impl Encodeable for NonZeroUsize { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for i8 { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_i8(*self) } } +impl Encodeable for NonZeroI8 { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for i16 { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_i16(*self) } } +impl Encodeable for NonZeroI16 { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for i32 { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_i32(*self) } } +impl Encodeable for NonZeroI32 { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for i64 { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_i64(*self) } } +impl Encodeable for NonZeroI64 { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for i128 { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_i128(*self) } } +impl Encodeable for NonZeroI128 { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for isize { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_isize(*self) } } +impl Encodeable for NonZeroIsize { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + self.get().encode(encoder) + } +} + impl Encodeable for f32 { fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { encoder.encode_f32(*self) diff --git a/src/error.rs b/src/error.rs index 77528df..2b037db 100644 --- a/src/error.rs +++ b/src/error.rs @@ -59,6 +59,12 @@ pub enum DecodeError { found: IntegerType, }, + /// The decoder tried to decode any of the `NonZero*` types but the value is zero + NonZeroTypeIsZero { + /// The type that was being read from the reader + non_zero_type: IntegerType, + }, + /// Invalid enum variant was found. The decoder tried to decode variant index `found`, but the variant index should be between `min` and `max`. UnexpectedVariant { /// The type name that was being decoded. @@ -104,9 +110,17 @@ pub enum DecodeError { #[derive(Debug)] #[allow(missing_docs)] pub enum IntegerType { + U8, U16, U32, U64, U128, - USize, + Usize, + + I8, + I16, + I32, + I64, + I128, + Isize, } diff --git a/src/varint/decode_unsigned.rs b/src/varint/decode_unsigned.rs index 53ac0e7..9954832 100644 --- a/src/varint/decode_unsigned.rs +++ b/src/varint/decode_unsigned.rs @@ -142,7 +142,7 @@ pub fn varint_decode_usize<'a, R: Reader<'a>>( }) } U128_BYTE => Err(DecodeError::InvalidIntegerType { - expected: IntegerType::USize, + expected: IntegerType::Usize, found: IntegerType::U128, }), x => Ok(x as usize), diff --git a/tests/basic_types.rs b/tests/basic_types.rs index a3c8d21..7bb100b 100644 --- a/tests/basic_types.rs +++ b/tests/basic_types.rs @@ -3,6 +3,7 @@ mod utils; use core::cell::{Cell, RefCell}; use core::ops::Bound; use core::time::Duration; +use std::num::*; use utils::the_same; #[test] @@ -82,6 +83,33 @@ fn test_numbers() { the_same(Bound::::Unbounded); the_same(Bound::::Included(105)); the_same(Bound::::Excluded(5)); + + // NonZero* types + the_same(NonZeroU8::new(0)); + the_same(NonZeroU8::new(123)); + the_same(NonZeroU16::new(0)); + the_same(NonZeroU16::new(12345)); + the_same(NonZeroU32::new(0)); + the_same(NonZeroU32::new(12345)); + the_same(NonZeroU64::new(0)); + the_same(NonZeroU64::new(12345)); + the_same(NonZeroU128::new(0)); + the_same(NonZeroU128::new(12345)); + the_same(NonZeroUsize::new(0)); + the_same(NonZeroUsize::new(12345)); + + the_same(NonZeroI8::new(0)); + the_same(NonZeroI8::new(123)); + the_same(NonZeroI16::new(0)); + the_same(NonZeroI16::new(12345)); + the_same(NonZeroI32::new(0)); + the_same(NonZeroI32::new(12345)); + the_same(NonZeroI64::new(0)); + the_same(NonZeroI64::new(12345)); + the_same(NonZeroI128::new(0)); + the_same(NonZeroI128::new(12345)); + the_same(NonZeroIsize::new(0)); + the_same(NonZeroIsize::new(12345)); } #[test]