3 ms·
I'm not a pro but here are my two cents. The benefit, and really the point of unsafe, is to encapsulate the unsafe stuff behind safe APIs and to use Rust's oth
by danhau 6y ago
I'm not a pro but here are my two cents.
The benefit, and really the point of unsafe, is to encapsulate the unsafe stuff behind safe APIs and to use Rust's other features, like the borrow checker for example, to enforce correct usage of such API.
Ex.:
I was using nanovg (vector graphics library in C) to build a GUI app some day. I was using Rust and a bare-bones FFI library that provided a 1:1 mapping to the C API. On top of this unsafe C-API, I built a wrapper library in safe Rust that doesn't allow me to use nanovg incorrectly. And not in the use-after-free sense of incorrect (that too), but instead in the "you can't call this function between these other two, that's undefined / will produce unexpected graphical results".
So the power of unsafe is to encapsulate "unsafe" behaviour and to enrich it with compile-time information, to build guarantees around it.
- adamnemecek 6y agoIf you are looking for a Rust nanovg port, look no further https://github.com/femtovg/femtovg https://github.com/femtovg/femtovg. We are in the process of adding a wgpu backend and we are definitely looking for contributors so contact me if you want to get involved.