Small win for writing large integers

This commit is contained in:
Tony Garnock-Jones 2022-07-13 14:09:21 +02:00
parent 18b9cb5df4
commit adc4f10d4c
1 changed files with 11 additions and 11 deletions

View File

@ -66,13 +66,13 @@ impl<W: io::Write> PackedWriter<W> {
}
#[inline(always)]
pub fn write_all(&mut self, bs: &[u8]) {
self.buffer.write_all(Cow::Borrowed(bs))
pub fn write_all(&mut self, bs: Cow<'_, [u8]>) {
self.buffer.write_all(bs)
}
#[inline(always)]
pub fn write_integer(&mut self, bs: &[u8]) -> io::Result<()> {
self.write_atom(Tag::SignedInteger, bs)
self.write_atom(Tag::SignedInteger, Cow::Borrowed(bs))
}
#[inline(always)]
@ -81,7 +81,7 @@ impl<W: io::Write> PackedWriter<W> {
}
#[inline(always)]
pub fn write_atom(&mut self, tag: Tag, bs: &[u8]) -> io::Result<()> {
pub fn write_atom(&mut self, tag: Tag, bs: Cow<'_, [u8]>) -> io::Result<()> {
self.write_tag(tag);
self.write_all(bs);
self.finish_item_if_toplevel()
@ -182,14 +182,14 @@ impl<W: io::Write> Writer for PackedWriter<W>
#[inline(always)]
fn write_f32(&mut self, v: f32) -> io::Result<()> {
self.write_tag(Tag::Float);
self.write_all(&u32::to_be_bytes(f32::to_bits(v)));
self.write_all(Cow::Borrowed(&u32::to_be_bytes(f32::to_bits(v))));
self.finish_item_if_toplevel()
}
#[inline(always)]
fn write_f64(&mut self, v: f64) -> io::Result<()> {
self.write_tag(Tag::Float);
self.write_all(&u64::to_be_bytes(f64::to_bits(v)));
self.write_all(Cow::Borrowed(&u64::to_be_bytes(f64::to_bits(v))));
self.finish_item_if_toplevel()
}
@ -315,7 +315,7 @@ impl<W: io::Write> Writer for PackedWriter<W>
let bs: [u8; 16] = v.to_be_bytes();
self.write_tag(Tag::SignedInteger);
self.write_byte(0);
self.write_all(&bs);
self.write_all(Cow::Borrowed(&bs));
Ok(())
}
@ -326,7 +326,7 @@ impl<W: io::Write> Writer for PackedWriter<W>
None => {
match v.to_i128() {
Some(n) => self.write_i128(n),
None => self.write_atom(Tag::SignedInteger, &v.to_signed_bytes_be()),
None => self.write_atom(Tag::SignedInteger, Cow::Owned(v.to_signed_bytes_be())),
}
}
}
@ -335,19 +335,19 @@ impl<W: io::Write> Writer for PackedWriter<W>
#[inline(always)]
fn write_string(&mut self, v: &str) -> io::Result<()> {
self.write_tag(Tag::String);
self.write_all(v.as_bytes());
self.write_all(Cow::Borrowed(v.as_bytes()));
self.write_byte(0);
self.finish_item_if_toplevel()
}
#[inline(always)]
fn write_bytes(&mut self, v: &[u8]) -> io::Result<()> {
self.write_atom(Tag::ByteString, v)
self.write_atom(Tag::ByteString, Cow::Borrowed(v))
}
#[inline(always)]
fn write_symbol(&mut self, v: &str) -> io::Result<()> {
self.write_atom(Tag::Symbol, v.as_bytes())
self.write_atom(Tag::Symbol, Cow::Borrowed(v.as_bytes()))
}
#[inline(always)]