Skip to content

Commit

Permalink
add debug impls
Browse files Browse the repository at this point in the history
  • Loading branch information
KGrewal1 committed Nov 22, 2024
1 parent af8f13f commit 582557d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 8 additions & 4 deletions tree_arena/src/tree_arena_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
//! will use an arena and unsafe code, but should have the exact same exported API as
//! this module.
#![expect(missing_debug_implementations, reason = "Deferred: Noisy")]

use super::NodeId;

use std::collections::HashMap;

#[derive(Debug)]
struct TreeNode<T> {
id: NodeId,
item: T,
Expand All @@ -32,7 +31,7 @@ struct TreeNode<T> {
/// will keep track of parent-child relationships, lets you efficiently find
/// an item anywhere in the tree hierarchy, and give you mutable access to this item
/// and its children.
#[derive(Default)]
#[derive(Debug, Default)]
pub struct TreeArena<T> {
roots: Vec<TreeNode<T>>,
parents_map: HashMap<NodeId, Option<NodeId>>,
Expand All @@ -42,6 +41,7 @@ pub struct TreeArena<T> {
///
/// When you borrow an item from a [`TreeArena`], it returns an `ArenaRef`.
/// You can iterate over its children to get access to child `ArenaRef` handles.
#[derive(Debug)]
pub struct ArenaRef<'arena, T> {
/// The parent of this node
pub parent_id: Option<NodeId>,
Expand All @@ -63,6 +63,7 @@ pub struct ArenaRef<'arena, T> {
/// and its children independently without invalidating the references.
///
/// You can iterate over its children to get access to child `ArenaMut` handles.
#[derive(Debug)]
pub struct ArenaMut<'arena, T> {
/// The parent of the node
pub parent_id: Option<NodeId>,
Expand All @@ -75,6 +76,7 @@ pub struct ArenaMut<'arena, T> {
/// A handle giving shared access to an arena item's children.
///
/// See [`ArenaRef`] for more information.
#[derive(Debug)]
pub struct ArenaRefChildren<'arena, T> {
id: Option<NodeId>,
children: &'arena Vec<TreeNode<T>>,
Expand All @@ -84,19 +86,21 @@ pub struct ArenaRefChildren<'arena, T> {
/// A handle giving mutable access to an arena item's children.
///
/// See [`ArenaMut`] for more information.
#[derive(Debug)]
pub struct ArenaMutChildren<'arena, T> {
id: Option<NodeId>,
children: &'arena mut Vec<TreeNode<T>>,
parents_map: ArenaMapMut<'arena>,
}

/// A shared reference to the parent father map
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub struct ArenaMapRef<'arena> {
parents_map: &'arena HashMap<NodeId, Option<NodeId>>,
}

/// A mutable reference to the parent father map
#[derive(Debug)]
pub struct ArenaMapMut<'arena> {
parents_map: &'arena mut HashMap<NodeId, Option<NodeId>>,
}
Expand Down
11 changes: 9 additions & 2 deletions tree_arena/src/tree_arena_unsafe.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// Copyright 2024 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

#![expect(missing_debug_implementations, reason = "Deferred: Noisy")]
#![allow(unsafe_code, reason = "Purpose is unsafe abstraction")]
use super::NodeId;

use std::cell::UnsafeCell;
use std::collections::HashMap;

#[derive(Debug)]
struct TreeNode<T> {
item: T,
children: Vec<NodeId>,
}

/// Mapping of data for the Tree Arena
#[derive(Debug)]
struct DataMap<T> {
/// The items in the tree
items: HashMap<NodeId, Box<UnsafeCell<TreeNode<T>>>>,
Expand All @@ -29,6 +30,7 @@ struct DataMap<T> {
/// will keep track of parent-child relationships, lets you efficiently find
/// an item anywhere in the tree hierarchy, and give you mutable access to this item
/// and its children.
#[derive(Debug)]
pub struct TreeArena<T> {
/// The items in the tree
data_map: DataMap<T>,
Expand All @@ -38,7 +40,7 @@ pub struct TreeArena<T> {
///
/// When you borrow an item from a [`TreeArena`], it returns an [`ArenaRef`].
/// You can access it children to get access to child [`ArenaRef`] handles.
#[derive(Debug)]
pub struct ArenaRef<'arena, T> {
/// Parent of the Node
pub parent_id: Option<NodeId>,
Expand All @@ -51,6 +53,7 @@ pub struct ArenaRef<'arena, T> {
/// A handle giving shared access to an arena item's children.
///
/// See [`ArenaRef`] for more information.
#[derive(Debug)]
pub struct ArenaRefChildren<'arena, T> {
/// The associated data arena
parent_arena: &'arena DataMap<T>,
Expand All @@ -74,7 +77,9 @@ impl<T> Clone for ArenaRefChildren<'_, T> {
*self
}
}

impl<Item> Copy for ArenaRefChildren<'_, Item> {}

/// A reference type giving mutable access to an arena item and its children.
///
/// When you borrow an item from a [`TreeArena`], it returns an `ArenaMut`.
Expand All @@ -87,6 +92,7 @@ impl<Item> Copy for ArenaRefChildren<'_, Item> {}
/// and its children independently without invalidating the references.
///
/// You can iterate over its children to get access to child `ArenaMut` handles.
#[derive(Debug)]
pub struct ArenaMut<'arena, T> {
/// Parent of the Node
pub parent_id: Option<NodeId>,
Expand All @@ -104,6 +110,7 @@ pub struct ArenaMut<'arena, T> {
/// As such if a [`std::mem::swap`] is used to swap the children of two trees,
/// each tree will still have the correct permissions. This also stores the roots, and so
/// that will also be in a consistent state
#[derive(Debug)]
pub struct ArenaMutChildren<'arena, T> {
/// The associated data arena
parent_arena: &'arena mut DataMap<T>,
Expand Down

0 comments on commit 582557d

Please sign in to comment.