-
Notifications
You must be signed in to change notification settings - Fork 385
Stable, Miri-friendly way to get length of a boxed slice that is partially mutably borrowed #4317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Shouldn't https://doc.rust-lang.org/nightly/std/primitive.pointer.html#method.len work for this? It has been stable for a few releases. |
Could you create a small self-contained example demonstrating this one not working? |
Yes, this was the exact one I was referring to with "(there fortunately is a method Thanks, I'll try creating a self-contained example. |
Yeah that's the "SB raw pointer casts do retags which is bad" issue again. :/ For funny reasons, what would work in your particular case is |
Oh, apparently! So, because this thread was about finding a workaround, turns out that adding a helper that does conversion through a
Thank you! |
Okay, great. :) Let's close the issue then -- the underlying problem about cast-to-raw behaving weirdly in SB is already tracked elsewhere. |
A follow-up from rust-lang/rust#129090 (comment)
I'm writing a custom memory allocator/object pool. I have a bunch of
Box<[Thing]>
chunks, where some of theThing
s are mutably borrowed as&mut Thing
for a limited lifetime. During that lifetime, I need to access the slicelen
, but I don't need to touch its contents. I figured this would be okay to do, since thelen
is stored in the fat pointer, not in the slice itself. However naively doingchunk.len()
or(&*chunk).len()
triggers Miri UB warning. The problem seems to be that the dereference invalidates an earlierUnique
tag within the slice. The offset0x0..0x60
below corresponds to the whole memory span of the slice.I managed to get it to work with
Box::as_ptr(&chunk).len()
(there fortunately is a method<*const [T]>::len()
), but this is not ideal asBox::as_ptr
is still unstable.(&raw const *chunk).len()
doesn't seem to work, as it retags the whole slice likechunk.len()
.Are there any stable, Miri-friendly workarounds for getting the length, or more generally getting a pointer to a slice behind a box without retagging the contents of the slice?
The text was updated successfully, but these errors were encountered: