Expand description
§Typeslot
Assigns each type a unique usize index at startup, with optional group compartmentalization.
§Usage
use typeslot::prelude::*;
// Define group markers.
struct ElementGroup;
struct ResourceGroup;
// Derive `TypeSlot` on your types.
#[derive(TypeSlot)]
#[slot(ElementGroup)]
struct Horizontal;
#[derive(TypeSlot)]
#[slot(ElementGroup)]
struct Vertical;
#[derive(TypeSlot)]
#[slot(ResourceGroup)]
struct Health;
// A type can belong to multiple groups.
#[derive(TypeSlot)]
#[slot(ElementGroup, ResourceGroup)]
struct Label;
// Call `init_slot` once per group before accessing slots.
// It returns the number of slots assigned in the group.
let element_count = init_slot::<ElementGroup>();
let resource_count = init_slot::<ResourceGroup>();
assert_eq!(element_count, 3); // Horizontal, Vertical, Label
assert_eq!(resource_count, 2); // Health, Label
// Use SlotGroup for ergonomic access without repeating the group type.
let elements = SlotGroup::<ElementGroup>::new();
let resources = SlotGroup::<ResourceGroup>::new();
println!("{}", elements.get::<Horizontal>());
println!("{}", resources.get::<Health>());
// Or use the free functions directly.
println!("{}", slot::<Horizontal, ElementGroup>());
println!("{}", slot::<Health, ResourceGroup>());§Join the community!
You can join us on the Voxell discord server.
§License
typeslot is dual-licensed under either:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
This means you can select the license you prefer! This dual-licensing approach is the de-facto standard in the Rust ecosystem and there are very good reasons to include both.
Modules§
Structs§
- Atomic
Slot - A write-once slot for a
usizeindex. - Slot
Group - A zero-sized handle for querying slot indices within group
G. - Type
Slot Entry - Registration entry for a type that implements
TypeSlot<G>within groupG.
Traits§
- Type
Slot - A type with a statically assigned slot index within group
G.
Functions§
- init_
slot - Assigns a unique index to each type registered in
group
G. - slot
- Returns the slot index of type
Tin groupG. - try_
slot - Returns the slot index of type
Tin groupG, orNoneifinit_slothas not been called forGyet.