4 releases (2 breaking)
0.4.0 | Jan 22, 2025 |
---|---|
0.3.0 | Jan 21, 2025 |
0.2.1 | Jan 21, 2025 |
0.2.0 | Jan 21, 2025 |
0.1.0 |
|
#2298 in Rust patterns
192 downloads per month
12KB
208 lines
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.
let channel = Rc::new(BorrowChannel::<&i32, _>::new_unsync());
let mut_channel = Rc::new(BorrowChannel::<&mut i32, _>::new_unsync());
let reads_a = || {
assert_eq!(42, *channel.borrow().get_mut());
};
let writes_a = || *mut_channel.borrow().get_mut() = 42;
let mut a = 0;
mut_channel.lend(&mut a, writes_a);
channel.lend(&mut a, reads_a);
Dependencies
~71KB