Skip to content
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

FromBytes::insert_vec_from_bytes #2243

Open
kupiakos opened this issue Jan 18, 2025 · 0 comments
Open

FromBytes::insert_vec_from_bytes #2243

kupiakos opened this issue Jan 18, 2025 · 0 comments

Comments

@kupiakos
Copy link
Contributor

kupiakos commented Jan 18, 2025

FromBytes::insert_vec_from_bytes is like FromZeros::insert_vec_zeroed, but accepts bytes: &[u8] to copy instead of an additional: usize zeroed elements to insert. It's an optimization of:

if bytes.len().checked_rem(size_of::<T>()).unwrap_or(bytes.len()) != 0 {
    return Err(/* bytes.len() is not a multiple of the size of T */);
}
let additional = bytes.len().checked_div(size_of::<T>()).unwrap_or(0);
T::insert_vec_zeroed(&mut values, position, additional)?;
// The panic should be optimized away due to the above rem check.
values[position..position + additional].as_mut_bytes().copy_from_slice(bytes);
  • It should not have a zeroing memory step.
  • The capacity * size_of::<T>() of the returned Vec<T> should match the size of the input to ensure into_boxed_slice doesn't need to realloc.
  • bytes does not need to be aligned, same as read_from_bytes.
  • Note that if T is a ZST, then the only valid input for bytes is &[], which is a no-op.
  • extend_vec_from_bytes is also included in this design, with no position parameter.
  • FromBytes::new_vec_from_bytes(bytes: &[u8]) copies into a new Vec<Self>
  • Open question: should it panic when position > v.len() given FromZeros::insert_vec_zeroed both panics and returns Result #2242?
  • Open question: what's the best Err return type?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant