A std-ish primitive for temporary ownership transfer in Rust.
This crate provides a comprehensive solution for the fundamental problem of temporarily transferring ownership of values across scopes, closures, and async boundaries. It solves the "cannot borrow across .await" problem and enables scoped mutation patterns that are otherwise impossible in safe Rust.
[dependencies]
lease-rs = "0.1"For no_std environments (embedded, WASM, etc.):
lease-rs = { version = "0.1", default-features = false }# #[cfg(feature = "std")]
# async fn example() {
use lease_rs::lease_async_mut;
let mut data = vec![1, 2, 3];
let result = lease_async_mut(&mut data, |mut owned| async move {
// You now have full ownership of the data across .await points
tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
owned.push(4);
(owned, Ok("success"))
}).await;
assert_eq!(data, [1, 2, 3, 4]);
assert_eq!(result, Ok("success"));
# }- Zero-overhead by default - Operations that can be zero-cost are zero-cost
- Memory safety first - Never compromise Rust's safety guarantees
- Async cancellation safety - Automatic restoration on tokio::select! cancellation
- Explicit trade-offs - All performance and safety trade-offs are visible to users
- Complete API - 11 functions covering all common use cases
- 62 unit & doc tests - 100% coverage
- 25M+ fuzz executions - Zero crashes found
- Memory safety verified - Address sanitizer clean
- CI/CD pipeline - Automated testing on every PR
Most functions are zero-cost. Only async mutable operations with cancellation safety have runtime cost:
- Zero-cost (9/11 functions): All owned variants and sync mutable variants
- Non-zero-cost (2/11 functions):
lease_async_mut,try_lease_async_mut(requireT: Clone)
This project is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.