summaryrefslogtreecommitdiffstats
path: root/third_party/rust/libloading-0.5.2/src/util.rs
blob: 650266e1df1a1b3063e6ed52f976a8d81b6d5992 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::ffi::{CStr, CString, NulError, FromBytesWithNulError};
use std::borrow::Cow;
use std::os::raw;

#[derive(Debug)]
pub struct NullError;

impl From<NulError> for NullError {
    fn from(_: NulError) -> NullError {
        NullError
    }
}

impl From<FromBytesWithNulError> for NullError {
    fn from(_: FromBytesWithNulError) -> NullError {
        NullError
    }
}

impl From<NullError> for ::std::io::Error {
    fn from(e: NullError) -> ::std::io::Error {
        ::std::io::Error::new(::std::io::ErrorKind::Other, format!("{}", e))
    }
}

impl ::std::error::Error for NullError {
    fn description(&self) -> &str { "non-final null byte found" }
}

impl ::std::fmt::Display for NullError {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "non-final null byte found")
    }
}

/// Checks for last byte and avoids allocating if it is zero.
///
/// Non-last null bytes still result in an error.
pub fn cstr_cow_from_bytes<'a>(slice: &'a [u8]) -> Result<Cow<'a, CStr>, NullError> {
    static ZERO: raw::c_char = 0;
    Ok(match slice.last() {
        // Slice out of 0 elements
        None => unsafe { Cow::Borrowed(CStr::from_ptr(&ZERO)) },
        // Slice with trailing 0
        Some(&0) => Cow::Borrowed(try!(CStr::from_bytes_with_nul(slice))),
        // Slice with no trailing 0
        Some(_) => Cow::Owned(try!(CString::new(slice))),
    })
}

#[inline]
pub fn ensure_compatible_types<T, E>() {
    #[cold]
    #[inline(never)]
    fn dopanic() {
        panic!("value of requested type cannot be dynamically loaded");
    }
    if ::std::mem::size_of::<T>() != ::std::mem::size_of::<E>() {
        dopanic()
    }
}