#arena #allocation #memory #proc-macro

macro no-std zone-alloc-strong-handle-derive

Procedural macro for zone-alloc StrongHandle types

3 releases

0.1.2 Nov 22, 2023
0.1.1 Sep 18, 2023
0.1.0 Sep 18, 2023

#652 in Memory management

Download history 3/week @ 2024-02-19 19/week @ 2024-02-26 4/week @ 2024-03-11 48/week @ 2024-04-01

52 downloads per month
Used in battler

MIT license

8KB
93 lines

zone-alloc-strong-handle-derive

Latest Version

This crate provides a procedural macro for deriving the StrongHandle interface on simple wrappers around the Handle type when working with the StrongRegistry container in the zone-alloc crate.

[dependencies]
zone-alloc = "0.3"
zone-alloc-strong-handle-derive = "0.1"

Usage

This crate defines one procedural macro:

  • StrongHandle - Automatically derives the StrongHandle interface for simple wrappers around the Handle type.

Example

Linked List Nodes with Arena<T>

use zone_alloc::{
    Handle,
    StrongRegistry,
};
use zone_alloc_strong_handle_derive::StrongHandle;

#[derive(Clone, Copy, Debug, PartialEq, Eq, StrongHandle)]
struct NodeHandle(Handle);

#[derive(Debug, PartialEq, Eq)]
struct Node<T> {
    parent: Option<NodeHandle>,
    value: T,
}

impl<T> Node<T> {
    pub fn new(parent: Option<NodeHandle>, value: T) -> Self {
        Self { parent, value }
    }
}

fn main() {
    let registry = StrongRegistry::<NodeHandle, Node<&str>>::new();
    let root_handle = registry.register(Node::new(None, "first"));
    let handle = registry.register(Node::new(Some(root_handle), "second"));
    let handle = registry.register(Node::new(Some(handle), "third"));
    registry.get_mut(root_handle).unwrap().parent = Some(handle);

    let node = registry.get(handle).unwrap();
    assert_eq!(node.value, "third");
    let node = registry.get(node.parent.unwrap()).unwrap();
    assert_eq!(node.value, "second");
    let node = registry.get(node.parent.unwrap()).unwrap();
    assert_eq!(node.value, "first");
    let node = registry.get(node.parent.unwrap()).unwrap();
    assert_eq!(node.value, "third");
}

Dependencies

~3MB
~58K SLoC