|
|
@ -12,6 +12,24 @@ type OwnedObjectStackSize = space::S4;
|
|
|
|
pub trait Object {
|
|
|
|
pub trait Object {
|
|
|
|
fn to_writer(&self, to: &mut dyn io::Write) -> io::Result<usize>;
|
|
|
|
fn to_writer(&self, to: &mut dyn io::Write) -> io::Result<usize>;
|
|
|
|
fn from_reader(&mut self, from: &mut dyn io::Read) -> io::Result<usize>; //ugh, this SUUUCKS...
|
|
|
|
fn from_reader(&mut self, from: &mut dyn io::Read) -> io::Result<usize>; //ugh, this SUUUCKS...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[inline] fn to_vec_with_cap(&self, cap: usize) -> io::Result<Vec<u8>>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let mut string = Vec::with_capacity(cap);
|
|
|
|
|
|
|
|
self.to_writer(&mut string)?;
|
|
|
|
|
|
|
|
Ok(string)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline] fn to_vec(&self) -> io::Result<Vec<u8>>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let mut string = Vec::new();
|
|
|
|
|
|
|
|
self.to_writer(&mut string)?;
|
|
|
|
|
|
|
|
Ok(string)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline] fn from_bytes(&mut self, mut bytes: &[u8]) -> io::Result<usize>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
self.from_reader(&mut bytes)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// A polymorphic `Object` that is owned.
|
|
|
|
/// A polymorphic `Object` that is owned.
|
|
|
@ -19,9 +37,16 @@ pub type OwnedObject<'a> = SmallBox<dyn Object + 'a, OwnedObjectStackSize>;
|
|
|
|
|
|
|
|
|
|
|
|
pub trait ObjectExt<'a>: Sized
|
|
|
|
pub trait ObjectExt<'a>: Sized
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Consume this object into a dynamic (boxed) one.
|
|
|
|
fn into_owned(self) -> OwnedObject<'a>;
|
|
|
|
fn into_owned(self) -> OwnedObject<'a>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub trait ObjectNewExt<'a>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Create a new object from this reader
|
|
|
|
|
|
|
|
fn new_obj_from_reader<R: io::Read>(from: R) -> io::Result<(OwnedObject<'a>, usize)>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, T: 'a> ObjectExt<'a> for T
|
|
|
|
impl<'a, T: 'a> ObjectExt<'a> for T
|
|
|
|
where T: Object
|
|
|
|
where T: Object
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -29,3 +54,12 @@ where T: Object
|
|
|
|
smallbox!(self)
|
|
|
|
smallbox!(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, T: 'a> ObjectNewExt<'a> for T
|
|
|
|
|
|
|
|
where T: Object + Default
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn new_obj_from_reader<R: io::Read>(mut from: R) -> io::Result<(OwnedObject<'a>, usize)> {
|
|
|
|
|
|
|
|
let mut this = T::default();
|
|
|
|
|
|
|
|
Ok((this.from_reader(&mut from)?, this.into_owned()).swap())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|