//! Well-typed indices into [`Arena`]s and [`UniqueArena`]s. //! //! This module defines [`Handle`] and related types. //! //! [`Arena`]: super::Arena //! [`UniqueArena`]: super::UniqueArena
use std::{cmp::Ordering, fmt, hash, marker::PhantomData};
/// An unique index in the arena array that a handle points to. /// The "non-max" part ensures that an `Option<Handle<T>>` has /// the same size and representation as `Handle<T>`. pubtype Index = crate::non_max_u32::NonMaxU32;
#[derive(Clone, Copy, Debug, thiserror::Error, PartialEq)] #[error("Handle {index} of {kind} is either not present, or inaccessible yet")] pubstruct BadHandle { pub kind: &'static str, pub index: usize,
}
/// A strongly typed reference to an arena item. /// /// A `Handle` value can be used as an index into an [`Arena`] or [`UniqueArena`]. /// /// [`Arena`]: super::Arena /// [`UniqueArena`]: super::UniqueArena #[cfg_attr(feature = "serialize", derive(serde::Serialize))] #[cfg_attr(feature = "deserialize", derive(serde::Deserialize))] #[cfg_attr(
any(feature = "serialize", feature = "deserialize"),
serde(transparent)
)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] pubstruct Handle<T> {
index: Index, #[cfg_attr(any(feature = "serialize", feature = "deserialize"), serde(skip))]
marker: PhantomData<T>,
}
/// Returns the index of this handle. pubconstfn index(self) -> usize { self.index.get() as usize
}
/// Convert a `usize` index into a `Handle<T>`. pub(super) fn from_usize(index: usize) -> Self { let handle_index = u32::try_from(index)
.ok()
.and_then(Index::new)
.expect("Failed to insert into arena. Handle overflows");
Handle::new(handle_index)
}
/// Convert a `usize` index into a `Handle<T>`, without range checks. pub(super) constunsafefn from_usize_unchecked(index: usize) -> Self {
Handle::new(Index::new_unchecked(index as u32))
}
/// Write this handle's index to `formatter`, preceded by `prefix`. pubfn write_prefixed(
&self,
formatter: &mut fmt::Formatter,
prefix: &'static str,
) -> fmt::Result {
formatter.write_str(prefix)?;
<usize as fmt::Display>::fmt(&self.index(), formatter)
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.19 Sekunden
(vorverarbeitet am 2026-06-19)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.