From 8e4702a1e84ddc87cfbd98cdce75b8967ccaac58 Mon Sep 17 00:00:00 2001 From: Gregory Szorc Date: Wed, 30 Dec 2020 09:20:48 -0700 Subject: [PATCH] rust: move CDict wrapper to own module I want to consolidate all the safe wrappers in their own module to make it easier to port them upstream. --- rust-ext/src/compression_dict.rs | 20 +++--------------- rust-ext/src/lib.rs | 1 + rust-ext/src/zstd_safe.rs | 35 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 rust-ext/src/zstd_safe.rs diff --git a/rust-ext/src/compression_dict.rs b/rust-ext/src/compression_dict.rs index 970d2789..49a48446 100644 --- a/rust-ext/src/compression_dict.rs +++ b/rust-ext/src/compression_dict.rs @@ -7,6 +7,7 @@ use { crate::{ compression_parameters::{get_cctx_parameter, int_to_strategy, ZstdCompressionParameters}, + zstd_safe::CDict, ZstdError, }, pyo3::{ @@ -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. @@ -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( @@ -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(()) } diff --git a/rust-ext/src/lib.rs b/rust-ext/src/lib.rs index e8f838dd..6417249b 100644 --- a/rust-ext/src/lib.rs +++ b/rust-ext/src/lib.rs @@ -13,6 +13,7 @@ mod compressor; mod constants; mod exceptions; mod frame_parameters; +mod zstd_safe; use exceptions::ZstdError; diff --git a/rust-ext/src/zstd_safe.rs b/rust-ext/src/zstd_safe.rs new file mode 100644 index 00000000..55b7076e --- /dev/null +++ b/rust-ext/src/zstd_safe.rs @@ -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> {}