Crate static_leak

source ·
Expand description

This crate extends Mutexes and RwLocks wrapping references with static lifetimes to leak these raw underlying references. This can be useful to safely obtain a static mutable reference without using any unsafe code when interacting with a legacy C project via FFI.

use static_leak::RwLockWriteGuardExtension;

lazy_static! {
    static ref VAR: RwLock<i8> = RwLock::new(42);
}

fn bar(_: &'static mut i8) {}

fn main() {
    bar(RwLockWriteGuard::leak(VAR.write().unwrap()));
}

This crate supports implementations from the following crates, which are activated through the respective feature flags:

  • std
  • async-std
  • spin

Traits

Improves the support for static references of MutexGuard.
Improves the support for static references of RwLockReadGuard.
Improves the support for static references of RwLockWriteGuard.