]> git.nega.tv - josh/narcissus/commitdiff
misc: Improve cstr! macro implementation
authorJoshua Simmons <josh@nega.tv>
Thu, 20 Apr 2023 19:22:20 +0000 (21:22 +0200)
committerJoshua Simmons <josh@nega.tv>
Thu, 20 Apr 2023 19:22:20 +0000 (21:22 +0200)
libs/ffi/renderdoc-sys/src/helpers.rs
libs/ffi/vulkan-sys/src/helpers.rs
libs/narcissus-core/src/lib.rs

index 6de982317f5c3be887d65668888e2e006d162531..8aee19cc91ccfd5348529ac70ea7a1954d8bdab1 100644 (file)
@@ -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;
index 6de982317f5c3be887d65668888e2e006d162531..8aee19cc91ccfd5348529ac70ea7a1954d8bdab1 100644 (file)
@@ -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;
index b57b5cbdadcc5d0beee0fc30c2aefbd89c73faa9..29a194c1be37909733a14f8e14f5b362aa2f967c 100644 (file)
@@ -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<T>() -> Box<MaybeUninit<T>> {