-#[allow(unconditional_panic)]
-const fn illegal_null_in_string() {
- [][0]
-}
-
#[doc(hidden)]
pub const fn validate_cstr_contents(bytes: &[u8]) {
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'\0' {
- illegal_null_in_string();
+ panic!("illegal null byte in string");
}
i += 1;
}
$crate::helpers::validate_cstr_contents($s.as_bytes());
#[allow(unused_unsafe)]
unsafe {
- std::mem::transmute::<_, &std::ffi::CStr>(concat!($s, "\0"))
+ std::ffi::CStr::from_bytes_with_nul_unchecked(concat!($s, "\0").as_bytes())
}
}};
}
-#[allow(dead_code)]
-pub fn string_from_c_str(c_str: &[i8]) -> String {
- let s = unsafe { std::ffi::CStr::from_ptr(c_str.as_ptr()).to_bytes() };
- String::from_utf8_lossy(s).into_owned()
-}
-
#[cfg(test)]
mod tests {
use std::ffi::CStr;
-#[allow(unconditional_panic)]
-const fn illegal_null_in_string() {
- [][0]
-}
-
#[doc(hidden)]
pub const fn validate_cstr_contents(bytes: &[u8]) {
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'\0' {
- illegal_null_in_string();
+ panic!("illegal null byte in string");
}
i += 1;
}
$crate::helpers::validate_cstr_contents($s.as_bytes());
#[allow(unused_unsafe)]
unsafe {
- std::mem::transmute::<_, &std::ffi::CStr>(concat!($s, "\0"))
+ std::ffi::CStr::from_bytes_with_nul_unchecked(concat!($s, "\0").as_bytes())
}
}};
}
-#[allow(dead_code)]
-pub fn string_from_c_str(c_str: &[i8]) -> String {
- let s = unsafe { std::ffi::CStr::from_ptr(c_str.as_ptr()).to_bytes() };
- String::from_utf8_lossy(s).into_owned()
-}
-
#[cfg(test)]
mod tests {
use std::ffi::CStr;
panic!("out of memory")
}
-#[allow(unconditional_panic)]
-const fn illegal_null_in_string() {
- [][0]
-}
-
#[doc(hidden)]
pub const fn validate_cstr_contents(bytes: &[u8]) {
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'\0' {
- illegal_null_in_string();
+ panic!("illegal null byte in string");
}
i += 1;
}
$crate::validate_cstr_contents($s.as_bytes());
#[allow(unused_unsafe)]
unsafe {
- std::mem::transmute::<_, &std::ffi::CStr>(concat!($s, "\0"))
+ std::ffi::CStr::from_bytes_with_nul_unchecked(concat!($s, "\0").as_bytes())
}
}};
}
-#[allow(dead_code)]
-pub fn string_from_c_str(c_str: &[i8]) -> String {
- let s = unsafe { std::ffi::CStr::from_ptr(c_str.as_ptr()).to_bytes() };
- String::from_utf8_lossy(s).into_owned()
-}
-
/// Constructs a new box with uninitialized contents.
#[inline]
pub fn uninit_box<T>() -> Box<MaybeUninit<T>> {