clang-rs icon indicating copy to clipboard operation
clang-rs copied to clipboard

`struct Clang` should not implement `Send`

Open nagisa opened this issue 4 years ago • 1 comments

Creating an instance of Clang involves calling clang-sys::load() which dynamically loads the clang library for the currently executing thread.

With Clang: Send (which is implemented automatically due to the auto-trait rules) the abstraction leaks when you do something like:

let clang = Clang::new();
std::thread::spawn(|| {
    /* use clang here */
});

When code is written this way, clang attempts to directly access functions from libclang without checking whether the library is loaded and fail at the assert here.

This bug occurred in actual code here https://github.com/twistedfall/opencv-rust/blob/master/build.rs#L99-L116

Ultimately I think using thread-local storage here is really an instance of superfluous complexity and the library would be way better off with plain global shared variable with an atomic and a std::sync::Once or a lazy_static.

nagisa avatar May 16 '20 21:05 nagisa