#pattern #options #unification

option-cell

OptionCell: OnceCell but derivable from Option

1 unstable release

0.1.0 Sep 10, 2023

#2478 in Rust patterns

Download history 1/week @ 2024-12-11 3/week @ 2025-02-05 322/week @ 2025-03-05 1176/week @ 2025-03-12 950/week @ 2025-03-19

2,448 downloads per month

MIT/Apache

11KB
172 lines

OptionCell: OnceCell but derivable from Option

This library provides an equivalent of OnceCell, but it guarantees layout compatibility with Option<T>, providing additional transmute helpers.

Known use-cases

  • Implementing the unification algorithm without exposing the interior mutability to the user or unnecessarily cloning the value.

Usage

cargo add option-cell
use option_cell::OptionCell;

let mut options = vec![None, None];
let cells = OptionCell::from_mut_slice(&mut options);
cells[0].set(1).unwrap();

Development

Check with MIRI:

cargo +nightly miri test

lib.rs:

OptionCell: OnceCell but derivable from Option

This library provides an equivalent of OnceCell, but it guarantees layout compatibility with Option<T>, providing additional transmute helpers.

Known use-cases

  • Implementing the unification algorithm without exposing the interior mutability to the user or unnecessarily cloning the value.

Usage

cargo add option-cell
use option_cell::OptionCell;

let mut options = vec![None, None];
let cells = OptionCell::from_mut_slice(&mut options);
cells[0].set(1).unwrap();

No runtime deps