18 releases

0.6.2 Jan 22, 2025
0.6.1 Dec 18, 2024
0.6.0-alpha.5 Nov 16, 2024
0.5.6 Jul 18, 2024
0.4.3 Dec 7, 2023

#122 in Memory management

Download history 5485/week @ 2024-12-08 6068/week @ 2024-12-15 3215/week @ 2024-12-22 2828/week @ 2024-12-29 5219/week @ 2025-01-05 5668/week @ 2025-01-12 6956/week @ 2025-01-19 5815/week @ 2025-01-26 6169/week @ 2025-02-02 7337/week @ 2025-02-09 6281/week @ 2025-02-16 6946/week @ 2025-02-23 6165/week @ 2025-03-02 7415/week @ 2025-03-09 7036/week @ 2025-03-16 6239/week @ 2025-03-23

27,551 downloads per month
Used in 121 crates (13 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