borrow_channel 0.2.1

A channel for borrows
Documentation

A channel for sending borrows. This is useful when you want to borrow data but cannot accept a reference to it via function arguments. For example, because a closure is called by code you do not control and which is unaware of the borrowing.

A reference can be inserted via lend. While the function passed to lend executes, the reference can be reborrowed using borrow. If lend attempts to return while the data is still borrowed, the program is aborted.

# use crate::borrow_channel::BorrowChannel;
# use std::rc::Rc;
let channel = Rc::new(BorrowChannel::<&i32, _>::new_unsync());
let mut_channel = Rc::new(BorrowChannel::<&mut i32, _>::new_unsync());

let reads_a = || {
channel.borrow().with(|a| {
assert_eq!(*a, 42);
})
};
let writes_a = || mut_channel.borrow().with(|b| *b = 42);

let mut a = 0;
mut_channel.lend(&mut a, writes_a);
channel.lend(&mut a, reads_a);