Pointer metadata in vtable

Aren't there any plans to store pointer metadata in a vtable? Because &str cannot be passed as &dyn Trait, you need &&str.

In the case of &str, that metadata is the length of the string, so where exactly would that be stored? It can't be in the vtable since you can't have a different one for every possible dynamic length of str.


You could store a pointer to a length getting function in the vtable though, I think.

Edit: actually, never mind. Since the length is stored in the fat pointer, that wouldn't work. You would need a double fat pointer (3 words) instead.

I’m not sure if this applies to string slices, but I believe that VTables already store the size and alignment of the pointed-to type.

So far, pointer metadata is only used for DSTs, and VTables already cover that.


That's exactly the problem: the vtable can't store the length of a slice or string, because it's dynamic and the content of a vtable is static. You'd have to create a different vtable for every possible usize, so you have a different vtable for a slice of length 0, a slice of length 1, a slice of length 2, etc.

There are languages that have more complex implementations that can do this (for example, having a designated location in the data or pointer for the size, having a vtable function call, etc), but not Rust's very simple DST concept.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.