lease-rs 0.1.8

Zero-cost ownership leasing for rust. Temporarily transfer full ownership across .await, threads, scopes, and closures with perfect safety and drop semantics
Documentation

Lease Rs

A std-ish primitive for temporary ownership transfer in Rust.

Crates.io Documentation License

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.

Installation

[dependencies]
lease-rs = "0.1"

For no_std environments (embedded, WASM, etc.):

lease-rs = { version = "0.1", default-features = false }

Quick Start

use lease_rs::lease_async_mut;

async fn example() {
    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"));
}

Key Features

  • 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

Quality Assurance

  • 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

Performance

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 (require T: Clone)

License

This project is licensed under either of

at your option.