reworked `Object`: is now a concrete single type (Currently an alias for serde_json::Value, this may prove a sufficient medium, if not, we can make our own pipeline intermedate type)
parent
1f1b6e68df
commit
191a466809
@ -1,65 +1,9 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use std::io;
|
use std::io;
|
||||||
use smallbox::{
|
|
||||||
SmallBox,
|
|
||||||
space,
|
|
||||||
smallbox,
|
|
||||||
};
|
|
||||||
|
|
||||||
type OwnedObjectStackSize = space::S4;
|
/// The intermediate representation object.
|
||||||
|
///
|
||||||
/// An object that can be expose serialise+deserialise methods polymorphically.
|
/// # Pipeline
|
||||||
pub trait Object {
|
/// This is used as the deserialisation target output, which is then re-serialized with the other serializer to output:
|
||||||
fn to_writer(&self, to: &mut dyn io::Write) -> io::Result<usize>;
|
/// * input -(de)> `Object` -(ser)> output
|
||||||
fn from_reader(&mut self, from: &mut dyn io::Read) -> io::Result<usize>; //ugh, this SUUUCKS...
|
pub type Object = serde_json::Value;
|
||||||
|
|
||||||
|
|
||||||
#[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.
|
|
||||||
pub type OwnedObject<'a> = SmallBox<dyn Object + 'a, OwnedObjectStackSize>;
|
|
||||||
|
|
||||||
pub trait ObjectExt<'a>: Sized
|
|
||||||
{
|
|
||||||
/// Consume this object into a dynamic (boxed) one.
|
|
||||||
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
|
|
||||||
where T: Object
|
|
||||||
{
|
|
||||||
#[inline] fn into_owned(self) -> OwnedObject<'a> {
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in new issue