Skip to main content

Crate lien

Crate lien 

Source
Expand description

Scoped lending of borrowed references as Send-able smart pointers.

let mut greeting = String::from("hello ");

{
    let scope = lien::scope!();
    let mut g = scope.lend_mut(&mut greeting);

    thread::spawn(move || {
        g.push_str("beautiful ");
    });
}

greeting.push_str("world");
assert_eq!(greeting, "hello beautiful world");

A lien is “a right to take possession of a debtor’s property as security until a debt or duty is discharged.”

Similarly, a Lien represents a borrow of something from a Scope. Like a static borrow with & or &mut, this forces the Scope to outlive any Liens made from it, but like an Arc, Lien carries no lifetime (it’s atomically reference-counted at runtime), is thread-safe, and can be freely cloned.

§Smart pointers

  • Lien: a bare scope token.
  • Ref: a sendable shared reference (like &T).
  • RefMut: a sendable exclusive reference (like &mut T).

Both Ref and RefMut support sub-borrowing through Ref::map / RefMut::map, which lets you re-lend fields against the original scope.

§#[no_std]

Disable the std feature:

[dependencies]
lien = { version = "0.1", default-features = false }

Macros§

scope
Creates a Scope.

Structs§

Lien
A claim that holds a Scope open.
Ref
A sendable shared reference backed by a Lien.
RefMut
A sendable exclusive reference backed by a Lien.
Rehypothecator
A helper for Ref::map and RefMut::map that re-lends from existing Refs or RefMuts.
Scope
A guard for the current scope to lend resources as long-lived borrows.