pac_cell

Crates.iopac_cell
lib.rspac_cell
version0.1.1
sourcesrc
created_at2024-04-14 17:31:36.337826
updated_at2024-04-14 17:36:14.523896
descriptionParent and child cell
homepagehttps://github.com/aljazerzen/pac_cell
repository
max_upload_size
id1208411
size12,833
Aljaž Mur Eržen (aljazerzen)

documentation

https://docs.rs/pac_cell

README

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

Commit count: 0

cargo fmt