2 releases

0.1.1 Apr 14, 2024
0.1.0 Apr 14, 2024

#1057 in Data structures

Download history 185/week @ 2024-08-30 105/week @ 2024-09-06 76/week @ 2024-09-13 90/week @ 2024-09-20 85/week @ 2024-09-27 188/week @ 2024-10-04 198/week @ 2024-10-11 161/week @ 2024-10-18 213/week @ 2024-10-25 151/week @ 2024-11-01 174/week @ 2024-11-08 223/week @ 2024-11-15 216/week @ 2024-11-22 210/week @ 2024-11-29 258/week @ 2024-12-06 82/week @ 2024-12-13

815 downloads per month
Used in 2 crates (via 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