Force send of raw pointer

The ONNX C API provides a *mut OrtSession pointer. And it is designed to be accessed by multiple threads at the same time.

But Rust does not allow that because a raw pointer is not Send.
How can I force add Send trait in this case?

You can wrap it in a newtype and unsafely implement Send for the wrapper. (Although you have to be very careful that you understand the exact thread safety guarantees of the API – Rust doesn't allow you to violate its memory model even in FFI, so doing so results in UB.)

1 Like

As long as OrtSession is designed to be thread-safe, it's perfectly sound to mark a type containing it as Send (transferrable across threads) and Sync (able to be used concurrently).

The unsafe bit in the unsafe impl Send for OrdSession just indicates that you've manually verified the type is safe for sending to other threads.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.