Can a struct containing a raw pointer implement Send and be FFI safe?

By default Rust assumes *mut T is not safe to send between threads, and this means structs containing it are not safe either.

You can tell Rust that it is safe indeed:

unsafe impl Send for Storage {}

It relies entirely on your knowledge of how C uses data behind this pointer. Implementing Send means C won’t rely on thread-local storage or thread-specific locks when using the object behind this pointer (paradoxically, that’s true for most “thread-unsafe” C code).

It doesn’t require C to handle access from multiple threads at once — that’s what Sync is for.

Leave a Comment