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

Use <[T]>::clone_from_slice(). #140

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/aead/chacha20_poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn chacha20_poly1305_update_old(state: &mut [u8; POLY1305_STATE_LEN],

/// Copies |key| into |ctx_buf|.
pub fn init(ctx_buf: &mut [u8], key: &[u8]) -> Result<(), ()> {
polyfill::slice::fill_from_slice(&mut ctx_buf[..key.len()], key);
ctx_buf[..key.len()].clone_from_slice(key);
Ok(())
}

Expand Down
14 changes: 5 additions & 9 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,17 @@ impl Context {
/// C analog: `EVP_DigestUpdate`
pub fn update(&mut self, data: &[u8]) {
if data.len() < self.algorithm.block_len - self.num_pending {
polyfill::slice::fill_from_slice(
&mut self.pending[self.num_pending..
(self.num_pending + data.len())],
data);
self.pending[self.num_pending..
(self.num_pending + data.len())].clone_from_slice(data);
self.num_pending += data.len();
return;
}

let mut remaining = data;
if self.num_pending > 0 {
let to_copy = self.algorithm.block_len - self.num_pending;
polyfill::slice::fill_from_slice(
&mut self.pending[self.num_pending..self.algorithm.block_len],
&data[..to_copy]);
self.pending[self.num_pending..self.algorithm.block_len]
.clone_from_slice(&data[..to_copy]);

unsafe {
(self.algorithm.block_data_order)(self.state.as_mut_ptr(),
Expand All @@ -132,8 +129,7 @@ impl Context {
.unwrap();
}
if num_to_save_for_later > 0 {
polyfill::slice::fill_from_slice(
&mut self.pending[..num_to_save_for_later],
self.pending[..num_to_save_for_later].clone_from_slice(
&remaining[(remaining.len() - num_to_save_for_later)..]);
self.num_pending = num_to_save_for_later;
}
Expand Down
11 changes: 0 additions & 11 deletions src/polyfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,6 @@ pub mod slice {
}
}

// https://github.com/rust-lang/rust/issues/27750
// https://internals.rust-lang.org/t/stabilizing-basic-functions-on-arrays-and-slices/2868
#[inline(always)]
pub fn fill_from_slice(dest: &mut [u8], src: &[u8]) {
assert_eq!(dest.len(), src.len());
unsafe {
core::ptr::copy_nonoverlapping(src.as_ptr(), dest.as_mut_ptr(),
src.len())
}
}

// https://internals.rust-lang.org/t/safe-trasnsmute-for-slices-e-g-u64-u32-particularly-simd-types/2871
#[inline(always)]
pub fn u64_as_u8<'a>(src: &'a [u64]) -> &'a [u8] {
Expand Down