mohrezaei/thincollections

Feature request: implement ThinSentinel for raw pointer values

Closed this issue · 6 comments

This would allow use of ThinMap<*const u8, *const u8>, for example.

I'll have a look at this over the next week, but in the meantime, I'll happily accept a PR.

I thought about this some more and I think I need a bit more info on your usecase. What's the point of constructing a ThinMap<*const u8, *const u8>? Are those pointers pointing to a plain u8 or is it some sort of buffer (e.g. null terminated string or whatever)? If they're just plain u8, what's the usecase for storing pointers to them? If they're pointing to buffers, then it's not clear to me that Eq and Hash would work properly.

The keys in a map (ThinMap or rust's stdlib HashMap) use == for equality. For raw pointers that means the memory location being pointed at. That would seem wrong for comparing buffers: the content is what should be Eq/Hashed.

If your intent is to construct a map of buffers with their contents properly compared and hashed, then you'll need a simple wrapper struct around the pointer:

struct RawBuffer {
    ptr: *const u8
}

and then implement ThinSentinel + Eq + Hash for RawBuffer used as the key. The value can be anything (including a *const u8). This struct is a zero cost abstraction and can be implemented to compare pointers or content.

That was just an example - my actual use case is for a hobby compiler I was building. I wanted to evaluate the performance of thincollections for a HashMap<*const ObjectString, Value>.

All strings are interned, so it's perfectly safe to be doing a pointer comparison.

Alright, I understand.

PR done.

Published as 0.5.4