Skip to content

BGR360/lender-loan-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lender-loan

Documentation

Lifetime-erased borrowing across threads.

This crate provides two types: Lender<'a, T> and Loan<T>. A Lender is constructed from a reference to some value, and it hands out Loans that can be sent to other threads. A Loan is like an Arc that points to data whose lifetime is tied to the Lender.

The key thing that makes this safe is that the lender blocks until all outstanding loans are dropped. Just as Mutex pushes Rust's aliasing guarantees from compile-time to run-time, Lender pushes Rust's lifetime guarantees from compile-time to run-time.

The tradeoff made here is that the lender may block for an indeterminate amount of time if loans are passed to other threads. However, this limitation also exists in the alternative solution, which is using scoped threads with explicit lifetimes all over the place. And this solution requires no lifetimes!

Example

use lender_loan::{Lender, Loan};

fn use_loan(loan: Loan<Vec<i32>>) {
    assert_eq!(*loan, vec![1, 2, 3]);
}

// Create a value that will be lent to other threads.
let mut value = vec![1, 2, 3];
Lender::with(&value, |lender: &Lender<'_, Vec<i32>>| {
    for _ in 0..100 {
        let loan = lender.lend();
        std::thread::spawn(move || use_loan(loan));
    }
});

// It is safe to modify the value again; `Lender::with` blocks until all loans are dropped.
value.push(4);

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Lifetime-erased borrowing across threads in Rust.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages