mirror of https://git.sr.ht/~stygianentity/bincode
Added support for NonZero* types
This commit is contained in:
parent
9420d03762
commit
a0469e08ef
145
src/de/impls.rs
145
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<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
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<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_u8()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for u16 {
|
||||
impl Decodable for NonZeroU8 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroU8::new(decoder.decode_u8()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::U8,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for u16 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_u16()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for u32 {
|
||||
impl Decodable for NonZeroU16 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroU16::new(decoder.decode_u16()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::U16,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for u32 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_u32()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for u64 {
|
||||
impl Decodable for NonZeroU32 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroU32::new(decoder.decode_u32()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::U32,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for u64 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_u64()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for u128 {
|
||||
impl Decodable for NonZeroU64 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroU64::new(decoder.decode_u64()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::U64,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for u128 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_u128()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for usize {
|
||||
impl Decodable for NonZeroU128 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroU128::new(decoder.decode_u128()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::U128,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for usize {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_usize()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for i8 {
|
||||
impl Decodable for NonZeroUsize {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroUsize::new(decoder.decode_usize()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::Usize,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for i8 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_i8()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for i16 {
|
||||
impl Decodable for NonZeroI8 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroI8::new(decoder.decode_i8()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::I8,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for i16 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_i16()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for i32 {
|
||||
impl Decodable for NonZeroI16 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroI16::new(decoder.decode_i16()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::I16,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for i32 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_i32()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for i64 {
|
||||
impl Decodable for NonZeroI32 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroI32::new(decoder.decode_i32()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::I32,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for i64 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_i64()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for i128 {
|
||||
impl Decodable for NonZeroI64 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroI64::new(decoder.decode_i64()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::I64,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for i128 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_i128()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for isize {
|
||||
impl Decodable for NonZeroI128 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroI128::new(decoder.decode_i128()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::I128,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for isize {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_isize()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for f32 {
|
||||
impl Decodable for NonZeroIsize {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
NonZeroIsize::new(decoder.decode_isize()?).ok_or(DecodeError::NonZeroTypeIsZero {
|
||||
non_zero_type: IntegerType::Isize,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for f32 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_f32()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for f64 {
|
||||
impl Decodable for f64 {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_f64()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Decodable for char {
|
||||
impl Decodable for char {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
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<const N: usize> Decodable for [u8; N] {
|
||||
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
|
||||
decoder.decode_array()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T> Decodable for core::marker::PhantomData<T> {
|
||||
impl<T> Decodable for core::marker::PhantomData<T> {
|
||||
fn decode<D: Decode>(_: D) -> Result<Self, DecodeError> {
|
||||
Ok(core::marker::PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T> Decodable for Option<T>
|
||||
impl<T> Decodable for Option<T>
|
||||
where
|
||||
T: Decodable,
|
||||
{
|
||||
|
|
@ -155,7 +254,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'de, T, U> Decodable for Result<T, U>
|
||||
impl<T, U> Decodable for Result<T, U>
|
||||
where
|
||||
T: Decodable,
|
||||
U: Decodable,
|
||||
|
|
|
|||
|
|
@ -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<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for u16 {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_u16(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for NonZeroU16 {
|
||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for u32 {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_u32(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for NonZeroU32 {
|
||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for u64 {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_u64(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for NonZeroU64 {
|
||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for u128 {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_u128(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for NonZeroU128 {
|
||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for usize {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_usize(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for NonZeroUsize {
|
||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for i8 {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_i8(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for NonZeroI8 {
|
||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for i16 {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_i16(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for NonZeroI16 {
|
||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for i32 {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_i32(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for NonZeroI32 {
|
||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for i64 {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_i64(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for NonZeroI64 {
|
||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for i128 {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_i128(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for NonZeroI128 {
|
||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for isize {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_isize(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for NonZeroIsize {
|
||||
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||
self.get().encode(encoder)
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodeable for f32 {
|
||||
fn encode<E: Encode>(&self, mut encoder: E) -> Result<(), EncodeError> {
|
||||
encoder.encode_f32(*self)
|
||||
|
|
|
|||
16
src/error.rs
16
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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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::<usize>::Unbounded);
|
||||
the_same(Bound::<usize>::Included(105));
|
||||
the_same(Bound::<usize>::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]
|
||||
|
|
|
|||
Loading…
Reference in New Issue