Merge pull request #196 from dgriffen/seal-bincode-read

Seal SizeLimit and BincodeRead
This commit is contained in:
David Tolnay 2017-07-22 13:27:40 -07:00 committed by GitHub
commit 2657d36ec2
3 changed files with 18 additions and 9 deletions

View File

@ -4,7 +4,7 @@ use serde_crate as serde;
/// A byte-oriented reading trait that is specialized for
/// slices and generic readers.
pub trait BincodeRead<'storage>: IoRead {
pub trait BincodeRead<'storage>: IoRead + ::private::Sealed {
#[doc(hidden)]
fn forward_read_str<V>(&mut self, length: usize, visitor: V) -> Result<V::Value>
where V: serde::de::Visitor<'storage>;

View File

@ -5,7 +5,7 @@
use std::io::{Write, Read};
use std::io::Error as IoError;
use std::{error, fmt, result};
use ::SizeLimit;
use ::{CountSize, SizeLimit};
use byteorder::{ByteOrder};
pub use super::de::{
@ -155,12 +155,6 @@ pub fn serialize<T: ?Sized, S, E>(value: &T, size_limit: S) -> Result<Vec<u8>>
Ok(writer)
}
struct CountSize {
total: u64,
limit: Option<u64>,
}
impl SizeLimit for CountSize {
fn add(&mut self, c: u64) -> Result<()> {
self.total += c;

View File

@ -125,7 +125,7 @@ pub fn serialize<T: ?Sized, S>(value: &T, size_limit: S) -> internal::Result<Vec
/// encoding function, the encoder will verify that the structure can be encoded
/// within that limit. This verification occurs before any bytes are written to
/// the Writer, so recovering from an error is easy.
pub trait SizeLimit {
pub trait SizeLimit: private::Sealed {
/// Tells the SizeLimit that a certain number of bytes has been
/// read or written. Returns Err if the limit has been exceeded.
fn add(&mut self, n: u64) -> Result<()>;
@ -143,6 +143,11 @@ pub struct Bounded(pub u64);
#[derive(Copy, Clone)]
pub struct Infinite;
struct CountSize {
total: u64,
limit: Option<u64>,
}
impl SizeLimit for Bounded {
#[inline(always)]
fn add(&mut self, n: u64) -> Result<()> {
@ -165,3 +170,13 @@ impl SizeLimit for Infinite {
#[inline(always)]
fn limit(&self) -> Option<u64> { None }
}
mod private {
pub trait Sealed {}
impl<'a> Sealed for super::de::read::SliceReader<'a> {}
impl<R> Sealed for super::de::read::IoReadReader<R> {}
impl Sealed for super::Infinite {}
impl Sealed for super::Bounded {}
impl Sealed for super::CountSize {}
}