Skip to content

Commit

Permalink
rust: start to implement ZstdCompressor.stream_writer()
Browse files Browse the repository at this point in the history
I started to implemented this type then quickly found myself blocked
on PyO3/pyo3#1205 /
PyO3/pyo3#1206 due to not being able to return
`Self` from `__enter__`. We'll have to wait for a future pyo3 release
before we can finish the Rust port.
  • Loading branch information
indygreg committed Dec 31, 2020
1 parent 713b68f commit d0aff43
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
46 changes: 46 additions & 0 deletions rust-ext/src/compression_writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2020-present, Gregory Szorc
// All rights reserved.
//
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.

use {crate::compressor::CCtx, pyo3::prelude::*, std::sync::Arc};

#[pyclass]
pub struct ZstdCompressionWriter {
cctx: Arc<CCtx<'static>>,
writer: PyObject,
source_size: u64,
write_size: usize,
write_return_read: bool,
closefd: bool,
entered: bool,
closing: bool,
closed: bool,
bytes_compressed: usize,
}

impl ZstdCompressionWriter {
pub fn new(
py: Python,
cctx: Arc<CCtx<'static>>,
writer: &PyAny,
source_size: u64,
write_size: usize,
write_return_read: bool,
closefd: bool,
) -> Self {
Self {
cctx,
writer: writer.into_py(py),
source_size,
write_size,
write_return_read,
closefd,
entered: false,
closing: false,
closed: false,
bytes_compressed: 0,
}
}
}
39 changes: 39 additions & 0 deletions rust-ext/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use {
crate::{
compression_dict::ZstdCompressionDict,
compression_parameters::{CCtxParams, ZstdCompressionParameters},
compression_writer::ZstdCompressionWriter,
compressionobj::ZstdCompressionObj,
ZstdError,
},
Expand Down Expand Up @@ -453,6 +454,44 @@ impl ZstdCompressor {

Ok((total_read, total_write))
}

#[args(
writer,
size = "None",
write_size = "None",
write_return_read = "true",
closefd = "true"
)]
fn stream_writer(
&self,
py: Python,
writer: &PyAny,
size: Option<u64>,
write_size: Option<usize>,
write_return_read: bool,
closefd: bool,
) -> PyResult<ZstdCompressionWriter> {
if !writer.hasattr("write")? {
return Err(PyValueError::new_err(
"must pass object with a write() method",
));
}

self.cctx.reset();

let size = size.unwrap_or(zstd_sys::ZSTD_CONTENTSIZE_UNKNOWN as _);
let write_size = write_size.unwrap_or_else(|| unsafe { zstd_sys::ZSTD_CStreamOutSize() });

Ok(ZstdCompressionWriter::new(
py,
self.cctx.clone(),
writer,
size,
write_size,
write_return_read,
closefd,
))
}
}

pub(crate) fn init_module(module: &PyModule) -> PyResult<()> {
Expand Down
1 change: 1 addition & 0 deletions rust-ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use pyo3::{prelude::*, types::PySet};

mod compression_dict;
mod compression_parameters;
mod compression_writer;
mod compressionobj;
mod compressor;
mod constants;
Expand Down

0 comments on commit d0aff43

Please sign in to comment.