Seal SizeLimit and BincodeRead
This commit is contained in:
parent
b6dbb08f92
commit
18bcfc420e
|
|
@ -4,7 +4,7 @@ use serde_crate as serde;
|
||||||
|
|
||||||
/// A byte-oriented reading trait that is specialized for
|
/// A byte-oriented reading trait that is specialized for
|
||||||
/// slices and generic readers.
|
/// slices and generic readers.
|
||||||
pub trait BincodeRead<'storage>: IoRead {
|
pub trait BincodeRead<'storage>: IoRead + ::private::Sealed {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn forward_read_str<V>(&mut self, length: usize, visitor: V) -> Result<V::Value>
|
fn forward_read_str<V>(&mut self, length: usize, visitor: V) -> Result<V::Value>
|
||||||
where V: serde::de::Visitor<'storage>;
|
where V: serde::de::Visitor<'storage>;
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
use std::io::{Write, Read};
|
use std::io::{Write, Read};
|
||||||
use std::io::Error as IoError;
|
use std::io::Error as IoError;
|
||||||
use std::{error, fmt, result};
|
use std::{error, fmt, result};
|
||||||
use ::SizeLimit;
|
use ::{CountSize, SizeLimit};
|
||||||
use byteorder::{ByteOrder};
|
use byteorder::{ByteOrder};
|
||||||
|
|
||||||
pub use super::de::{
|
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)
|
Ok(writer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct CountSize {
|
|
||||||
total: u64,
|
|
||||||
limit: Option<u64>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SizeLimit for CountSize {
|
impl SizeLimit for CountSize {
|
||||||
fn add(&mut self, c: u64) -> Result<()> {
|
fn add(&mut self, c: u64) -> Result<()> {
|
||||||
self.total += c;
|
self.total += c;
|
||||||
|
|
|
||||||
17
src/lib.rs
17
src/lib.rs
|
|
@ -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
|
/// encoding function, the encoder will verify that the structure can be encoded
|
||||||
/// within that limit. This verification occurs before any bytes are written to
|
/// within that limit. This verification occurs before any bytes are written to
|
||||||
/// the Writer, so recovering from an error is easy.
|
/// 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
|
/// Tells the SizeLimit that a certain number of bytes has been
|
||||||
/// read or written. Returns Err if the limit has been exceeded.
|
/// read or written. Returns Err if the limit has been exceeded.
|
||||||
fn add(&mut self, n: u64) -> Result<()>;
|
fn add(&mut self, n: u64) -> Result<()>;
|
||||||
|
|
@ -143,6 +143,11 @@ pub struct Bounded(pub u64);
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct Infinite;
|
pub struct Infinite;
|
||||||
|
|
||||||
|
struct CountSize {
|
||||||
|
total: u64,
|
||||||
|
limit: Option<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
impl SizeLimit for Bounded {
|
impl SizeLimit for Bounded {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn add(&mut self, n: u64) -> Result<()> {
|
fn add(&mut self, n: u64) -> Result<()> {
|
||||||
|
|
@ -165,3 +170,13 @@ impl SizeLimit for Infinite {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn limit(&self) -> Option<u64> { None }
|
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 {}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue