#static #mutex #leak #rwlock #ffi

no-std static_leak

Leak references with static lifetimes from static Mutexes and RwLocks

1 unstable release

0.1.0 Nov 10, 2022

#2808 in Rust patterns

Download history 8/week @ 2024-02-26 6/week @ 2024-03-11 67/week @ 2024-03-25 15/week @ 2024-04-01

88 downloads per month

MIT/Apache

9KB
120 lines

static_leak

Build Status codecov badge License: MIT License: Apache 2.0

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.

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

Dependencies

~0–11MB
~95K SLoC