Comments (4)
Hmm how would you recommend factoring this in without making the interface difficult to use (like happened with socket2, where ordinary "safe" users would have to invoke the "unsafe" tags just to use the lib) and without forcing users to change their upstream types?
would we need a separate, parallel code path that would have the uninit type everywhere?
from rust-brotli.
@danielrh Not really, essentially both are the same under hood (pointer + size)
So you could have 1 function that takes pointer + size
E.g. how I write similar functions: https://github.com/DoumanAsh/compu/blob/master/src/encoder/mod.rs#L178-L198
You do not necessary need to expose function that takes raw pointers, but it is most convenient wrapper to get things done.
Alternatively, especially because most of the code works with slices, you can also make MaybeUninit main path and use it:
#[inline(always)]
BrotliEncoderCompressStream(
s: &mut BrotliEncoderStateStruct<Alloc>,
op: BrotliEncoderOperation,
available_in: &mut usize,
next_in_array: &[u8],
next_in_offset: &mut usize,
available_out: &mut usize,
next_out_array: &mut [u8],
next_out_offset: &mut usize,
total_out: &mut Option<usize>,
) -> i32 {
//Way one
let next_out_array = unsafe {
core::mem::transmute(next_our_array)
}
//Way two
let next_out_array = unsafe {
core::slice::from_raw_parts_mut(next_out_array.as_ptr_mut() as *mut mem::MaybeUninit<u8>, next_out_array.len())
};
//We just call Uninit variant
BrotliEncoderCompressStreamUninit(s, op, available_in, next_in_array, next_in_offset, available_out, next_out_array, next_out_offset, total_out)
}
fn BrotliEncoderCompressStreamUninit(
s: &mut BrotliEncoderStateStruct<Alloc>,
op: BrotliEncoderOperation,
available_in: &mut usize,
next_in_array: &[u8],
next_in_offset: &mut usize,
available_out: &mut usize,
next_out_array: &mut [mem::MaybeUninit<u8>],
next_out_offset: &mut usize,
total_out: &mut Option<usize>,
) -> i32 {
//todo
}
from rust-brotli.
One of the goals of this library is to have no unsafe functions. Or at least the default features would eschew unsafe. I guess we would need a feature or parallel lib for this interface.
from rust-brotli.
This is unfortunate consequence of uninitialized memory and Rust being unfriendly to it
Still, if unsafe is prohibited then it cannot be helped
But do note that it is actually necessary if you want to maintain correct C interface
Current Rust API basically forces you to assume C's API input is initialized:
E.g. here
https://github.com/dropbox/rust-brotli/blob/master/src/ffi/compressor.rs#L301
Although in practice this UB is harmless and well defined as you only write into resulting slice
from rust-brotli.
Related Issues (20)
- Support stream testing without expecting a decompressed stream
- Publish new version HOT 2
- Tags for 3.4.0 and 3.5.0 are missing HOT 1
- Linking errors when multiple brotli crate versions are used in the same project HOT 4
- Merge decompressor into this repo
- Prevent exposing internal APIs as public HOT 1
- `BrotliDecompress` panics if output writer stops accepting data HOT 3
- into_inner on Decompressor discards data due to buffering
- Add a helper for compressing a slice into a vec
- Protection against zip bombs HOT 1
- https://github.com/dropbox/dropbox-sdk-java/blob/master/License.txt
- Ghh
- Code style HOT 1
- 3.3.4 cannot be built on stable release channel
- drop in replacement of brotli, how so? HOT 1
- Changing the default quality level HOT 1
- Does rust-brotli support self-contained compressed data?
- Multithreaded compression HOT 2
- Amir
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-brotli.