Adjust internal trait API
This commit is contained in:
parent
9f83756931
commit
7cc7bd783d
|
@ -178,7 +178,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
|||
|
||||
fn serialize_seq(self, count: Option<usize>) -> Result<Self::SerializeSeq> {
|
||||
let c = match count {
|
||||
Some(n) => self.write.start_sequence(n as u64)?,
|
||||
Some(n) => self.write.start_sequence(n)?,
|
||||
None => match self.write.stream_sequence()? {
|
||||
Some(c) => c,
|
||||
None => return Err(Error::StreamingSerializationUnsupported),
|
||||
|
@ -188,7 +188,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
|||
}
|
||||
|
||||
fn serialize_tuple(self, count: usize) -> Result<Self::SerializeTuple> {
|
||||
let mut c = self.write.start_record(count as u64)?;
|
||||
let mut c = self.write.start_record(count)?;
|
||||
let p = self.write.write_symbol("tuple")?;
|
||||
self.write.extend_compound(&mut c, p)?;
|
||||
Ok(SerializeCompound { ser: self, c })
|
||||
|
@ -197,7 +197,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
|||
fn serialize_tuple_struct(self, name: &'static str, count: usize) ->
|
||||
Result<Self::SerializeTupleStruct>
|
||||
{
|
||||
let mut c = self.write.start_record(count as u64)?;
|
||||
let mut c = self.write.start_record(count)?;
|
||||
let p = self.write.write_symbol(name)?;
|
||||
self.write.extend_compound(&mut c, p)?;
|
||||
Ok(SerializeCompound { ser: self, c })
|
||||
|
@ -210,7 +210,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
|||
count: usize) ->
|
||||
Result<Self::SerializeTupleVariant>
|
||||
{
|
||||
let mut c = self.write.start_record(count as u64)?;
|
||||
let mut c = self.write.start_record(count)?;
|
||||
let p = self.write.write_symbol(variant_name)?;
|
||||
self.write.extend_compound(&mut c, p)?;
|
||||
Ok(SerializeCompound { ser: self, c })
|
||||
|
@ -218,7 +218,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
|||
|
||||
fn serialize_map(self, count: Option<usize>) -> Result<Self::SerializeMap> {
|
||||
let d = match count {
|
||||
Some(n) => self.write.start_dictionary(n as u64)?,
|
||||
Some(n) => self.write.start_dictionary(n)?,
|
||||
None => match self.write.stream_dictionary()? {
|
||||
Some(d) => d,
|
||||
None => return Err(Error::StreamingSerializationUnsupported),
|
||||
|
@ -228,7 +228,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
|||
}
|
||||
|
||||
fn serialize_struct(self, name: &'static str, count: usize) -> Result<Self::SerializeStruct> {
|
||||
let mut c = self.write.start_record(count as u64)?;
|
||||
let mut c = self.write.start_record(count)?;
|
||||
let p = self.write.write_symbol(name)?;
|
||||
self.write.extend_compound(&mut c, p)?;
|
||||
Ok(SerializeCompound { ser: self, c })
|
||||
|
@ -241,7 +241,7 @@ impl<'a, 'b, W: Writer> serde::Serializer for &'a mut Serializer<'b, W> {
|
|||
count: usize) ->
|
||||
Result<Self::SerializeStructVariant>
|
||||
{
|
||||
let mut c = self.write.start_record(count as u64)?;
|
||||
let mut c = self.write.start_record(count)?;
|
||||
let p = self.write.write_symbol(variant_name)?;
|
||||
self.write.extend_compound(&mut c, p)?;
|
||||
Ok(SerializeCompound { ser: self, c })
|
||||
|
|
|
@ -13,17 +13,18 @@ pub fn write_op<W: std::io::Write>(w: &mut PackedWriter<W>, op: Op, arg: u8) ->
|
|||
w.0.write_all(&[(u8::from(op) << 4) | (arg & 15)])
|
||||
}
|
||||
|
||||
pub fn write_header<W: std::io::Write>(w: &mut PackedWriter<W>, op: Op, wirelength: u64) -> Result<()> {
|
||||
pub fn write_header<W: std::io::Write>(w: &mut PackedWriter<W>, op: Op, wirelength: usize) -> Result<()> {
|
||||
if wirelength < 15 {
|
||||
write_op(w, op, wirelength as u8)
|
||||
} else {
|
||||
write_op(w, op, 15)?;
|
||||
varint(w.0, wirelength)
|
||||
varint(w.0, wirelength as u64)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_atom<W: std::io::Write>(w: &mut PackedWriter<W>, minor: AtomMinor, bs: &[u8]) -> Result<()> {
|
||||
write_header(w, Op::Atom(minor), bs.len() as u64)?;
|
||||
write_header(w, Op::Atom(minor), bs.len())?;
|
||||
w.0.write_all(bs)
|
||||
}
|
||||
|
||||
|
@ -56,10 +57,6 @@ impl<'w, W: std::io::Write> Writer for PackedWriter<'w, W> {
|
|||
type StreamedAtom = ();
|
||||
type KeyPointer = ();
|
||||
|
||||
fn supports_streaming(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn start_annotation(&mut self) -> Result<Self::Annotation> {
|
||||
Ok(())
|
||||
}
|
||||
|
@ -199,7 +196,7 @@ impl<'w, W: std::io::Write> Writer for PackedWriter<'w, W> {
|
|||
fn write_u128(&mut self, v: u128) -> Result<()> {
|
||||
if let Ok(w) = v.try_into() { return self.write_i128(w) }
|
||||
let bs: [u8; 16] = v.to_be_bytes();
|
||||
write_header(self, Op::Atom(AtomMinor::SignedInteger), (bs.len() + 1) as u64)?;
|
||||
write_header(self, Op::Atom(AtomMinor::SignedInteger), bs.len() + 1)?;
|
||||
self.0.write_all(&[0])?;
|
||||
self.0.write_all(&bs)
|
||||
}
|
||||
|
@ -246,17 +243,17 @@ impl<'w, W: std::io::Write> Writer for PackedWriter<'w, W> {
|
|||
write_op(self, Op::Misc(0), 4)
|
||||
}
|
||||
|
||||
fn start_record(&mut self, field_count: u64) -> Result<Self::Compound> {
|
||||
fn start_record(&mut self, field_count: usize) -> Result<Self::Compound> {
|
||||
write_header(self, Op::Compound(CompoundMinor::Record), field_count + 1)?;
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
fn start_sequence(&mut self, item_count: u64) -> Result<Self::Compound> {
|
||||
fn start_sequence(&mut self, item_count: usize) -> Result<Self::Compound> {
|
||||
write_header(self, Op::Compound(CompoundMinor::Sequence), item_count)?;
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
fn start_set(&mut self, item_count: u64) -> Result<Self::Compound> {
|
||||
fn start_set(&mut self, item_count: usize) -> Result<Self::Compound> {
|
||||
write_header(self, Op::Compound(CompoundMinor::Set), item_count)?;
|
||||
Ok(false)
|
||||
}
|
||||
|
@ -288,7 +285,7 @@ impl<'w, W: std::io::Write> Writer for PackedWriter<'w, W> {
|
|||
}
|
||||
}
|
||||
|
||||
fn start_dictionary(&mut self, entry_count: u64) -> Result<Self::Dictionary> {
|
||||
fn start_dictionary(&mut self, entry_count: usize) -> Result<Self::Dictionary> {
|
||||
write_header(self, Op::Compound(CompoundMinor::Dictionary), entry_count << 1)?;
|
||||
Ok(false)
|
||||
}
|
||||
|
|
|
@ -14,8 +14,6 @@ pub trait Writer {
|
|||
type StreamedAtom;
|
||||
type KeyPointer;
|
||||
|
||||
fn supports_streaming(&self) -> bool;
|
||||
|
||||
fn align(&mut self, natural_chunksize: u64) -> Result<()>;
|
||||
|
||||
fn start_annotation(&mut self) -> Result<Self::Annotation>;
|
||||
|
@ -49,16 +47,16 @@ pub trait Writer {
|
|||
fn extend_atom(&mut self, s: &mut Self::StreamedAtom, bs: &[u8]) -> Result<()>;
|
||||
fn end_atom(&mut self, s: Self::StreamedAtom) -> Result<Self::Pointer>;
|
||||
|
||||
fn start_record(&mut self, field_count: u64) -> Result<Self::Compound>;
|
||||
fn start_sequence(&mut self, item_count: u64) -> Result<Self::Compound>;
|
||||
fn start_set(&mut self, item_count: u64) -> Result<Self::Compound>;
|
||||
fn start_record(&mut self, field_count: usize) -> Result<Self::Compound>;
|
||||
fn start_sequence(&mut self, item_count: usize) -> Result<Self::Compound>;
|
||||
fn start_set(&mut self, item_count: usize) -> Result<Self::Compound>;
|
||||
fn stream_record(&mut self) -> Result<Option<Self::Compound>>;
|
||||
fn stream_sequence(&mut self) -> Result<Option<Self::Compound>>;
|
||||
fn stream_set(&mut self) -> Result<Option<Self::Compound>>;
|
||||
fn extend_compound(&mut self, s: &mut Self::Compound, value_p: Self::Pointer) -> Result<()>;
|
||||
fn end_compound(&mut self, s: Self::Compound) -> Result<Self::Pointer>;
|
||||
|
||||
fn start_dictionary(&mut self, entry_count: u64) -> Result<Self::Dictionary>;
|
||||
fn start_dictionary(&mut self, entry_count: usize) -> Result<Self::Dictionary>;
|
||||
fn stream_dictionary(&mut self) -> Result<Option<Self::Dictionary>>;
|
||||
fn extend_dictionary_key(&mut self, s: &mut Self::Dictionary, key_p: Self::Pointer) -> Result<Self::KeyPointer>;
|
||||
fn extend_dictionary_value(&mut self, s: &mut Self::Dictionary, key_p: Self::KeyPointer, value_p: Self::Pointer) -> Result<()>;
|
||||
|
@ -94,7 +92,7 @@ pub trait Writer {
|
|||
Value::ByteString(ref bs) => self.write_bytes(bs),
|
||||
Value::Symbol(ref s) => self.write_symbol(s),
|
||||
Value::Record(r) => {
|
||||
let mut c = self.start_record(r.arity() as u64)?;
|
||||
let mut c = self.start_record(r.arity())?;
|
||||
let p = self.write(r.label())?;
|
||||
self.extend_compound(&mut c, p)?;
|
||||
for f in r.fields() {
|
||||
|
@ -104,7 +102,7 @@ pub trait Writer {
|
|||
self.end_compound(c)
|
||||
}
|
||||
Value::Sequence(ref vs) => {
|
||||
let mut c = self.start_sequence(vs.len() as u64)?;
|
||||
let mut c = self.start_sequence(vs.len())?;
|
||||
for v in vs {
|
||||
let p = self.write(v)?;
|
||||
self.extend_compound(&mut c, p)?;
|
||||
|
@ -112,7 +110,7 @@ pub trait Writer {
|
|||
self.end_compound(c)
|
||||
}
|
||||
Value::Set(ref vs) => {
|
||||
let mut c = self.start_set(vs.len() as u64)?;
|
||||
let mut c = self.start_set(vs.len())?;
|
||||
for v in vs {
|
||||
let p = self.write(v)?;
|
||||
self.extend_compound(&mut c, p)?;
|
||||
|
@ -120,7 +118,7 @@ pub trait Writer {
|
|||
self.end_compound(c)
|
||||
}
|
||||
Value::Dictionary(ref vs) => {
|
||||
let mut d = self.start_dictionary(vs.len() as u64)?;
|
||||
let mut d = self.start_dictionary(vs.len())?;
|
||||
for (k, v) in vs {
|
||||
let p = self.write(k)?;
|
||||
let kp = self.extend_dictionary_key(&mut d, p)?;
|
||||
|
@ -134,10 +132,13 @@ pub trait Writer {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn bigvarint<W: std::io::Write>(w: &mut W, mut v: u64) -> Result<()> {
|
||||
pub fn bigvarint<W: std::io::Write>(w: &mut W, mut v: u64) -> Result<usize> {
|
||||
let mut byte_count = 0;
|
||||
loop {
|
||||
byte_count = byte_count + 4;
|
||||
if v < (2 << 31) {
|
||||
return w.write_all(&(v as u32).to_le_bytes());
|
||||
w.write_all(&(v as u32).to_le_bytes())?;
|
||||
return Ok(byte_count);
|
||||
} else {
|
||||
w.write_all(&(((v & 0x7fffffff) + (2 << 31)) as u32).to_le_bytes())?;
|
||||
v = v >> 31;
|
||||
|
@ -145,10 +146,13 @@ pub fn bigvarint<W: std::io::Write>(w: &mut W, mut v: u64) -> Result<()> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn varint<W: std::io::Write>(w: &mut W, mut v: u64) -> Result<()> {
|
||||
pub fn varint<W: std::io::Write>(w: &mut W, mut v: u64) -> Result<usize> {
|
||||
let mut byte_count = 0;
|
||||
loop {
|
||||
byte_count = byte_count + 1;
|
||||
if v < 128 {
|
||||
return w.write_all(&[v as u8])
|
||||
w.write_all(&[v as u8])?;
|
||||
return Ok(byte_count);
|
||||
} else {
|
||||
w.write_all(&[((v & 0x7f) + 128) as u8])?;
|
||||
v = v >> 7;
|
||||
|
|
Loading…
Reference in New Issue