Expand description

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.

Why

A general use for this crate is when you need a reference to a variable, but you’re not able to use a lifetime. For example, Rust’s Iterator type doesn’t allow a lifetime on each Item that’s independent of the container you’re iterating over which means that Rust only allows you to iterate over a container with a lifetime exceeding your iterator, or to consume the container. What if instead you had to modify your

Structs

The container that ensures you have borrowed the LendingCell.

A container that allows borrowing without lifetimes.