-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Box<[T]>::into_raw
is useless
#36284
Comments
This is how you construct a fat slice box out of pointer and capacity:
You should be able to pass |
@nagisa |
@ollie27 then we have a larger problem of invalid implementation of |
See #29847. |
I think the problem is with documentation. shrink_to_fit from the documentation isn't guaranteed to make |
I think boxed slice is more appropriate here. We don't need to give more guarantees to |
Previous implementation assumed `Vec::shrink_to_fit` would always make Vec's length equal to its capacity. However, there was no such guarantee and undefined behaviour could be triggered. Currently Rust implementation assume this as well. A proper fix would use boxed slices so we don't need to store capacity. We may want to revisit this as the following Rust issue gets more attention: rust-lang/rust#36284
Previous implementation assumed `Vec::shrink_to_fit` would always make Vec's length equal to its capacity. However, there was no such guarantee and undefined behaviour could be triggered. Currently Rust implementation assume this as well. A proper fix would use boxed slices so we don't need to store capacity. We may want to revisit this as the following Rust issue gets more attention: rust-lang/rust#36284
Hey guys, any update on this? Our FFI interfaces still feel weird taking a |
I'm not sure what update you're waiting on. The documentation certainly needs to be improved but as far as I'm aware you can do the following now with no problems: pub fn vec_to_ptr_len(v: Vec<u8>) -> (*mut u8, usize) {
let mut b = v.into_boxed_slice();
let ptr = b.as_mut_ptr();
let len = b.len();
std::mem::forget(b);
(ptr, len)
}
pub unsafe fn ptr_len_to_vec(ptr: *mut u8, len: usize) -> Vec<u8> {
Box::from_raw(std::slice::from_raw_parts_mut(ptr, len)).into_vec()
} |
I would like to close out this ticket. Here's how I understand it: What's left is basically @ustulation's comment above, that is, @rust-lang/libs, what do you want done here? |
I think that there is lacking clarity and it needs one hard look at usable size again (if that is ok)? Rust could gain some memory utilization there, removing clown shoes, without breaking any current Vec API. It requires looking at jemalloc's size bins and working out the relevant details. The theory is that all capacities in the same bin are equivalent, and usable size gives you the largest one in the allocation's current bin. With that in place, docs related to this can be clarified with confidence. |
Taking advantage of extra space given to us by the allocator seems like it falls under #29931, in which case it sounds to me like @steveklabnik is right, we should just remove the clause from The fact that |
That sounds good! |
In case anyone is willing to tackle this, the docs team wants to take this approach:
|
Hi there! I'm a student at Seneca College whose enrolled in an Open Source course taught by my professor and Mozillian, David Humphrey. I would love to tackle this bug for my course if it's still available and unassigned. |
That'd be great! Is @QuietMisdreavus 's post on what to do clear enough? |
@steveklabnik I would love to say yes and get on my way but I'm still a little unclear. Would I be removing documentation for |
Yup! To elaborate a little further, that means modifying these lines: Lines 510 to 516 in 94a82ad
The sentence about excess capacity should stay, but the second one and the link should be removed. |
Thank you so much @steveklabnik! I will have to prepare my local environment on my laptop and get to work. Once I'm done and ready to submit a PR, I will let you know! |
Resolves #36284 - vec.rs documentation Removed comments associated with `[into_vec]` being equivalent to `[shrink_to_fit]` as requested.
Thanks. |
Sorry about the horrible title, but I really don't know how to else "summarize" this problem.
I tried to get an answer for this several times on StackOverflow (after documentation proved useless) and now I'm coming here. Maybe it's just a matter of updating documentation to have guarantees of a behaviour that will continue to work on future versions of Rust (the main purpose of documentation in the first place) or maybe the error is me who failed to spot the useful part.
I need to expose a Rust library to C applications and I need to expose an array of size only know at runtime to a function which will own this data and later will call a Rust function to deallocate it. I have a
Vec<u8>
and I need to get this(*mut u8, usize)
tuple out of it. Later I need to get receive this same tuple and deallocate its data.One option is to use
mem::forget
+Vec::from_raw_parts
, but this is far from ideal asshrink_to_fit
isn't guaranteed to work and I'd also need to store capacity (the C library really shouldn't need to worry about that).The perfect option would be to use
Vec::into_boxed_slice
. However, it crashed when I tried to use the pointer for the first element (slice::as_mut_ptr
) toBox::from_raw
, as it should. And I cannot pass the pointer return fromBox::into_raw
to the C function, as the memory layout is unspecified and I don't have any guarantee it'll be a pointer to the first element.How do we fix this? Update documentation? Add some extra function to
Box<[T]>
likeBox<[T]>::from_raw_parts(ptr: *mut T, len: usize)
?The text was updated successfully, but these errors were encountered: