pub struct Owned<L: LeanRepr> { /* private fields */ }Expand description
A Lean value of the type named by L with one owner, which can move between threads and
is still updated in place.
The objects of the value keep the reference counts of one thread, like those of a
Local. That is sound because nothing else refers to them while the Owned is at rest:
it is made from a Rust value or from a Lean, it cannot be cloned or shared (Send, not
Sync), and Owned::with lends the value as a Local for the time of a closure only.
A Local that outlives the closure is detected when the closure returns, and what it
shares with the value is then marked as shared between threads, which costs the in-place
updates of those objects and nothing else.
use leanfran_runtime::{List, Local, Owned};
use std::sync::Mutex;
let numbers = Mutex::new(Owned::<List<u32>>::new(vec![1, 2, 3]));
std::thread::scope(|s| {
s.spawn(|| {
let length = numbers.lock().unwrap().with(|list: Local<List<u32>>| {
let length = list.get().len();
(list, length)
});
assert_eq!(length, 3);
});
});let owned = leanfran_runtime::Owned::<u8>::new(1);
std::thread::scope(|s| {
s.spawn(|| owned.get());
});Implementations§
Source§impl<L: LeanRepr> Owned<L>
impl<L: LeanRepr> Owned<L>
Sourcepub fn with<R>(&mut self, f: impl FnOnce(Local<L>) -> (Local<L>, R)) -> R
pub fn with<R>(&mut self, f: impl FnOnce(Local<L>) -> (Local<L>, R)) -> R
Lends the value to f as a Local and takes the Local that f returns as the new
value. Passing the Local by value to a function of the local module of a generated
module updates the value in place.
Every other Local made in f should be gone when f returns. If one is still alive
(kept in a captured variable or in the result), or if f returns a Local it got from
elsewhere, the new value is marked as shared between threads.
The handle that can be shared between threads.