Cesar Eduardo Barros
9 years ago
2 changed files with 34 additions and 36 deletions
@ -1,42 +1,40 @@ |
|||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|||
// file at the top-level directory of the Rust distribution and at
|
|||
// http://rust-lang.org/COPYRIGHT.
|
|||
// Copyright 2015 blake2-rfc Developers
|
|||
//
|
|||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|||
// option. This file may not be copied, modified, or distributed
|
|||
// except according to those terms.
|
|||
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
|
|||
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
|
|||
// http://opensource.org/licenses/MIT>, at your option. This file may not be
|
|||
// copied, modified, or distributed except according to those terms.
|
|||
|
|||
//! A local copy of the unstable std::slice::bytes module.
|
|||
//! Operations on byte slices.
|
|||
|
|||
use std::ptr; |
|||
|
|||
/// A trait for operations on mutable `[u8]`s.
|
|||
pub trait MutableByteVector { |
|||
/// Sets all bytes of the receiver to the given value.
|
|||
fn set_memory(&mut self, value: u8); |
|||
/// Operations on byte slices.
|
|||
pub trait BytesExt { |
|||
/// Set all bytes of this slice to the same value.
|
|||
///
|
|||
/// Equivalent to C's memset().
|
|||
fn set_bytes(&mut self, value: u8); |
|||
|
|||
/// Copy all bytes from a source slice to the start of this slice.
|
|||
///
|
|||
/// Equivalent to C's memcpy().
|
|||
fn copy_bytes_from(&mut self, src: &[u8]); |
|||
} |
|||
|
|||
impl MutableByteVector for [u8] { |
|||
impl BytesExt for [u8] { |
|||
#[inline] |
|||
fn set_memory(&mut self, value: u8) { |
|||
unsafe { ptr::write_bytes(self.as_mut_ptr(), value, self.len()) }; |
|||
fn set_bytes(&mut self, value: u8) { |
|||
unsafe { |
|||
ptr::write_bytes(self.as_mut_ptr(), value, self.len()); |
|||
} |
|||
} |
|||
} |
|||
|
|||
/// Copies data from `src` to `dst`
|
|||
///
|
|||
/// Panics if the length of `dst` is less than the length of `src`.
|
|||
#[inline] |
|||
pub fn copy_memory(src: &[u8], dst: &mut [u8]) { |
|||
let len_src = src.len(); |
|||
assert!(dst.len() >= len_src); |
|||
// `dst` is unaliasable, so we know statically it doesn't overlap
|
|||
// with `src`.
|
|||
unsafe { |
|||
ptr::copy_nonoverlapping(src.as_ptr(), |
|||
dst.as_mut_ptr(), |
|||
len_src); |
|||
#[inline] |
|||
fn copy_bytes_from(&mut self, src: &[u8]) { |
|||
assert!(src.len() <= self.len()); |
|||
unsafe { |
|||
ptr::copy_nonoverlapping(src.as_ptr(), self.as_mut_ptr(), src.len()); |
|||
} |
|||
} |
|||
} |
|||
|
Loading…
Reference in new issue