about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm/src/common.rs
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2024-10-28 18:52:39 +1100
committerZalathar <Zalathar@users.noreply.github.com>2024-10-28 21:31:32 +1100
commit4bd84b23a8537314132e98b9fb2c3fea2cb57496 (patch)
tree2713d71a7a0e2b74a492006c9f95f09ba06edf72 /compiler/rustc_codegen_llvm/src/common.rs
parent66701c42263042f7120385725606edeb987ad4f1 (diff)
downloadrust-4bd84b23a8537314132e98b9fb2c3fea2cb57496.tar.gz
rust-4bd84b23a8537314132e98b9fb2c3fea2cb57496.zip
Use a type-safe helper to cast `&str` and `&[u8]` to `*const c_char`
Diffstat (limited to 'compiler/rustc_codegen_llvm/src/common.rs')
-rw-r--r--compiler/rustc_codegen_llvm/src/common.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs
index ff47eb944dd..29adc616ee2 100644
--- a/compiler/rustc_codegen_llvm/src/common.rs
+++ b/compiler/rustc_codegen_llvm/src/common.rs
@@ -392,3 +392,21 @@ pub(crate) fn get_dllimport<'tcx>(
     tcx.native_library(id)
         .and_then(|lib| lib.dll_imports.iter().find(|di| di.name.as_str() == name))
 }
+
+/// Extension trait for explicit casts to `*const c_char`.
+pub(crate) trait AsCCharPtr {
+    /// Equivalent to `self.as_ptr().cast()`, but only casts to `*const c_char`.
+    fn as_c_char_ptr(&self) -> *const c_char;
+}
+
+impl AsCCharPtr for str {
+    fn as_c_char_ptr(&self) -> *const c_char {
+        self.as_ptr().cast()
+    }
+}
+
+impl AsCCharPtr for [u8] {
+    fn as_c_char_ptr(&self) -> *const c_char {
+        self.as_ptr().cast()
+    }
+}