Are there any ways to get the length encoded in a raw fat pointer on stable Rust? (len
exists but is unstable.)
Hmm. Funny that we have this but not the opposite. Another also-unstable way is to_raw_parts
.
You could write your own:
fn len<T>(ptr: *const [T]) -> usize {
let ptr: *const [()] = ptr as _;
// SAFETY: There is no aliasing as () is zero-sized
let slice: &[()] = unsafe { &*ptr };
slice.len()
}
1 Like
Thanks, that works great!
While we're on the subject, do you know of a stable way to emulate size_of_val_raw
(assuming we don't know that our raw pointer is specifically a DST - could be a trait object, etc)?
A trait object is a DST.
But you mean, you don't know if it's Sized
, a trait object, a slice, or something else? I'm not thinking of a way to pull that off on stable, no.
Yeah, I should have said "custom DST" or something. Just something whose trailing field isn't necessarily a slice.
Oh well