From 00b7b1e1ba9cd7b05bf68563df0faa5ccce1d4c1 Mon Sep 17 00:00:00 2001 From: Maxime Lenoir Date: Thu, 18 Aug 2016 10:24:42 +0200 Subject: [PATCH] Make Encoder and Decoder Send LZ4F{Compression,Decompression}Context are raw pointers which make Encoder and Decoder non-Send. However, given that those contexts are uniquely owned by the Encoder/Decoder, it is safe to implement Send on them, making Encoder/Decoder Send themselves. --- src/decoder.rs | 9 ++++++++- src/encoder.rs | 9 ++++++++- src/liblz4.rs | 10 ++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index 7ca62b4a0..7f5f3a376 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -96,7 +96,7 @@ impl Read for Decoder { impl DecoderContext { fn new() -> Result { - let mut context: LZ4FDecompressionContext = ptr::null_mut(); + let mut context = LZ4FDecompressionContext(ptr::null_mut()); try!(check_error(unsafe { LZ4F_createDecompressionContext(&mut context, LZ4F_VERSION) })); Ok(DecoderContext { c: context }) } @@ -277,4 +277,11 @@ mod test { fn random_stream(rng: &mut R, size: usize) -> Vec { rand::sample(rng, 0x00..0xFF, size) } + + #[test] + fn test_decoder_send() { + fn check_send(_: &S) {} + let dec = Decoder::new(Cursor::new(Vec::new())).unwrap(); + check_send(&dec); + } } diff --git a/src/encoder.rs b/src/encoder.rs index 627dd6a61..5625546da 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -170,7 +170,7 @@ impl Write for Encoder { impl EncoderContext { fn new() -> Result { - let mut context: LZ4FCompressionContext = ptr::null_mut(); + let mut context = LZ4FCompressionContext(ptr::null_mut()); try!(check_error(unsafe { LZ4F_createCompressionContext(&mut context, LZ4F_VERSION) })); Ok(EncoderContext { c: context }) } @@ -209,4 +209,11 @@ mod test { let (_, result) = encoder.finish(); result.unwrap(); } + + #[test] + fn test_encoder_send() { + fn check_send(_: &S) {} + let enc = EncoderBuilder::new().build(Vec::new()); + check_send(&enc); + } } diff --git a/src/liblz4.rs b/src/liblz4.rs index 8b6b3c9ba..c78f15e27 100644 --- a/src/liblz4.rs +++ b/src/liblz4.rs @@ -7,9 +7,15 @@ use std::ffi::CStr; use libc::{c_void, c_char, c_uint, size_t}; -pub type LZ4FCompressionContext = *mut c_void; +#[derive(Clone, Copy)] +#[repr(C)] +pub struct LZ4FCompressionContext(pub *mut c_void); +unsafe impl Send for LZ4FCompressionContext {} -pub type LZ4FDecompressionContext = *mut c_void; +#[derive(Clone, Copy)] +#[repr(C)] +pub struct LZ4FDecompressionContext(pub *mut c_void); +unsafe impl Send for LZ4FDecompressionContext {} pub type LZ4FErrorCode = size_t;