Originally posted by Weasel
View Post
If they're communicating using the C ABI, then it's your responsibility to ensure that Rust's invariants are upheld and one of those invariants is that only one binding can be mut at any given time. Using casts to gain mut invokes undefined behaviour. (For the same reason that it's a bug to bypass a reader-writer lock in a threaded application for the sake of convenience. Think of Rust's type system as enforcing zero-overhead reader-writer locking on every variable binding using compile-time validation.)
That's one of the various reasons that FFI via the C ABI must be done within an unsafe block. Such a block tells the compiler to temporarily relax certain invariants on the promise that the programmer will have re-established any broken invariants by the end of the block. Proper Rust APIs wraip unsafe blocks in APIs which can only be used safely, thus simplifying debugging by narrowing down where the bug could originate.
Leave a comment: