From 4bd84b23a8537314132e98b9fb2c3fea2cb57496 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Mon, 28 Oct 2024 18:52:39 +1100 Subject: Use a type-safe helper to cast `&str` and `&[u8]` to `*const c_char` --- compiler/rustc_codegen_llvm/src/common.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'compiler/rustc_codegen_llvm/src/common.rs') 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() + } +} -- cgit 1.4.1-3-g733a5