From: Joshua Simmons Date: Thu, 20 Apr 2023 19:22:20 +0000 (+0200) Subject: misc: Improve cstr! macro implementation X-Git-Url: https://git.nega.tv//gitweb.cgi?a=commitdiff_plain;h=00013b87a356dcdce7b83be5db244d996f63014f;p=josh%2Fnarcissus misc: Improve cstr! macro implementation --- diff --git a/libs/ffi/renderdoc-sys/src/helpers.rs b/libs/ffi/renderdoc-sys/src/helpers.rs index 6de9823..8aee19c 100644 --- a/libs/ffi/renderdoc-sys/src/helpers.rs +++ b/libs/ffi/renderdoc-sys/src/helpers.rs @@ -1,14 +1,9 @@ -#[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; } @@ -20,17 +15,11 @@ macro_rules! cstr { $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; diff --git a/libs/ffi/vulkan-sys/src/helpers.rs b/libs/ffi/vulkan-sys/src/helpers.rs index 6de9823..8aee19c 100644 --- a/libs/ffi/vulkan-sys/src/helpers.rs +++ b/libs/ffi/vulkan-sys/src/helpers.rs @@ -1,14 +1,9 @@ -#[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; } @@ -20,17 +15,11 @@ macro_rules! cstr { $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; diff --git a/libs/narcissus-core/src/lib.rs b/libs/narcissus-core/src/lib.rs index b57b5cb..29a194c 100644 --- a/libs/narcissus-core/src/lib.rs +++ b/libs/narcissus-core/src/lib.rs @@ -221,17 +221,12 @@ pub fn oom() -> ! { 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; } @@ -243,17 +238,11 @@ macro_rules! cstr { $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() -> Box> {