zone-alloc-strong-handle-derive

Crates.iozone-alloc-strong-handle-derive
lib.rszone-alloc-strong-handle-derive
version0.1.2
sourcesrc
created_at2023-09-18 04:45:27.144144
updated_at2023-11-22 06:30:55.225276
descriptionProcedural macro for zone-alloc StrongHandle types.
homepage
repository
max_upload_size
id975535
size10,007
Jackson Nestelroad (jackson-nestelroad)

documentation

https://docs.rs/zone_alloc_strong_handle_derive

README

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");
}
Commit count: 0

cargo fmt