extend-mut
extend-mut is a #![no_std] Rust crate that provides safe and unsafe
utilities to extend the lifetime of an exclusive mutable reference (&mut). It
includes both synchronous and asynchronous methods for achieving this, with a
focus on correctness and safety guarantees around mutable reference lifetimes.
Features
extend_mut: A synchronous function that safely extends the lifetime of a mutable reference using a sync closure. Note that you can still use this in async context, if you will call it on every poll, instead of on future creation.extend_mut_async: An asynchronous function that allows extending the lifetime of a mutable reference in anasynccontext. This function comes with important safety considerations.
Why Use extend-mut?
Rust's borrow checker enforces strict lifetime rules to ensure memory safety.
However, there are scenarios where you may need to work around lifetime
limitations in a controlled way. extend-mut provides a way to extend the
lifetime of mutable references safely and correctly, without introducing
undefined behavior.
A commom use case is crating a temporary &'static mut reference to send
somewhere, give execution control to other function that expects &'static mut,
and then take back the control, take back &'static mut and recover original lifetime.
Crate Attributes
#![no_std]support: This crate is compatible with#![no_std]environments, making it suitable for embedded and constrained systems.
Usage
Add extend-mut to your Cargo.toml:
[]
= "0.1"
Synchronous Example
use extend_mut;
Safety Considerations
extend_mut is designed to be safe, while extend_mut_async is inherently
unsafe due to the lack of linear types in Rust. When using extend_mut_async,
ensure that the returned future is fully awaited before being dropped. Violating
this requirement will cause the process to abort.