Skip to content

Commit

Permalink
rust: move CDict wrapper to own module
Browse files Browse the repository at this point in the history
I want to consolidate all the safe wrappers in their own module
to make it easier to port them upstream.
  • Loading branch information
indygreg committed Dec 30, 2020
1 parent f98195f commit 8e4702a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
20 changes: 3 additions & 17 deletions rust-ext/src/compression_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use {
crate::{
compression_parameters::{get_cctx_parameter, int_to_strategy, ZstdCompressionParameters},
zstd_safe::CDict,
ZstdError,
},
pyo3::{
Expand All @@ -16,23 +17,8 @@ use {
types::{PyBytes, PyList},
wrap_pyfunction,
},
std::marker::PhantomData,
};

/// Safe wrapper for ZSTD_CDict instances.
pub struct CDict<'a>(*mut zstd_sys::ZSTD_CDict, PhantomData<&'a ()>);

impl<'a> Drop for CDict<'a> {
fn drop(&mut self) {
unsafe {
zstd_sys::ZSTD_freeCDict(self.0);
}
}
}

unsafe impl<'a> Send for CDict<'a> {}
unsafe impl<'a> Sync for CDict<'a> {}

#[pyclass]
pub struct ZstdCompressionDict {
/// Internal format of dictionary data.
Expand All @@ -58,7 +44,7 @@ pub struct ZstdCompressionDict {
impl ZstdCompressionDict {
pub(crate) fn load_into_cctx(&self, cctx: *mut zstd_sys::ZSTD_CCtx) -> PyResult<()> {
let zresult = if let Some(cdict) = &self.cdict {
unsafe { zstd_sys::ZSTD_CCtx_refCDict(cctx, cdict.0) }
unsafe { zstd_sys::ZSTD_CCtx_refCDict(cctx, cdict.ptr) }
} else {
unsafe {
zstd_sys::ZSTD_CCtx_loadDictionary_advanced(
Expand Down Expand Up @@ -195,7 +181,7 @@ impl ZstdCompressionDict {
return Err(ZstdError::new_err("unable to precompute dictionary"));
}

self.cdict = Some(CDict(cdict, PhantomData));
self.cdict = Some(CDict::from_ptr(cdict));

Ok(())
}
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 @@ -13,6 +13,7 @@ mod compressor;
mod constants;
mod exceptions;
mod frame_parameters;
mod zstd_safe;

use exceptions::ZstdError;

Expand Down
35 changes: 35 additions & 0 deletions rust-ext/src/zstd_safe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 std::marker::PhantomData;

/// Safe wrapper for ZSTD_CDict instances.
pub(crate) struct CDict<'a> {
// TODO don't expose field.
pub(crate) ptr: *mut zstd_sys::ZSTD_CDict,
_phantom: PhantomData<&'a ()>,
}

impl<'a> CDict<'a> {
pub fn from_ptr(ptr: *mut zstd_sys::ZSTD_CDict) -> Self {
Self {
ptr,
_phantom: PhantomData,
}
}
}

impl<'a> Drop for CDict<'a> {
fn drop(&mut self) {
unsafe {
zstd_sys::ZSTD_freeCDict(self.ptr);
}
}
}

unsafe impl<'a> Send for CDict<'a> {}

unsafe impl<'a> Sync for CDict<'a> {}

0 comments on commit 8e4702a

Please sign in to comment.