Skip to content

Commit

Permalink
feat: add AugDict::cast_into and AugDict::from_parts
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Jun 13, 2024
1 parent 3655c23 commit 32467d0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/dict/aug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,36 @@ impl<K, A: Default, V> AugDict<K, A, V> {
_value: PhantomData,
}
}

/// Manually constructs the dictionaty from parts.
pub const fn from_parts(dict: Dict<K, (A, V)>, extra: A) -> Self {
Self {
dict,
extra,
_key: PhantomData,
_value: PhantomData,
}
}

/// Returns an underlying dictionary and the extra value.
pub fn into_parts(self) -> (Dict<K, (A, V)>, A) {
(self.dict, self.extra)
}

/// Converts into a dictionary with an equivalent value type.
#[inline]
pub fn cast_into<Q, T>(self) -> AugDict<Q, A, T>
where
Q: EquivalentRepr<K>,
(A, T): EquivalentRepr<(A, V)>,
{
AugDict {
dict: self.dict.cast_into(),
extra: self.extra,
_key: PhantomData,
_value: PhantomData,
}
}
}

impl<K: DictKey, A, V> AugDict<K, A, V> {
Expand Down Expand Up @@ -151,7 +181,8 @@ where
K: DictKey,
for<'a> A: Default + Load<'a>,
{
fn update_root_extra(&mut self) -> Result<(), Error> {
/// Recomputes the root extra value.
pub fn update_root_extra(&mut self) -> Result<(), Error> {
self.extra = match &self.dict.root {
Some(root) => {
let slice = &mut ok!(root.as_slice());
Expand Down
6 changes: 6 additions & 0 deletions src/dict/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ impl<const N: u16> RawDict<N> {
&self.0
}

/// Returns the underlying root cell of the dictionary.
#[inline]
pub fn into_root(self) -> Option<Cell> {
self.0
}

/// Loads a non-empty dictionary from a root cell.
#[inline]
pub fn load_from_root_ext(
Expand Down
6 changes: 6 additions & 0 deletions src/dict/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ impl<K, V> Dict<K, V> {
&self.root
}

/// Returns the underlying root cell of the dictionary.
#[inline]
pub fn into_root(self) -> Option<Cell> {
self.root
}

/// Converts into a dictionary with an equivalent value type.
#[inline]
pub fn cast_into<Q, T>(self) -> Dict<Q, T>
Expand Down

0 comments on commit 32467d0

Please sign in to comment.