TL;DR is it possible, if not already proposed, to add a Cargo option so that a crate can only be imported once?
I have seen some discussions around about the problem of being safe when using unsafe external FFI. One major concern is that you write a beautiful
-sys crate to wrap a C lib and a higher-level safe crate which uses an internal thread locking system (like a
std::Mutex) to make everything work in a safe way. Then someone very silly uses directly some
-sys functions inside his crate, and a big project that use both crates suddenly triggers UB without using a single line of unsafe code.
We love ownership and borrowing, why not applying the same concepts to the handling of the crates? I mean, we can say that, with the dependency declarations, a crate is normally available to be referenced through
extern crate. What if could have an option inside
cargo.toml to say that a crate must be
extern crate? Setting this parameter inside a
-sys crate, it won’t be possible to use two different crates that need to own it.
Said that, I am quite new with Rust, and maybe something similar has been already discussed somewhere (in this case it is my fault i did not find anything). I am also aware that it is not straightforward to apply this concept on different versions of the same crate (maybe a crate can import and own a previous version of itself?), and obviously there are issues with this idea I am not able to see in this moment. Nevertheless, maybe someone more experienced than me could assess this idea and, if if could be valuable, a pre-RFC could be written.
What do you think?