mirror of https://git.sr.ht/~stygianentity/bincode
Combine error types (#104)
* Combine error types * Correct errors from merge conflict * Create ErrorKind and Box it up * Fix merge errors. * Fix merge errors re-adding length encoding.
This commit is contained in:
parent
19538cff29
commit
ffbe4387dd
124
src/serde/mod.rs
124
src/serde/mod.rs
|
|
@ -3,19 +3,16 @@
|
|||
//! implementation.
|
||||
|
||||
use std::io::{Write, Read};
|
||||
use std::io::Error as IoError;
|
||||
use std::{error, fmt, result};
|
||||
use ::SizeLimit;
|
||||
|
||||
pub use self::reader::{
|
||||
Deserializer,
|
||||
DeserializeResult,
|
||||
DeserializeError,
|
||||
InvalidEncoding
|
||||
};
|
||||
|
||||
pub use self::writer::{
|
||||
Serializer,
|
||||
SerializeResult,
|
||||
SerializeError,
|
||||
};
|
||||
|
||||
use self::writer::SizeChecker;
|
||||
|
|
@ -25,15 +22,115 @@ use serde_crate as serde;
|
|||
mod reader;
|
||||
mod writer;
|
||||
|
||||
pub type Result<T> = result::Result<T, Error>;
|
||||
|
||||
/// An error that can be produced during (de)serializing.
|
||||
///
|
||||
/// If decoding from a Buffer, assume that the buffer has been left
|
||||
/// in an invalid state.
|
||||
pub type Error = Box<ErrorKind>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ErrorKind {
|
||||
/// If the error stems from the reader/writer that is being used
|
||||
/// during (de)serialization, that error will be stored and returned here.
|
||||
IoError(IoError),
|
||||
/// If the bytes in the reader are not decodable because of an invalid
|
||||
/// encoding, this error will be returned. This error is only possible
|
||||
/// if a stream is corrupted. A stream produced from `encode` or `encode_into`
|
||||
/// should **never** produce an InvalidEncoding error.
|
||||
InvalidEncoding(InvalidEncoding),
|
||||
/// If (de)serializing a message takes more than the provided size limit, this
|
||||
/// error is returned.
|
||||
SizeLimit,
|
||||
SequenceMustHaveLength,
|
||||
Custom(String)
|
||||
}
|
||||
|
||||
impl error::Error for ErrorKind {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
ErrorKind::IoError(ref err) => error::Error::description(err),
|
||||
ErrorKind::InvalidEncoding(ref ib) => ib.desc,
|
||||
ErrorKind::SequenceMustHaveLength => "bincode can't encode infinite sequences",
|
||||
ErrorKind::SizeLimit => "the size limit for decoding has been reached",
|
||||
ErrorKind::Custom(ref msg) => msg,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&error::Error> {
|
||||
match *self {
|
||||
ErrorKind::IoError(ref err) => err.cause(),
|
||||
ErrorKind::InvalidEncoding(_) => None,
|
||||
ErrorKind::SequenceMustHaveLength => None,
|
||||
ErrorKind::SizeLimit => None,
|
||||
ErrorKind::Custom(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IoError> for Error {
|
||||
fn from(err: IoError) -> Error {
|
||||
ErrorKind::IoError(err).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ErrorKind {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
ErrorKind::IoError(ref ioerr) =>
|
||||
write!(fmt, "IoError: {}", ioerr),
|
||||
ErrorKind::InvalidEncoding(ref ib) =>
|
||||
write!(fmt, "InvalidEncoding: {}", ib),
|
||||
ErrorKind::SequenceMustHaveLength =>
|
||||
write!(fmt, "Bincode can only encode sequences and maps that have a knowable size ahead of time."),
|
||||
ErrorKind::SizeLimit =>
|
||||
write!(fmt, "SizeLimit"),
|
||||
ErrorKind::Custom(ref s) =>
|
||||
s.fmt(fmt),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::de::Error for Error {
|
||||
fn custom<T: fmt::Display>(desc: T) -> Error {
|
||||
ErrorKind::Custom(desc.to_string()).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::ser::Error for Error {
|
||||
fn custom<T: fmt::Display>(msg: T) -> Self {
|
||||
ErrorKind::Custom(msg.to_string()).into()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Debug)]
|
||||
pub struct InvalidEncoding {
|
||||
pub desc: &'static str,
|
||||
pub detail: Option<String>,
|
||||
}
|
||||
|
||||
impl fmt::Display for InvalidEncoding {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
InvalidEncoding { detail: None, desc } =>
|
||||
write!(fmt, "{}", desc),
|
||||
InvalidEncoding { detail: Some(ref detail), desc } =>
|
||||
write!(fmt, "{} ({})", desc, detail)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Serializes an object directly into a `Writer`.
|
||||
///
|
||||
/// If the serialization would take more bytes than allowed by `size_limit`, an error
|
||||
/// is returned and *no bytes* will be written into the `Writer`.
|
||||
///
|
||||
/// If this returns an `SerializeError` (other than SizeLimit), assume that the
|
||||
/// If this returns an `Error` (other than SizeLimit), assume that the
|
||||
/// writer is in an invalid state, as writing could bail out in the middle of
|
||||
/// serializing.
|
||||
pub fn serialize_into<W: ?Sized, T: ?Sized>(writer: &mut W, value: &T, size_limit: SizeLimit) -> SerializeResult<()>
|
||||
pub fn serialize_into<W: ?Sized, T: ?Sized>(writer: &mut W, value: &T, size_limit: SizeLimit) -> Result<()>
|
||||
where W: Write,
|
||||
T: serde::Serialize,
|
||||
{
|
||||
|
|
@ -53,7 +150,7 @@ pub fn serialize_into<W: ?Sized, T: ?Sized>(writer: &mut W, value: &T, size_limi
|
|||
///
|
||||
/// If the serialization would take more bytes than allowed by `size_limit`,
|
||||
/// an error is returned.
|
||||
pub fn serialize<T: ?Sized>(value: &T, size_limit: SizeLimit) -> SerializeResult<Vec<u8>>
|
||||
pub fn serialize<T: ?Sized>(value: &T, size_limit: SizeLimit) -> Result<Vec<u8>>
|
||||
where T: serde::Serialize
|
||||
{
|
||||
// Since we are putting values directly into a vector, we can do size
|
||||
|
|
@ -61,10 +158,7 @@ pub fn serialize<T: ?Sized>(value: &T, size_limit: SizeLimit) -> SerializeResult
|
|||
// the right size.
|
||||
let mut writer = match size_limit {
|
||||
SizeLimit::Bounded(size_limit) => {
|
||||
let actual_size = match serialized_size_bounded(value, size_limit) {
|
||||
Some(actual_size) => actual_size,
|
||||
None => { return Err(SerializeError::SizeLimit); }
|
||||
};
|
||||
let actual_size = try!(serialized_size_bounded(value, size_limit).ok_or(ErrorKind::SizeLimit));
|
||||
Vec::with_capacity(actual_size as usize)
|
||||
}
|
||||
SizeLimit::Infinite => Vec::new()
|
||||
|
|
@ -105,10 +199,10 @@ pub fn serialized_size_bounded<T: ?Sized>(value: &T, max: u64) -> Option<u64>
|
|||
/// A SizeLimit can help prevent an attacker from flooding your server with
|
||||
/// a neverending stream of values that runs your server out of memory.
|
||||
///
|
||||
/// If this returns an `DeserializeError`, assume that the buffer that you passed
|
||||
/// If this returns an `Error`, assume that the buffer that you passed
|
||||
/// in is in an invalid state, as the error could be returned during any point
|
||||
/// in the reading.
|
||||
pub fn deserialize_from<R: ?Sized, T>(reader: &mut R, size_limit: SizeLimit) -> DeserializeResult<T>
|
||||
pub fn deserialize_from<R: ?Sized, T>(reader: &mut R, size_limit: SizeLimit) -> Result<T>
|
||||
where R: Read,
|
||||
T: serde::Deserialize,
|
||||
{
|
||||
|
|
@ -120,7 +214,7 @@ pub fn deserialize_from<R: ?Sized, T>(reader: &mut R, size_limit: SizeLimit) ->
|
|||
///
|
||||
/// This method does not have a size-limit because if you already have the bytes
|
||||
/// in memory, then you don't gain anything by having a limiter.
|
||||
pub fn deserialize<T>(bytes: &[u8]) -> DeserializeResult<T>
|
||||
pub fn deserialize<T>(bytes: &[u8]) -> Result<T>
|
||||
where T: serde::Deserialize,
|
||||
{
|
||||
let mut reader = bytes;
|
||||
|
|
|
|||
|
|
@ -1,102 +1,11 @@
|
|||
use std::io::Read;
|
||||
use std::io::Error as IoError;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::convert::From;
|
||||
|
||||
use byteorder::{BigEndian, ReadBytesExt};
|
||||
use serde_crate as serde;
|
||||
use serde_crate::de::value::ValueDeserializer;
|
||||
use serde_crate::de::Error as DeError;
|
||||
use ::SizeLimit;
|
||||
|
||||
#[derive(Eq, PartialEq, Clone, Debug)]
|
||||
pub struct InvalidEncoding {
|
||||
pub desc: &'static str,
|
||||
pub detail: Option<String>,
|
||||
}
|
||||
|
||||
impl fmt::Display for InvalidEncoding {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
InvalidEncoding { detail: None, desc } =>
|
||||
write!(fmt, "{}", desc),
|
||||
InvalidEncoding { detail: Some(ref detail), desc } =>
|
||||
write!(fmt, "{} ({})", desc, detail)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An error that can be produced during decoding.
|
||||
///
|
||||
/// If decoding from a Buffer, assume that the buffer has been left
|
||||
/// in an invalid state.
|
||||
#[derive(Debug)]
|
||||
pub enum DeserializeError {
|
||||
/// If the error stems from the reader that is being used
|
||||
/// during decoding, that error will be stored and returned here.
|
||||
IoError(IoError),
|
||||
/// If the bytes in the reader are not decodable because of an invalid
|
||||
/// encoding, this error will be returned. This error is only possible
|
||||
/// if a stream is corrupted. A stream produced from `encode` or `encode_into`
|
||||
/// should **never** produce an InvalidEncoding error.
|
||||
InvalidEncoding(InvalidEncoding),
|
||||
/// If decoding a message takes more than the provided size limit, this
|
||||
/// error is returned.
|
||||
SizeLimit,
|
||||
Custom(String)
|
||||
}
|
||||
|
||||
impl Error for DeserializeError {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
DeserializeError::IoError(ref err) => Error::description(err),
|
||||
DeserializeError::InvalidEncoding(ref ib) => ib.desc,
|
||||
DeserializeError::SizeLimit => "the size limit for decoding has been reached",
|
||||
DeserializeError::Custom(ref msg) => msg,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
match *self {
|
||||
DeserializeError::IoError(ref err) => err.cause(),
|
||||
DeserializeError::InvalidEncoding(_) => None,
|
||||
DeserializeError::SizeLimit => None,
|
||||
DeserializeError::Custom(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IoError> for DeserializeError {
|
||||
fn from(err: IoError) -> DeserializeError {
|
||||
DeserializeError::IoError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DeserializeError {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
DeserializeError::IoError(ref ioerr) =>
|
||||
write!(fmt, "IoError: {}", ioerr),
|
||||
DeserializeError::InvalidEncoding(ref ib) =>
|
||||
write!(fmt, "InvalidEncoding: {}", ib),
|
||||
DeserializeError::SizeLimit =>
|
||||
write!(fmt, "SizeLimit"),
|
||||
DeserializeError::Custom(ref s) =>
|
||||
s.fmt(fmt),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl serde::de::Error for DeserializeError {
|
||||
fn custom<T: fmt::Display>(desc: T) -> DeserializeError {
|
||||
DeserializeError::Custom(desc.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub type DeserializeResult<T> = Result<T, DeserializeError>;
|
||||
|
||||
use super::{Result, Error, ErrorKind, InvalidEncoding};
|
||||
|
||||
/// A Deserializer that reads bytes from a buffer.
|
||||
///
|
||||
|
|
@ -128,21 +37,21 @@ impl<R: Read> Deserializer<R> {
|
|||
self.read
|
||||
}
|
||||
|
||||
fn read_bytes(&mut self, count: u64) -> Result<(), DeserializeError> {
|
||||
fn read_bytes(&mut self, count: u64) -> Result<()> {
|
||||
self.read += count;
|
||||
match self.size_limit {
|
||||
SizeLimit::Infinite => Ok(()),
|
||||
SizeLimit::Bounded(x) if self.read <= x => Ok(()),
|
||||
SizeLimit::Bounded(_) => Err(DeserializeError::SizeLimit)
|
||||
SizeLimit::Bounded(_) => Err(ErrorKind::SizeLimit.into())
|
||||
}
|
||||
}
|
||||
|
||||
fn read_type<T>(&mut self) -> Result<(), DeserializeError> {
|
||||
fn read_type<T>(&mut self) -> Result<()> {
|
||||
use std::mem::size_of;
|
||||
self.read_bytes(size_of::<T>() as u64)
|
||||
}
|
||||
|
||||
fn read_string(&mut self) -> DeserializeResult<String> {
|
||||
fn read_string(&mut self) -> Result<String> {
|
||||
let len = try!(serde::Deserialize::deserialize(&mut *self));
|
||||
try!(self.read_bytes(len));
|
||||
|
||||
|
|
@ -150,17 +59,17 @@ impl<R: Read> Deserializer<R> {
|
|||
try!(self.reader.by_ref().take(len as u64).read_to_end(&mut buffer));
|
||||
|
||||
String::from_utf8(buffer).map_err(|err|
|
||||
DeserializeError::InvalidEncoding(InvalidEncoding {
|
||||
ErrorKind::InvalidEncoding(InvalidEncoding {
|
||||
desc: "error while decoding utf8 string",
|
||||
detail: Some(format!("Deserialize error: {}", err))
|
||||
}))
|
||||
}).into())
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_nums {
|
||||
($ty:ty, $dser_method:ident, $visitor_method:ident, $reader_method:ident) => {
|
||||
#[inline]
|
||||
fn $dser_method<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn $dser_method<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
try!(self.read_type::<$ty>());
|
||||
|
|
@ -172,17 +81,17 @@ macro_rules! impl_nums {
|
|||
|
||||
|
||||
impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
||||
type Error = DeserializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn deserialize<V>(self, _visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize<V>(self, _visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
let message = "bincode does not support Deserializer::deserialize";
|
||||
Err(DeserializeError::custom(message))
|
||||
Err(Error::custom(message))
|
||||
}
|
||||
|
||||
fn deserialize_bool<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
let value: u8 = try!(serde::Deserialize::deserialize(self));
|
||||
|
|
@ -190,10 +99,10 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
1 => visitor.visit_bool(true),
|
||||
0 => visitor.visit_bool(false),
|
||||
value => {
|
||||
Err(DeserializeError::InvalidEncoding(InvalidEncoding {
|
||||
Err(ErrorKind::InvalidEncoding(InvalidEncoding {
|
||||
desc: "invalid u8 when decoding bool",
|
||||
detail: Some(format!("Expected 0 or 1, got {}", value))
|
||||
}))
|
||||
}).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -209,7 +118,7 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
|
||||
|
||||
#[inline]
|
||||
fn deserialize_u8<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
try!(self.read_type::<u8>());
|
||||
|
|
@ -217,28 +126,28 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn deserialize_i8<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
try!(self.read_type::<i8>());
|
||||
visitor.visit_i8(try!(self.reader.read_i8()))
|
||||
}
|
||||
|
||||
fn deserialize_unit<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
visitor.visit_unit()
|
||||
}
|
||||
|
||||
fn deserialize_char<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
use std::str;
|
||||
|
||||
let error = DeserializeError::InvalidEncoding(InvalidEncoding {
|
||||
let error = ErrorKind::InvalidEncoding(InvalidEncoding {
|
||||
desc: "Invalid char encoding",
|
||||
detail: None
|
||||
});
|
||||
}).into();
|
||||
|
||||
let mut buf = [0];
|
||||
|
||||
|
|
@ -268,25 +177,25 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
visitor.visit_char(res)
|
||||
}
|
||||
|
||||
fn deserialize_str<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
visitor.visit_str(&try!(self.read_string()))
|
||||
}
|
||||
|
||||
fn deserialize_string<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
visitor.visit_string(try!(self.read_string()))
|
||||
}
|
||||
|
||||
fn deserialize_bytes<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
self.deserialize_seq(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_byte_buf<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
self.deserialize_seq(visitor)
|
||||
|
|
@ -295,18 +204,18 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
fn deserialize_enum<V>(self,
|
||||
_enum: &'static str,
|
||||
_variants: &'static [&'static str],
|
||||
visitor: V) -> Result<V::Value, Self::Error>
|
||||
visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
impl<'a, R: Read + 'a> serde::de::EnumVisitor for &'a mut Deserializer<R> {
|
||||
type Error = DeserializeError;
|
||||
type Error = Error;
|
||||
type Variant = Self;
|
||||
|
||||
fn visit_variant_seed<V>(self, seed: V) -> DeserializeResult<(V::Value, Self::Variant)>
|
||||
fn visit_variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant)>
|
||||
where V: serde::de::DeserializeSeed,
|
||||
{
|
||||
let idx: u32 = try!(serde::de::Deserialize::deserialize(&mut *self));
|
||||
let val: Result<_, DeserializeError> = seed.deserialize(idx.into_deserializer());
|
||||
let val: Result<_> = seed.deserialize(idx.into_deserializer());
|
||||
Ok((try!(val), self))
|
||||
}
|
||||
}
|
||||
|
|
@ -316,15 +225,15 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
|
||||
fn deserialize_tuple<V>(self,
|
||||
_len: usize,
|
||||
visitor: V) -> DeserializeResult<V::Value>
|
||||
visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
struct TupleVisitor<'a, R: Read + 'a>(&'a mut Deserializer<R>);
|
||||
|
||||
impl<'a, 'b: 'a, R: Read + 'b> serde::de::SeqVisitor for TupleVisitor<'a, R> {
|
||||
type Error = DeserializeError;
|
||||
type Error = Error;
|
||||
|
||||
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
||||
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
|
||||
where T: serde::de::DeserializeSeed,
|
||||
{
|
||||
let value = try!(serde::de::DeserializeSeed::deserialize(seed, &mut *self.0));
|
||||
|
|
@ -337,7 +246,7 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
|
||||
fn deserialize_seq_fixed_size<V>(self,
|
||||
len: usize,
|
||||
visitor: V) -> DeserializeResult<V::Value>
|
||||
visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
struct SeqVisitor<'a, R: Read + 'a> {
|
||||
|
|
@ -346,9 +255,9 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
}
|
||||
|
||||
impl<'a, 'b: 'a, R: Read + 'b> serde::de::SeqVisitor for SeqVisitor<'a, R> {
|
||||
type Error = DeserializeError;
|
||||
type Error = Error;
|
||||
|
||||
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
|
||||
fn visit_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
|
||||
where T: serde::de::DeserializeSeed,
|
||||
{
|
||||
if self.len > 0 {
|
||||
|
|
@ -364,21 +273,21 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
visitor.visit_seq(SeqVisitor { deserializer: self, len: len })
|
||||
}
|
||||
|
||||
fn deserialize_option<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
let value: u8 = try!(serde::de::Deserialize::deserialize(&mut *self));
|
||||
match value {
|
||||
0 => visitor.visit_none(),
|
||||
1 => visitor.visit_some(&mut *self),
|
||||
_ => Err(DeserializeError::InvalidEncoding(InvalidEncoding {
|
||||
_ => Err(ErrorKind::InvalidEncoding(InvalidEncoding {
|
||||
desc: "invalid tag when decoding Option",
|
||||
detail: Some(format!("Expected 0 or 1, got {}", value))
|
||||
})),
|
||||
}).into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_seq<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
let len = try!(serde::Deserialize::deserialize(&mut *self));
|
||||
|
|
@ -386,7 +295,7 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
self.deserialize_seq_fixed_size(len, visitor)
|
||||
}
|
||||
|
||||
fn deserialize_map<V>(self, visitor: V) -> DeserializeResult<V::Value>
|
||||
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
struct MapVisitor<'a, R: Read + 'a> {
|
||||
|
|
@ -395,9 +304,9 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
}
|
||||
|
||||
impl<'a, 'b: 'a, R: Read + 'b> serde::de::MapVisitor for MapVisitor<'a, R> {
|
||||
type Error = DeserializeError;
|
||||
type Error = Error;
|
||||
|
||||
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
|
||||
fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
|
||||
where K: serde::de::DeserializeSeed,
|
||||
{
|
||||
if self.len > 0 {
|
||||
|
|
@ -409,7 +318,7 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
}
|
||||
}
|
||||
|
||||
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
|
||||
fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
|
||||
where V: serde::de::DeserializeSeed,
|
||||
{
|
||||
let value = try!(serde::de::DeserializeSeed::deserialize(seed, &mut *self.deserializer));
|
||||
|
|
@ -425,23 +334,23 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
fn deserialize_struct<V>(self,
|
||||
_name: &str,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V) -> DeserializeResult<V::Value>
|
||||
visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
self.deserialize_tuple(fields.len(), visitor)
|
||||
}
|
||||
|
||||
fn deserialize_struct_field<V>(self,
|
||||
_visitor: V) -> DeserializeResult<V::Value>
|
||||
_visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
let message = "bincode does not support Deserializer::deserialize_struct_field";
|
||||
Err(DeserializeError::custom(message))
|
||||
Err(Error::custom(message))
|
||||
}
|
||||
|
||||
fn deserialize_newtype_struct<V>(self,
|
||||
_name: &str,
|
||||
visitor: V) -> DeserializeResult<V::Value>
|
||||
visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
visitor.visit_newtype_struct(self)
|
||||
|
|
@ -449,7 +358,7 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
|
||||
fn deserialize_unit_struct<V>(self,
|
||||
_name: &'static str,
|
||||
visitor: V) -> DeserializeResult<V::Value>
|
||||
visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
visitor.visit_unit()
|
||||
|
|
@ -458,29 +367,29 @@ impl<'a, R: Read> serde::Deserializer for &'a mut Deserializer<R> {
|
|||
fn deserialize_tuple_struct<V>(self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
visitor: V) -> DeserializeResult<V::Value>
|
||||
visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
self.deserialize_tuple(len, visitor)
|
||||
}
|
||||
|
||||
fn deserialize_ignored_any<V>(self,
|
||||
_visitor: V) -> DeserializeResult<V::Value>
|
||||
_visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
let message = "bincode does not support Deserializer::deserialize_ignored_any";
|
||||
Err(DeserializeError::custom(message))
|
||||
Err(Error::custom(message))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, R: Read> serde::de::VariantVisitor for &'a mut Deserializer<R> {
|
||||
type Error = DeserializeError;
|
||||
type Error = Error;
|
||||
|
||||
fn visit_unit(self) -> Result<(), Self::Error> {
|
||||
fn visit_unit(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn visit_newtype_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
|
||||
fn visit_newtype_seed<T>(self, seed: T) -> Result<T::Value>
|
||||
where T: serde::de::DeserializeSeed,
|
||||
{
|
||||
serde::de::DeserializeSeed::deserialize(seed, self)
|
||||
|
|
@ -488,7 +397,7 @@ impl<'a, R: Read> serde::de::VariantVisitor for &'a mut Deserializer<R> {
|
|||
|
||||
fn visit_tuple<V>(self,
|
||||
len: usize,
|
||||
visitor: V) -> Result<V::Value, Self::Error>
|
||||
visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
serde::de::Deserializer::deserialize_tuple(self, len, visitor)
|
||||
|
|
@ -496,7 +405,7 @@ impl<'a, R: Read> serde::de::VariantVisitor for &'a mut Deserializer<R> {
|
|||
|
||||
fn visit_struct<V>(self,
|
||||
fields: &'static [&'static str],
|
||||
visitor: V) -> Result<V::Value, Self::Error>
|
||||
visitor: V) -> Result<V::Value>
|
||||
where V: serde::de::Visitor,
|
||||
{
|
||||
serde::de::Deserializer::deserialize_tuple(self, fields.len(), visitor)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,3 @@
|
|||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::io::Error as IoError;
|
||||
use std::io::Write;
|
||||
use std::u32;
|
||||
|
||||
|
|
@ -8,23 +5,7 @@ use serde_crate as serde;
|
|||
|
||||
use byteorder::{BigEndian, WriteBytesExt};
|
||||
|
||||
pub type SerializeResult<T> = Result<T, SerializeError>;
|
||||
|
||||
|
||||
/// An error that can be produced during encoding.
|
||||
#[derive(Debug)]
|
||||
pub enum SerializeError {
|
||||
/// An error originating from the underlying `Writer`.
|
||||
IoError(IoError),
|
||||
/// An object could not be encoded with the given size limit.
|
||||
///
|
||||
/// This error is returned before any bytes are written to the
|
||||
/// output `Writer`.
|
||||
SizeLimit,
|
||||
SequenceMustHaveLength,
|
||||
/// A custom error message
|
||||
Custom(String)
|
||||
}
|
||||
use super::{Result, Error, ErrorKind};
|
||||
|
||||
/// An Serializer that encodes values directly into a Writer.
|
||||
///
|
||||
|
|
@ -34,48 +15,6 @@ pub struct Serializer<W> {
|
|||
writer: W,
|
||||
}
|
||||
|
||||
fn wrap_io(err: IoError) -> SerializeError {
|
||||
SerializeError::IoError(err)
|
||||
}
|
||||
|
||||
impl serde::ser::Error for SerializeError {
|
||||
fn custom<T: fmt::Display>(msg: T) -> Self {
|
||||
SerializeError::Custom(msg.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for SerializeError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
match *self {
|
||||
SerializeError::IoError(ref err) => write!(f, "IoError: {}", err),
|
||||
SerializeError::Custom(ref s) => write!(f, "Custom Error {}", s),
|
||||
SerializeError::SequenceMustHaveLength =>
|
||||
write!(f, "Bincode can only encode sequences and maps that have a knowable size ahead of time."),
|
||||
SerializeError::SizeLimit => write!(f, "SizeLimit"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for SerializeError {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
SerializeError::IoError(ref err) => Error::description(err),
|
||||
SerializeError::Custom(_) => "a custom serialization error was reported",
|
||||
SerializeError::SequenceMustHaveLength => "bincode can't encode infinite sequences",
|
||||
SerializeError::SizeLimit => "the size limit for decoding has been reached",
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
match *self {
|
||||
SerializeError::IoError(ref err) => err.cause(),
|
||||
SerializeError::Custom(_) => None,
|
||||
SerializeError::SizeLimit => None,
|
||||
SerializeError::SequenceMustHaveLength => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Write> Serializer<W> {
|
||||
pub fn new(w: W) -> Serializer<W> {
|
||||
Serializer {
|
||||
|
|
@ -83,7 +22,7 @@ impl<W: Write> Serializer<W> {
|
|||
}
|
||||
}
|
||||
|
||||
fn add_enum_tag(&mut self, tag: usize) -> SerializeResult<()> {
|
||||
fn add_enum_tag(&mut self, tag: usize) -> Result<()> {
|
||||
if tag > u32::MAX as usize {
|
||||
panic!("Variant tag doesn't fit in a u32")
|
||||
}
|
||||
|
|
@ -94,7 +33,7 @@ impl<W: Write> Serializer<W> {
|
|||
|
||||
impl<'a, W: Write> serde::Serializer for &'a mut Serializer<W> {
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
type SerializeSeq = Compound<'a, W>;
|
||||
type SerializeTuple = Compound<'a, W>;
|
||||
type SerializeTupleStruct = Compound<'a, W>;
|
||||
|
|
@ -103,94 +42,94 @@ impl<'a, W: Write> serde::Serializer for &'a mut Serializer<W> {
|
|||
type SerializeStruct = Compound<'a, W>;
|
||||
type SerializeStructVariant = Compound<'a, W>;
|
||||
|
||||
fn serialize_unit(self) -> SerializeResult<()> { Ok(()) }
|
||||
fn serialize_unit(self) -> Result<()> { Ok(()) }
|
||||
|
||||
fn serialize_unit_struct(self, _: &'static str) -> SerializeResult<()> { Ok(()) }
|
||||
fn serialize_unit_struct(self, _: &'static str) -> Result<()> { Ok(()) }
|
||||
|
||||
fn serialize_bool(self, v: bool) -> SerializeResult<()> {
|
||||
self.writer.write_u8(if v {1} else {0}).map_err(wrap_io)
|
||||
fn serialize_bool(self, v: bool) -> Result<()> {
|
||||
self.writer.write_u8(if v {1} else {0}).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_u8(self, v: u8) -> SerializeResult<()> {
|
||||
self.writer.write_u8(v).map_err(wrap_io)
|
||||
fn serialize_u8(self, v: u8) -> Result<()> {
|
||||
self.writer.write_u8(v).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_u16(self, v: u16) -> SerializeResult<()> {
|
||||
self.writer.write_u16::<BigEndian>(v).map_err(wrap_io)
|
||||
fn serialize_u16(self, v: u16) -> Result<()> {
|
||||
self.writer.write_u16::<BigEndian>(v).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_u32(self, v: u32) -> SerializeResult<()> {
|
||||
self.writer.write_u32::<BigEndian>(v).map_err(wrap_io)
|
||||
fn serialize_u32(self, v: u32) -> Result<()> {
|
||||
self.writer.write_u32::<BigEndian>(v).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_u64(self, v: u64) -> SerializeResult<()> {
|
||||
self.writer.write_u64::<BigEndian>(v).map_err(wrap_io)
|
||||
fn serialize_u64(self, v: u64) -> Result<()> {
|
||||
self.writer.write_u64::<BigEndian>(v).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_i8(self, v: i8) -> SerializeResult<()> {
|
||||
self.writer.write_i8(v).map_err(wrap_io)
|
||||
fn serialize_i8(self, v: i8) -> Result<()> {
|
||||
self.writer.write_i8(v).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_i16(self, v: i16) -> SerializeResult<()> {
|
||||
self.writer.write_i16::<BigEndian>(v).map_err(wrap_io)
|
||||
fn serialize_i16(self, v: i16) -> Result<()> {
|
||||
self.writer.write_i16::<BigEndian>(v).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_i32(self, v: i32) -> SerializeResult<()> {
|
||||
self.writer.write_i32::<BigEndian>(v).map_err(wrap_io)
|
||||
fn serialize_i32(self, v: i32) -> Result<()> {
|
||||
self.writer.write_i32::<BigEndian>(v).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_i64(self, v: i64) -> SerializeResult<()> {
|
||||
self.writer.write_i64::<BigEndian>(v).map_err(wrap_io)
|
||||
fn serialize_i64(self, v: i64) -> Result<()> {
|
||||
self.writer.write_i64::<BigEndian>(v).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_f32(self, v: f32) -> SerializeResult<()> {
|
||||
self.writer.write_f32::<BigEndian>(v).map_err(wrap_io)
|
||||
fn serialize_f32(self, v: f32) -> Result<()> {
|
||||
self.writer.write_f32::<BigEndian>(v).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_f64(self, v: f64) -> SerializeResult<()> {
|
||||
self.writer.write_f64::<BigEndian>(v).map_err(wrap_io)
|
||||
fn serialize_f64(self, v: f64) -> Result<()> {
|
||||
self.writer.write_f64::<BigEndian>(v).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_str(self, v: &str) -> SerializeResult<()> {
|
||||
fn serialize_str(self, v: &str) -> Result<()> {
|
||||
try!(self.serialize_u64(v.len() as u64));
|
||||
self.writer.write_all(v.as_bytes()).map_err(SerializeError::IoError)
|
||||
self.writer.write_all(v.as_bytes()).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_char(self, c: char) -> SerializeResult<()> {
|
||||
self.writer.write_all(encode_utf8(c).as_slice()).map_err(SerializeError::IoError)
|
||||
fn serialize_char(self, c: char) -> Result<()> {
|
||||
self.writer.write_all(encode_utf8(c).as_slice()).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_bytes(self, v: &[u8]) -> SerializeResult<()> {
|
||||
fn serialize_bytes(self, v: &[u8]) -> Result<()> {
|
||||
try!(self.serialize_u64(v.len() as u64));
|
||||
self.writer.write_all(v).map_err(SerializeError::IoError)
|
||||
self.writer.write_all(v).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_none(self) -> SerializeResult<()> {
|
||||
self.writer.write_u8(0).map_err(wrap_io)
|
||||
fn serialize_none(self) -> Result<()> {
|
||||
self.writer.write_u8(0).map_err(Into::into)
|
||||
}
|
||||
|
||||
fn serialize_some<T: ?Sized>(self, v: &T) -> SerializeResult<()>
|
||||
fn serialize_some<T: ?Sized>(self, v: &T) -> Result<()>
|
||||
where T: serde::Serialize,
|
||||
{
|
||||
try!(self.writer.write_u8(1).map_err(wrap_io));
|
||||
try!(self.writer.write_u8(1));
|
||||
v.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_seq(self, len: Option<usize>) -> SerializeResult<Self::SerializeSeq> {
|
||||
let len = try!(len.ok_or(SerializeError::SequenceMustHaveLength));
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
|
||||
let len = try!(len.ok_or(ErrorKind::SequenceMustHaveLength));
|
||||
try!(self.serialize_u64(len as u64));
|
||||
Ok(Compound {ser: self})
|
||||
}
|
||||
|
||||
fn serialize_seq_fixed_size(self, _len: usize) -> SerializeResult<Self::SerializeSeq> {
|
||||
fn serialize_seq_fixed_size(self, _len: usize) -> Result<Self::SerializeSeq> {
|
||||
Ok(Compound {ser: self})
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, _len: usize) -> SerializeResult<Self::SerializeTuple> {
|
||||
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
|
||||
Ok(Compound {ser: self})
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(self, _name: &'static str, _len: usize) -> SerializeResult<Self::SerializeTupleStruct> {
|
||||
fn serialize_tuple_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeTupleStruct> {
|
||||
Ok(Compound {ser: self})
|
||||
}
|
||||
|
||||
|
|
@ -198,19 +137,19 @@ impl<'a, W: Write> serde::Serializer for &'a mut Serializer<W> {
|
|||
_name: &'static str,
|
||||
variant_index: usize,
|
||||
_variant: &'static str,
|
||||
_len: usize) -> SerializeResult<Self::SerializeTupleVariant>
|
||||
_len: usize) -> Result<Self::SerializeTupleVariant>
|
||||
{
|
||||
try!(self.add_enum_tag(variant_index));
|
||||
Ok(Compound {ser: self})
|
||||
}
|
||||
|
||||
fn serialize_map(self, len: Option<usize>) -> SerializeResult<Self::SerializeMap> {
|
||||
let len = try!(len.ok_or(SerializeError::SequenceMustHaveLength));
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
|
||||
let len = try!(len.ok_or(ErrorKind::SequenceMustHaveLength));
|
||||
try!(self.serialize_u64(len as u64));
|
||||
Ok(Compound {ser: self})
|
||||
}
|
||||
|
||||
fn serialize_struct(self, _name: &'static str, _len: usize) -> SerializeResult<Self::SerializeStruct> {
|
||||
fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
|
||||
Ok(Compound {ser: self})
|
||||
}
|
||||
|
||||
|
|
@ -218,7 +157,7 @@ impl<'a, W: Write> serde::Serializer for &'a mut Serializer<W> {
|
|||
_name: &'static str,
|
||||
variant_index: usize,
|
||||
_variant: &'static str,
|
||||
_len: usize) -> SerializeResult<Self::SerializeStructVariant>
|
||||
_len: usize) -> Result<Self::SerializeStructVariant>
|
||||
{
|
||||
try!(self.add_enum_tag(variant_index));
|
||||
Ok(Compound {ser: self})
|
||||
|
|
@ -226,7 +165,7 @@ impl<'a, W: Write> serde::Serializer for &'a mut Serializer<W> {
|
|||
|
||||
fn serialize_newtype_struct<T: ?Sized>(self,
|
||||
_name: &'static str,
|
||||
value: &T) -> SerializeResult<()>
|
||||
value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize,
|
||||
{
|
||||
value.serialize(self)
|
||||
|
|
@ -236,7 +175,7 @@ impl<'a, W: Write> serde::Serializer for &'a mut Serializer<W> {
|
|||
_name: &'static str,
|
||||
variant_index: usize,
|
||||
_variant: &'static str,
|
||||
value: &T) -> SerializeResult<()>
|
||||
value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize,
|
||||
{
|
||||
try!(self.add_enum_tag(variant_index));
|
||||
|
|
@ -246,7 +185,7 @@ impl<'a, W: Write> serde::Serializer for &'a mut Serializer<W> {
|
|||
fn serialize_unit_variant(self,
|
||||
_name: &'static str,
|
||||
variant_index: usize,
|
||||
_variant: &'static str) -> SerializeResult<()> {
|
||||
_variant: &'static str) -> Result<()> {
|
||||
self.add_enum_tag(variant_index)
|
||||
}
|
||||
}
|
||||
|
|
@ -264,21 +203,21 @@ impl SizeChecker {
|
|||
}
|
||||
}
|
||||
|
||||
fn add_raw(&mut self, size: usize) -> SerializeResult<()> {
|
||||
fn add_raw(&mut self, size: usize) -> Result<()> {
|
||||
self.written += size as u64;
|
||||
if self.written <= self.size_limit {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(SerializeError::SizeLimit)
|
||||
Err(ErrorKind::SizeLimit.into())
|
||||
}
|
||||
}
|
||||
|
||||
fn add_value<T>(&mut self, t: T) -> SerializeResult<()> {
|
||||
fn add_value<T>(&mut self, t: T) -> Result<()> {
|
||||
use std::mem::size_of_val;
|
||||
self.add_raw(size_of_val(&t))
|
||||
}
|
||||
|
||||
fn add_enum_tag(&mut self, tag: usize) -> SerializeResult<()> {
|
||||
fn add_enum_tag(&mut self, tag: usize) -> Result<()> {
|
||||
if tag > u32::MAX as usize {
|
||||
panic!("Variant tag doesn't fit in a u32")
|
||||
}
|
||||
|
|
@ -289,7 +228,7 @@ impl SizeChecker {
|
|||
|
||||
impl<'a> serde::Serializer for &'a mut SizeChecker {
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
type SerializeSeq = SizeCompound<'a>;
|
||||
type SerializeTuple = SizeCompound<'a>;
|
||||
type SerializeTupleStruct = SizeCompound<'a>;
|
||||
|
|
@ -298,95 +237,95 @@ impl<'a> serde::Serializer for &'a mut SizeChecker {
|
|||
type SerializeStruct = SizeCompound<'a>;
|
||||
type SerializeStructVariant = SizeCompound<'a>;
|
||||
|
||||
fn serialize_unit(self) -> SerializeResult<()> { Ok(()) }
|
||||
fn serialize_unit(self) -> Result<()> { Ok(()) }
|
||||
|
||||
fn serialize_unit_struct(self, _: &'static str) -> SerializeResult<()> { Ok(()) }
|
||||
fn serialize_unit_struct(self, _: &'static str) -> Result<()> { Ok(()) }
|
||||
|
||||
fn serialize_bool(self, _: bool) -> SerializeResult<()> {
|
||||
fn serialize_bool(self, _: bool) -> Result<()> {
|
||||
self.add_value(0 as u8)
|
||||
}
|
||||
|
||||
fn serialize_u8(self, v: u8) -> SerializeResult<()> {
|
||||
fn serialize_u8(self, v: u8) -> Result<()> {
|
||||
self.add_value(v)
|
||||
}
|
||||
|
||||
fn serialize_u16(self, v: u16) -> SerializeResult<()> {
|
||||
fn serialize_u16(self, v: u16) -> Result<()> {
|
||||
self.add_value(v)
|
||||
}
|
||||
|
||||
fn serialize_u32(self, v: u32) -> SerializeResult<()> {
|
||||
fn serialize_u32(self, v: u32) -> Result<()> {
|
||||
self.add_value(v)
|
||||
}
|
||||
|
||||
fn serialize_u64(self, v: u64) -> SerializeResult<()> {
|
||||
fn serialize_u64(self, v: u64) -> Result<()> {
|
||||
self.add_value(v)
|
||||
}
|
||||
|
||||
fn serialize_i8(self, v: i8) -> SerializeResult<()> {
|
||||
fn serialize_i8(self, v: i8) -> Result<()> {
|
||||
self.add_value(v)
|
||||
}
|
||||
|
||||
fn serialize_i16(self, v: i16) -> SerializeResult<()> {
|
||||
fn serialize_i16(self, v: i16) -> Result<()> {
|
||||
self.add_value(v)
|
||||
}
|
||||
|
||||
fn serialize_i32(self, v: i32) -> SerializeResult<()> {
|
||||
fn serialize_i32(self, v: i32) -> Result<()> {
|
||||
self.add_value(v)
|
||||
}
|
||||
|
||||
fn serialize_i64(self, v: i64) -> SerializeResult<()> {
|
||||
fn serialize_i64(self, v: i64) -> Result<()> {
|
||||
self.add_value(v)
|
||||
}
|
||||
|
||||
fn serialize_f32(self, v: f32) -> SerializeResult<()> {
|
||||
fn serialize_f32(self, v: f32) -> Result<()> {
|
||||
self.add_value(v)
|
||||
}
|
||||
|
||||
fn serialize_f64(self, v: f64) -> SerializeResult<()> {
|
||||
fn serialize_f64(self, v: f64) -> Result<()> {
|
||||
self.add_value(v)
|
||||
}
|
||||
|
||||
fn serialize_str(self, v: &str) -> SerializeResult<()> {
|
||||
fn serialize_str(self, v: &str) -> Result<()> {
|
||||
try!(self.add_value(0 as u64));
|
||||
self.add_raw(v.len())
|
||||
}
|
||||
|
||||
fn serialize_char(self, c: char) -> SerializeResult<()> {
|
||||
fn serialize_char(self, c: char) -> Result<()> {
|
||||
self.add_raw(encode_utf8(c).as_slice().len())
|
||||
}
|
||||
|
||||
fn serialize_bytes(self, v: &[u8]) -> SerializeResult<()> {
|
||||
fn serialize_bytes(self, v: &[u8]) -> Result<()> {
|
||||
try!(self.add_value(0 as u64));
|
||||
self.add_raw(v.len())
|
||||
}
|
||||
|
||||
fn serialize_none(self) -> SerializeResult<()> {
|
||||
fn serialize_none(self) -> Result<()> {
|
||||
self.add_value(0 as u8)
|
||||
}
|
||||
|
||||
fn serialize_some<T: ?Sized>(self, v: &T) -> SerializeResult<()>
|
||||
fn serialize_some<T: ?Sized>(self, v: &T) -> Result<()>
|
||||
where T: serde::Serialize,
|
||||
{
|
||||
try!(self.add_value(1 as u8));
|
||||
v.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_seq(self, len: Option<usize>) -> SerializeResult<Self::SerializeSeq> {
|
||||
let len = try!(len.ok_or(SerializeError::SequenceMustHaveLength));
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
|
||||
let len = try!(len.ok_or(ErrorKind::SequenceMustHaveLength));
|
||||
|
||||
try!(self.serialize_u64(len as u64));
|
||||
Ok(SizeCompound {ser: self})
|
||||
}
|
||||
|
||||
fn serialize_seq_fixed_size(self, _len: usize) -> SerializeResult<Self::SerializeSeq> {
|
||||
fn serialize_seq_fixed_size(self, _len: usize) -> Result<Self::SerializeSeq> {
|
||||
Ok(SizeCompound {ser: self})
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, _len: usize) -> SerializeResult<Self::SerializeTuple> {
|
||||
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
|
||||
Ok(SizeCompound {ser: self})
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(self, _name: &'static str, _len: usize) -> SerializeResult<Self::SerializeTupleStruct> {
|
||||
fn serialize_tuple_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeTupleStruct> {
|
||||
Ok(SizeCompound {ser: self})
|
||||
}
|
||||
|
||||
|
|
@ -394,21 +333,21 @@ impl<'a> serde::Serializer for &'a mut SizeChecker {
|
|||
_name: &'static str,
|
||||
variant_index: usize,
|
||||
_variant: &'static str,
|
||||
_len: usize) -> SerializeResult<Self::SerializeTupleVariant>
|
||||
_len: usize) -> Result<Self::SerializeTupleVariant>
|
||||
{
|
||||
try!(self.add_enum_tag(variant_index));
|
||||
Ok(SizeCompound {ser: self})
|
||||
}
|
||||
|
||||
fn serialize_map(self, len: Option<usize>) -> SerializeResult<Self::SerializeMap>
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap>
|
||||
{
|
||||
let len = try!(len.ok_or(SerializeError::SequenceMustHaveLength));
|
||||
let len = try!(len.ok_or(ErrorKind::SequenceMustHaveLength));
|
||||
|
||||
try!(self.serialize_u64(len as u64));
|
||||
Ok(SizeCompound {ser: self})
|
||||
}
|
||||
|
||||
fn serialize_struct(self, _name: &'static str, _len: usize) -> SerializeResult<Self::SerializeStruct> {
|
||||
fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
|
||||
Ok(SizeCompound {ser: self})
|
||||
}
|
||||
|
||||
|
|
@ -416,20 +355,20 @@ impl<'a> serde::Serializer for &'a mut SizeChecker {
|
|||
_name: &'static str,
|
||||
variant_index: usize,
|
||||
_variant: &'static str,
|
||||
_len: usize) -> SerializeResult<Self::SerializeStructVariant>
|
||||
_len: usize) -> Result<Self::SerializeStructVariant>
|
||||
{
|
||||
try!(self.add_enum_tag(variant_index));
|
||||
Ok(SizeCompound {ser: self})
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<V: serde::Serialize + ?Sized>(self, _name: &'static str, v: &V) -> SerializeResult<()> {
|
||||
fn serialize_newtype_struct<V: serde::Serialize + ?Sized>(self, _name: &'static str, v: &V) -> Result<()> {
|
||||
v.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(self,
|
||||
_name: &'static str,
|
||||
variant_index: usize,
|
||||
_variant: &'static str) -> SerializeResult<()> {
|
||||
_variant: &'static str) -> Result<()> {
|
||||
self.add_enum_tag(variant_index)
|
||||
}
|
||||
|
||||
|
|
@ -437,7 +376,7 @@ impl<'a> serde::Serializer for &'a mut SizeChecker {
|
|||
_name: &'static str,
|
||||
variant_index: usize,
|
||||
_variant: &'static str,
|
||||
value: &V) -> SerializeResult<()>
|
||||
value: &V) -> Result<()>
|
||||
{
|
||||
try!(self.add_enum_tag(variant_index));
|
||||
value.serialize(self)
|
||||
|
|
@ -453,17 +392,17 @@ impl<'a, W> serde::ser::SerializeSeq for Compound<'a, W>
|
|||
where W: Write
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> SerializeResult<()>
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -472,17 +411,17 @@ impl<'a, W> serde::ser::SerializeTuple for Compound<'a, W>
|
|||
where W: Write
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> SerializeResult<()>
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -491,17 +430,17 @@ impl<'a, W> serde::ser::SerializeTupleStruct for Compound<'a, W>
|
|||
where W: Write
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> SerializeResult<()>
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -510,17 +449,17 @@ impl<'a, W> serde::ser::SerializeTupleVariant for Compound<'a, W>
|
|||
where W: Write
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> SerializeResult<()>
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -529,24 +468,24 @@ impl<'a, W> serde::ser::SerializeMap for Compound<'a, W>
|
|||
where W: Write
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_key<K: ?Sized>(&mut self, value: &K) -> SerializeResult<()>
|
||||
fn serialize_key<K: ?Sized>(&mut self, value: &K) -> Result<()>
|
||||
where K: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_value<V: ?Sized>(&mut self, value: &V) -> SerializeResult<()>
|
||||
fn serialize_value<V: ?Sized>(&mut self, value: &V) -> Result<()>
|
||||
where V: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -555,17 +494,17 @@ impl<'a, W> serde::ser::SerializeStruct for Compound<'a, W>
|
|||
where W: Write
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> SerializeResult<()>
|
||||
fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -574,17 +513,17 @@ impl<'a, W> serde::ser::SerializeStructVariant for Compound<'a, W>
|
|||
where W: Write
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> SerializeResult<()>
|
||||
fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -597,17 +536,17 @@ pub struct SizeCompound<'a> {
|
|||
impl<'a> serde::ser::SerializeSeq for SizeCompound<'a>
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> SerializeResult<()>
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -615,17 +554,17 @@ impl<'a> serde::ser::SerializeSeq for SizeCompound<'a>
|
|||
impl<'a> serde::ser::SerializeTuple for SizeCompound<'a>
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> SerializeResult<()>
|
||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -633,17 +572,17 @@ impl<'a> serde::ser::SerializeTuple for SizeCompound<'a>
|
|||
impl<'a> serde::ser::SerializeTupleStruct for SizeCompound<'a>
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> SerializeResult<()>
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -651,17 +590,17 @@ impl<'a> serde::ser::SerializeTupleStruct for SizeCompound<'a>
|
|||
impl<'a> serde::ser::SerializeTupleVariant for SizeCompound<'a>
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> SerializeResult<()>
|
||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -669,24 +608,24 @@ impl<'a> serde::ser::SerializeTupleVariant for SizeCompound<'a>
|
|||
impl<'a> serde::ser::SerializeMap for SizeCompound<'a>
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_key<K: ?Sized>(&mut self, value: &K) -> SerializeResult<()>
|
||||
fn serialize_key<K: ?Sized>(&mut self, value: &K) -> Result<()>
|
||||
where K: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn serialize_value<V: ?Sized>(&mut self, value: &V) -> SerializeResult<()>
|
||||
fn serialize_value<V: ?Sized>(&mut self, value: &V) -> Result<()>
|
||||
where V: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -694,17 +633,17 @@ impl<'a> serde::ser::SerializeMap for SizeCompound<'a>
|
|||
impl<'a> serde::ser::SerializeStruct for SizeCompound<'a>
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> SerializeResult<()>
|
||||
fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -712,17 +651,17 @@ impl<'a> serde::ser::SerializeStruct for SizeCompound<'a>
|
|||
impl<'a> serde::ser::SerializeStructVariant for SizeCompound<'a>
|
||||
{
|
||||
type Ok = ();
|
||||
type Error = SerializeError;
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> SerializeResult<()>
|
||||
fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> Result<()>
|
||||
where T: serde::ser::Serialize
|
||||
{
|
||||
value.serialize(&mut *self.ser)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn end(self) -> SerializeResult<()> {
|
||||
fn end(self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use std::ops::Deref;
|
|||
use bincode::refbox::{RefBox, StrBox, SliceBox};
|
||||
|
||||
use bincode::SizeLimit::{self, Infinite, Bounded};
|
||||
use bincode::{serialize, serialized_size, deserialize, deserialize_from, DeserializeError, DeserializeResult};
|
||||
use bincode::{serialize, serialized_size, deserialize, deserialize_from, ErrorKind, Result};
|
||||
|
||||
fn proxy_encode<V>(element: &V, size_limit: SizeLimit) -> Vec<u8>
|
||||
where V: serde::Serialize + serde::Deserialize + PartialEq + Debug + 'static
|
||||
|
|
@ -211,12 +211,12 @@ fn test_fixed_size_array() {
|
|||
|
||||
#[test]
|
||||
fn deserializing_errors() {
|
||||
fn isize_invalid_deserialize<T: Debug>(res: DeserializeResult<T>) {
|
||||
match res {
|
||||
Err(DeserializeError::InvalidEncoding(_)) => {},
|
||||
Err(DeserializeError::Custom(ref s)) if s.contains("invalid encoding") => {},
|
||||
Err(DeserializeError::Custom(ref s)) if s.contains("invalid value") => {},
|
||||
_ => panic!("Expecting InvalidEncoding, got {:?}", res),
|
||||
fn isize_invalid_deserialize<T: Debug>(res: Result<T>) {
|
||||
match res.map_err(|e| *e) {
|
||||
Err(ErrorKind::InvalidEncoding(_)) => {},
|
||||
Err(ErrorKind::Custom(ref s)) if s.contains("invalid encoding") => {},
|
||||
Err(ErrorKind::Custom(ref s)) if s.contains("invalid value") => {},
|
||||
other => panic!("Expecting InvalidEncoding, got {:?}", other),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -235,11 +235,11 @@ fn deserializing_errors() {
|
|||
#[test]
|
||||
fn too_big_deserialize() {
|
||||
let serialized = vec![0,0,0,3];
|
||||
let deserialized: Result<u32, _> = deserialize_from(&mut &serialized[..], Bounded(3));
|
||||
let deserialized: Result<u32> = deserialize_from(&mut &serialized[..], Bounded(3));
|
||||
assert!(deserialized.is_err());
|
||||
|
||||
let serialized = vec![0,0,0,3];
|
||||
let deserialized: Result<u32, _> = deserialize_from(&mut &serialized[..], Bounded(4));
|
||||
let deserialized: Result<u32> = deserialize_from(&mut &serialized[..], Bounded(4));
|
||||
assert!(deserialized.is_ok());
|
||||
}
|
||||
|
||||
|
|
@ -256,7 +256,7 @@ fn char_serialization() {
|
|||
#[test]
|
||||
fn too_big_char_deserialize() {
|
||||
let serialized = vec![0x41];
|
||||
let deserialized: Result<char, _> = deserialize_from(&mut &serialized[..], Bounded(1));
|
||||
let deserialized: Result<char> = deserialize_from(&mut &serialized[..], Bounded(1));
|
||||
assert!(deserialized.is_ok());
|
||||
assert_eq!(deserialized.unwrap(), 'A');
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue