Skip to content

Commit

Permalink
Fix writer copy
Browse files Browse the repository at this point in the history
  • Loading branch information
brian14708 committed May 25, 2024
1 parent 258f861 commit 2632b31
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
27 changes: 19 additions & 8 deletions tsar-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ use rayon::prelude::*;

#[pyclass(module = "tsar.tsar")]
struct Writer {
w: tsar::Builder<std::fs::File>,
w: Option<tsar::Builder<std::fs::File>>,
}

#[pymethods]
impl Writer {
#[new]
fn new(dst: &str) -> PyResult<Self> {
Ok(Self {
w: tsar::Builder::new(std::fs::File::create(dst)?),
w: Some(tsar::Builder::new(std::fs::File::create(dst)?)),
})
}

pub fn write_file(&mut self, name: String, d: &[u8]) -> PyResult<()> {
self.w.add_file(name, std::io::Cursor::new(d)).unwrap();
self.w
.as_mut()
.expect("The writer has already been closed.")
.add_file(name, std::io::Cursor::new(d))
.unwrap();
Ok(())
}

Expand Down Expand Up @@ -57,18 +61,25 @@ impl Writer {
_ => None,
};

let w = self
.w
.as_mut()
.expect("The writer has already been closed.");

match ty {
Some(ty) => self.w.add_blob(name, data, ty, &dims, opt),
_ => self
.w
.add_blob(name, data, tsar::DataType::Byte, &[data.len()], opt),
Some(ty) => w.add_blob(name, data, ty, &dims, opt),
_ => w.add_blob(name, data, tsar::DataType::Byte, &[data.len()], opt),
}
.unwrap();
Ok(())
}

pub fn close(&mut self) -> PyResult<()> {
self.w.finish().unwrap();
self.w
.take()
.expect("The writer has already been closed.")
.finish()
.unwrap();
Ok(())
}
}
Expand Down
18 changes: 10 additions & 8 deletions tsar-rs/src/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,17 @@ impl<W: Write + Seek> Builder<W> {
self.write_chunks(b, [data])
}

pub fn finish(&mut self) -> Result<()> {
self.z
.start_file(paths::BUNDLE_META_PATH, SimpleFileOptions::default())?;
self.meta.blobs.sort_by(|a, b| a.name.cmp(&b.name));
pub fn finish(self) -> Result<()> {
let Self {
mut z,
mut meta,
chunks: _,
} = self;
z.start_file(paths::BUNDLE_META_PATH, SimpleFileOptions::default())?;
meta.blobs.sort_by(|a, b| a.name.cmp(&b.name));
// TODO check target_file contiguous
self.meta
.write_to(&mut CodedOutputStream::new(&mut self.z))
.unwrap();
self.z.finish()?;
meta.write_to(&mut CodedOutputStream::new(&mut z)).unwrap();
z.finish()?;
Ok(())
}

Expand Down

0 comments on commit 2632b31

Please sign in to comment.