2 releases

0.1.1 Apr 14, 2024
0.1.0 Apr 14, 2024

#3 in #mutably

Download history 208/week @ 2024-04-13 9/week @ 2024-04-20

217 downloads per month
Used in connector_arrow

MIT license

6KB

Parent and Child cell

A cell of a parent and a child, which is created by mutably borrowing the parent. While the parent is in the cell, it cannot be accessed in any way. Provides mutable access to the child.

This is useful in a rare case when you need to store and move both parent and their child together.

Example

Basic usage:

struct Hello {
    world: i64,
}
let hello = Hello { world: 10 };

let mut pac = pac_cell::PacCell::new(hello, |h| &mut h.world);

let initial = pac.with_mut(|world| {
    let i = **world;
    **world = 12;
    i
});
assert_eq!(initial, 10);

let hello_again = pac.unwrap();
assert_eq!(hello_again.world, 12);

For a real-world-like example, see the crate tests.

Soundness

This crate has unsound functions (such as PacCell::new), which might might lead to undefined behavior when used "incorrectly".

See https://users.rust-lang.org/t/soundness-of-pac-cell-library/108598/4

No runtime deps