|
1 | | -# manual-static |
2 | | -Provides `ManualStatic` that is a wrapper which allows to manually manage `'static` lifetimes. It is unsafe but with `debug_assertions` it panics when the usage is wrong. |
| 1 | +# Manually-static: Bridging the `'static` Gap with Debug-Time Safety |
| 2 | + |
| 3 | +This crate provides `ManuallyStatic<T>`, |
| 4 | +a powerful wrapper that allows you to manually manage `'static` lifetimes for your data. |
| 5 | +While it uses `unsafe` under the hood, it also uses robust debug-time checks that panic on incorrect usage. |
| 6 | +This means you can confidently assert `'static` guarantees in your code, |
| 7 | +knowing that misuse will be caught during development and testing. |
| 8 | + |
| 9 | +## Why `manually-static`? |
| 10 | +In concurrent programming with threads or asynchronous operations, data often needs to be `'static` to |
| 11 | +be shared or moved across task boundaries. |
| 12 | +However, sometimes you have a logical |
| 13 | +guarantee that a reference will live for the entire program's duration, |
| 14 | +even if you can't easily prove it to the compiler through standard means. |
| 15 | + |
| 16 | +`manually-static` empowers you to: |
| 17 | + |
| 18 | +- Opt-in to manual `'static` management: Take control when the compiler's strictness becomes a hurdle. |
| 19 | + |
| 20 | +- Catch errors early: Leverage `debug_assertions` to detect use-after-free scenarios or other incorrect |
| 21 | + dereferencing before they become hard-to-debug runtime crashes in production. |
| 22 | + |
| 23 | +- Simplify complex lifetime annotations: Reduce boilerplate and make your code more readable in scenarios |
| 24 | + where `'static` is implicitly guaranteed. |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +First, add `manually-static` to your `Cargo.toml`: |
| 29 | + |
| 30 | +```toml |
| 31 | +[dependencies] |
| 32 | +manually-static = "1.0.1" # Or the latest version |
| 33 | +``` |
| 34 | + |
| 35 | +## Threading Example (Illustrating `'static` need) |
| 36 | + |
| 37 | +```rust |
| 38 | +use manually_static::ManuallyStatic; |
| 39 | +use std::thread; |
| 40 | +use std::time::Duration; |
| 41 | + |
| 42 | +struct AppConfig { |
| 43 | + version: String, |
| 44 | +} |
| 45 | + |
| 46 | +fn main() { |
| 47 | + let config = ManuallyStatic::new(AppConfig { |
| 48 | + version: String::from("1.0.0"), |
| 49 | + }); |
| 50 | + |
| 51 | + // Get a 'static reference to the config. |
| 52 | + // This is where ManuallyStatic shines, allowing us to pass |
| 53 | + // a reference that the compiler would normally complain about |
| 54 | + // without complex ownership transfers or Arc for simple reads. |
| 55 | + let config_ref = config.get_ref(); |
| 56 | + |
| 57 | + let handle = thread::spawn(move || { |
| 58 | + // In this thread, we can safely access the config via the 'static reference. |
| 59 | + // In debug builds, if `config` (the original ManuallyStatic) was dropped |
| 60 | + // before this thread accessed it, it would panic. |
| 61 | + |
| 62 | + thread::sleep(Duration::from_millis(100)); // Simulate some work |
| 63 | + |
| 64 | + println!("Thread: App Version: {}", config_ref.version); |
| 65 | + }); |
| 66 | + |
| 67 | + handle.join().unwrap(); |
| 68 | + |
| 69 | + // config is dropped here after the thread has finished |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Example with allocating the data on the heap |
| 74 | + |
| 75 | +```rust |
| 76 | +use manually_static::ManuallyStaticPtr; |
| 77 | +use std::sync::Mutex; |
| 78 | +use std::array; |
| 79 | + |
| 80 | +const N: usize = 10280; |
| 81 | +const PAR: usize = 16; |
| 82 | + |
| 83 | +#[allow(dead_code, reason = "It is an example.")] |
| 84 | +struct Pool(Mutex<([Vec<u8>; N], usize)>); |
| 85 | + |
| 86 | +fn main() { |
| 87 | + let pool = ManuallyStaticPtr::new(Pool(Mutex::new((array::from_fn(|_| Vec::new()), 0)))); |
| 88 | + let mut joins = Vec::with_capacity(PAR); |
| 89 | + |
| 90 | + for _ in 0..PAR { |
| 91 | + #[allow(unused_variables, reason = "It is an example.")] |
| 92 | + let pool = pool.clone(); |
| 93 | + |
| 94 | + joins.push(std::thread::spawn(move || { |
| 95 | + /* ... do some work ... */ |
| 96 | + })); |
| 97 | + } |
| 98 | + |
| 99 | + for join in joins { |
| 100 | + join.join().unwrap(); |
| 101 | + } |
| 102 | + |
| 103 | + unsafe { pool.free(); } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## ⚠️ Important Considerations |
| 108 | + |
| 109 | +- `unsafe` under the hood: While `manually-static` provides debug-time checks, |
| 110 | + the underlying mechanism involves raw pointers. |
| 111 | + In release builds, these checks are absent, |
| 112 | + and misusing `ManuallyStaticRef` after the original `ManuallyStatic` has been dropped |
| 113 | + will lead to undefined behavior (UB). |
| 114 | +- Use responsibly: This crate is intended for specific scenarios where you have a strong, |
| 115 | + provable-by-logic guarantee about the lifetime of your data, |
| 116 | + but the compiler's static analysis cannot infer it. |
| 117 | + Avoid using it as a general workaround for lifetime errors without fully understanding the implications. |
| 118 | + |
| 119 | +`manually-static` is your trusty companion for those tricky `'static` lifetime puzzles, |
| 120 | +offering a powerful blend of flexibility and debug-time safety! |
0 commit comments