sync-ptr
Sync & Send wrappers for raw pointer's in rust.
This crate uses #![no_std] and can be used in projects that do not use the rust standard library.
Intended use
This crate is intended for handles or pointers to data behind an FFI boundary that is known to be Send or Sync.
Example where this is most likely the case:
- shared memory from mmap() or MapViewOfFile()
- global JNI handles
- ffi mutex handles etc
- ...
I would like to mention that this crate does not magically make any C/FFI pointers into arbitrary data Send or Sync safely. Using pointers/handles from a different thread can be UB in rust, or you may cause UB on the C/FFI side.
This crate is not intended for usage with pointers to rust data. While it can be used like this, one has to be VERY careful to avoid UB.
Example
use c_void;
use null_mut;
use *;
Why not just make RustControlStructureThatIsNowSend implement Send directly?
This is prone to error as once a struct is "unsafe impl Sync," for example, it will be Sync no matter what struct members get added later. If the initial reason for that unsafe impl was a raw pointer, then the compiler has no opportunity to inform the Human that adding a RefCell to such a struct is maybe not a good idea.
In addition, there are sometimes cases where one only needs to send a single pointer, and writing an unsafe impl wrapper struct everytime is annoying.