Skip to content

Add as_slice methods to DenseSlotMap.#140

Merged
orlp merged 1 commit intomasterfrom
as-slice
Dec 5, 2025
Merged

Add as_slice methods to DenseSlotMap.#140
orlp merged 1 commit intomasterfrom
as-slice

Conversation

@orlp
Copy link
Owner

@orlp orlp commented Dec 5, 2025

Supersedes #134.

Fixes #131.

Credit to @Swiiz for the examples.

@jagprog5
Copy link

jagprog5 commented Dec 6, 2025

@orlp this is missing critical functionality. see my MR. sure, I understand now allowing mutable slice to keys, that's fine. but there should still be both [ immutable keys slice, and mutable values slice] provided as output together.

this is required by ets. current exposed API makes it impossible to satisfy borrow checker - need explicit disjoint borrow

@orlp
Copy link
Owner Author

orlp commented Dec 6, 2025

Hmm I see. I also just noticed that in my earlier discussion I wanted to add as_slices and as_mut_slices.

I think I'll add these two and probably remove the other *_as_slice variants in 2.0 again.

@orlp
Copy link
Owner Author

orlp commented Dec 6, 2025

@jagprog5 Fixed in 1.1.1.

Copy link

@melody-rs melody-rs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can see this is missing a way to go from key -> value index?

@jagprog5
Copy link

jagprog5 commented Dec 7, 2025

From what I can see this is missing a way to go from key -> value index?

@melody-rs not sure what you mean; the keys and values are in the same arbitrary order so key[i] goes to value[i]

@melody-rs
Copy link

melody-rs commented Dec 7, 2025

From what I can see this is missing a way to go from key -> value index?

@melody-rs not sure what you mean; the keys and values are in the same arbitrary order so key[i] goes to value

Sorry, could've explained better!
I meant that there's no way to do:

use slotmap::*;
let mut sm = DenseSlotMap::new();
let key = sm.insert(0i32);
let values = sm.values_as_slice();
let value_index = /* get the index of the value in the slice */
let value = values[value_index];
assert_eq!(value, sm[key]);

Being able to get the index is useful for copying a DenseSlotMap into a GPU buffer since you can use the index associated with a key to look up something in a shader

@jagprog5
Copy link

jagprog5 commented Dec 7, 2025

@melody-rs I don't think this is possible since the index isn't stable; it will re-order to compact to contiguous memory if elements are deleted.

If you're using the dense slot map and only inserting, then you could use an incrementing counter? double check this. So first insertion is at slice index 0, next one at index 1, ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide ways to get a slice to DenseSlotMap's values?

3 participants