16 releases

new 0.6.0 Dec 7, 2024
0.6.0-alpha.5 Nov 16, 2024
0.5.6 Jul 18, 2024
0.5.0 Mar 28, 2024
0.4.3 Dec 7, 2023

#146 in Memory management

Download history 3013/week @ 2024-08-21 3764/week @ 2024-08-28 3286/week @ 2024-09-04 3251/week @ 2024-09-11 2929/week @ 2024-09-18 3297/week @ 2024-09-25 2550/week @ 2024-10-02 2921/week @ 2024-10-09 3400/week @ 2024-10-16 3486/week @ 2024-10-23 2829/week @ 2024-10-30 2359/week @ 2024-11-06 2673/week @ 2024-11-13 2881/week @ 2024-11-20 3078/week @ 2024-11-27 3769/week @ 2024-12-04

12,768 downloads per month
Used in 89 crates (12 directly)

MIT/Apache

66KB
1.5K SLoC

Generational Box

Generational Box is a runtime for Rust that allows any static type to implement Copy. It can be combined with a global runtime to create an ergonomic state solution like dioxus-signals. This crate doesn't have any unsafe code.

Three main types manage state in Generational Box:

  • Store: Handles recycling generational boxes that have been dropped. Your application should have one store or one store per thread.
  • Owner: Handles dropping generational boxes. The owner acts like a runtime lifetime guard. Any states that you create with an owner will be dropped when that owner is dropped.
  • GenerationalBox: The core Copy state type. The generational box will be dropped when the owner is dropped.

Example:

use generational_box::{UnsyncStorage, AnyStorage};

// Create an owner for some state for a scope
let owner = UnsyncStorage::owner();

// Create some non-copy data, move it into a owner, and work with copy data
let data: String = "hello world".to_string();
let key = owner.insert(data);

// The generational box can be read from and written to like a RefCell
let value = key.read();
assert_eq!(*value, "hello world");

How it works

Internally, generational-box creates an arena of generational RefCells that are recycled when the owner is dropped. You can think of the cells as something like &'static RefCell<Box<dyn Any>> with a generational check to make recycling a cell easier to debug. Then GenerationalBoxes are Copy because the &'static pointer is Copy.

Dependencies

~0.7–5.5MB
~17K SLoC