Skip to content

Commit f5c8c58

Browse files
committed
Use Rust's BufWriter
Rust's BufWriter is highly optimized. Use it instead of a custom one. Wrap it in a newtype so that we can implement `cbor4ii`s `enc::Write`.
1 parent d8be9fa commit f5c8c58

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

src/lib.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use std::io::{BufWriter as IoBufWriter, Write};
2+
13
use anyhow::{anyhow, Result};
24
use cbor4ii::core::{
35
dec::{self, Decode, Read},
46
enc::{self, Encode},
57
major, types,
6-
utils::BufWriter,
78
};
89
use cid::{multibase, Cid};
910
use pyo3::pybacked::PyBackedStr;
@@ -18,6 +19,28 @@ mod marker {
1819
pub const F64: u8 = 0xfb;
1920
}
2021

22+
struct BufWriter<W: Write>(IoBufWriter<W>);
23+
24+
impl<W: Write> BufWriter<W> {
25+
pub fn new(inner: W) -> Self {
26+
BufWriter(IoBufWriter::new(inner))
27+
}
28+
29+
pub fn buffer(&self) -> &[u8] {
30+
self.0.buffer()
31+
}
32+
}
33+
34+
impl<W: Write> enc::Write for BufWriter<W> {
35+
type Error = std::io::Error;
36+
37+
#[inline]
38+
fn push(&mut self, input: &[u8]) -> Result<(), Self::Error> {
39+
self.0.write_all(input)?;
40+
Ok(())
41+
}
42+
}
43+
2144
// Based on cbor4ii/src/utils.rs.
2245
/// An in-memory reader.
2346
struct SliceReader<'a> {

0 commit comments

Comments
 (0)