SlotMap IDs with types, sub-typing?
⚓ Rust 📅 2025-06-22 👤 surdeus 👁️ 14I'm thinking about how to model a document with a slotmaps. My documents are sort of like HTML, but simpler. They have blocks, spans, images, lists, etc. If I do something like this:
new_key_type! { pub struct NodeId; }
struct Document {
// every document node has data here
nodes: SlotMap<NodeId, NodeBase>,
// only spans have data here
spans: SecondaryMap<NodeId, SpanData>,
// only images have data here
images: SecondaryMap<NodeId, ImageData>,
...
}
The NodeId is a loose type. I'd like to have something like ImageId or SpanId for when I know I'm dealing with that specific sub-type of node. But I don't see anything in the slotmap docs about how to handle this. It just shows SlotMaps and SecondaryMaps with a single key type.
I was thinking something like...
enum NodeId {
Span(SpanId),
Block(BlockId),
Image(ImageId),
...
}
But then I have no idea how to connect that to the SlotMap and SecondaryMap types.
6 posts - 2 participants
🏷️ rust_feed