Skip to content

Commit

Permalink
Use <[T]>::clone_from_slice().
Browse files Browse the repository at this point in the history
Replace all occurences of `ring::polyfill::fill_from_slice` with
`<[T]>::clone_from_slice()`.
  • Loading branch information
pyfisch authored and briansmith committed Mar 8, 2016
1 parent 862882c commit 43af354
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 21 deletions.
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

0 comments on commit 43af354

Please sign in to comment.