-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
Add try_zeroed_vec
and zeroed_vec
#117
Conversation
I believe vec's allocation strategy is unspecified, so using from_raw_parts on something vec didn't allocate itself is questionable. I'd do let mut v = Vec::new();
v.try_reserve(length)?;
// somehow zero the vec, maybe make a zeroed item with T::zeroed() and write it to all items? or use `resize_with(T::zeroed)`
Ok(v) |
Sounds like a bug in vecs documentation. |
@@ -131,7 +151,7 @@ pub fn try_zeroed_slice_box<T: Zeroable>( | |||
let slice = | |||
unsafe { core::slice::from_raw_parts_mut(ptr as *mut T, length) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be core::ptr::slice_from_raw_parts_mut()
to avoid creating an intermediate reference (also also reduces the amount of unsafe by one line).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a possible change, but in this case the intermediate reference is fine, because the allocation is initialized anyway.
I think |
try_zeroed_vec
and zeroed_vec
, adjust impl of try_zeroed_slice_box
try_zeroed_vec
and zeroed_vec
@Lokathor I think this is a pretty easy change with the new strategy. Thoughts/hesitations? |
* add `try_zeroed_vec` and `zeroed_vec`, adjust impl of `try_zeroed_slice_box` * go back to returning an error rather than calling handle_alloc_error * use boxed slice .into_vec instead :)
So, I believe this is sound as it essentially exactly mimics the internal implementation of
Vec::with_capacity
except usingalloc_zeroed
instead. However,Vec::from_raw_parts_mut
is scary so would like to get someone else to review it and make sure I'm not going crazy.