1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// Copyright 2014 Christopher Schröder, Johannes Köster.
// Licensed under the MIT license (http://opensource.org/licenses/MIT)
// This file may not be copied, modified, or distributed
// except according to those terms.
//! Module with utility code.
use std::ffi;
use std::path::Path;
use std::ptr;
/// Copies data from `src` to `dst`
/// TODO remove once stable in standard library.
///
/// 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,
format!("dst len {} < src len {}", dst.len(), src.len())
);
// `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);
}
}
pub fn path_to_cstring<P: AsRef<Path>>(path: &P) -> Option<ffi::CString> {
path.as_ref()
.to_str()
.and_then(|p| ffi::CString::new(p).ok())
}