#ownership #cell #lifetime #borrowing #aliasing

lending-cell

Like a cell, but make lifetimes dynamic instead of ownership

3 unstable releases

0.1.1 Jul 14, 2022
0.1.0 Jul 9, 2022
0.0.1 Jul 9, 2022

#8 in #aliasing

Download history 7208/week @ 2024-07-20 6292/week @ 2024-07-27 5193/week @ 2024-08-03 5039/week @ 2024-08-10 6751/week @ 2024-08-17 2210/week @ 2024-08-24 459/week @ 2024-08-31 181/week @ 2024-09-07 166/week @ 2024-09-14 210/week @ 2024-09-21 116/week @ 2024-09-28 187/week @ 2024-10-05 136/week @ 2024-10-12 89/week @ 2024-10-19 139/week @ 2024-10-26 168/week @ 2024-11-02

546 downloads per month
Used in sonnerie

BSD-2-Clause

9KB
64 lines

GitHub license Crates.io docs

LendingCell is a mutable container that allows you to get an owned reference to the same object. When the owned reference is dropped, ownership returns to the original container.

As opposed to a std::cell::Cell which moves Rust's ownership rules to runtime, a LendingCell moves Rust's lifetime rules to runtime.

The value of a LendingCell is present at construction-time, but you can convert it to a BorrowedCell<T> by calling LendingCell::to_borrowed. While that BorrowedCell lives (that is, until it is dropped), calling LendingCell::try_get will return None. The BorrowedCell has exclusive access to your type, as though you have a directly owned instance of the T, and so it is Send as long as T is Send. At last, when you drop the BorrowedCell, the T is returned to the LendingCell.

If you drop your LendingCell before dropping the BorrowedCell, then the T is dropped at the time you drop the BorrowedCell.

The invariants that Rust's memory safety rules enforce, like the single-mutable reference rule, are therefor partially ensured at compile time (you can't get two mutable references to the T), but also partially at runtime (while the BorrowedCell is active, the LendingCell behaves as though it is an Option containing None.

Example

let mut lender = LendingCell::new("borrowed");
let borrowed = lender.to_borrowed();
assert!(lender.try_get().is_none()); // `lender` is empty because it was borrowed
assert_eq!(*borrowed, "borrowed"); // it's certain that `borrowed` is valid while in scope
drop(borrowed); // dropping `borrowed` returns its value to `lender`
assert!(lender.try_get().is_some());

No runtime deps