Skip to content

Commit

Permalink
Add Index::DANGLING for two-phase initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed Jun 25, 2023
1 parent 0c8c018 commit 064608b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ pub struct Index {
}

impl Index {
/// Represents an `Index` that is unlikely to be in use. This is useful for
/// programs that want to do two-phase initialization in safe Rust. Avoid
/// using this value to represent the absence of an `Index`: prefer
/// `Option<Index>`.
pub const DANGLING: Self = Self {
slot: u32::MAX,
generation: Generation::DANGLING,
};

/// Convert this `Index` to an equivalent `u64` representation. Mostly
/// useful for passing to code outside of Rust.
#[allow(clippy::integer_arithmetic)]
Expand Down
5 changes: 5 additions & 0 deletions src/generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ use core::num::NonZeroU32;
pub(crate) struct Generation(NonZeroU32);

impl Generation {
/// Represents a generation that is unlikely to be used. This is useful for
/// programs that want to do two-phase initialization in safe Rust.
// This is safe because the maximum value of a u32 is not zero.
pub(crate) const DANGLING: Self = unsafe { Generation(NonZeroU32::new_unchecked(u32::MAX)) };

#[must_use]
pub(crate) fn first() -> Self {
// This is safe because 1 is not zero.
Expand Down

0 comments on commit 064608b

Please sign in to comment.