|
1 | | -use crate::ffi::CStr; |
| 1 | +use crate::ffi::c_char; |
2 | 2 | use crate::fmt; |
3 | 3 | use crate::marker::PhantomData; |
4 | 4 |
|
@@ -36,7 +36,7 @@ use crate::marker::PhantomData; |
36 | 36 | pub struct Location<'a> { |
37 | 37 | // A raw pointer is used rather than a reference because the pointer is valid for one more byte |
38 | 38 | // than the length stored in this pointer; the additional byte is the NUL-terminator used by |
39 | | - // `Location::file_with_nul`. |
| 39 | + // `Location::file_ptr`. |
40 | 40 | filename: *const str, |
41 | 41 | line: u32, |
42 | 42 | col: u32, |
@@ -135,27 +135,22 @@ impl<'a> Location<'a> { |
135 | 135 | #[stable(feature = "panic_hooks", since = "1.10.0")] |
136 | 136 | #[rustc_const_stable(feature = "const_location_fields", since = "1.79.0")] |
137 | 137 | pub const fn file(&self) -> &str { |
138 | | - // SAFETY: The filename is valid. |
| 138 | + // SAFETY: The compiler generates a pointer to a valid readable filename. |
139 | 139 | unsafe { &*self.filename } |
140 | 140 | } |
141 | 141 |
|
142 | | - /// Returns the name of the source file as a nul-terminated `CStr`. |
| 142 | + /// Returns the name of the source file as a nul-terminated string. |
143 | 143 | /// |
144 | 144 | /// This is useful for interop with APIs that expect C/C++ `__FILE__` or |
145 | 145 | /// `std::source_location::file_name`, both of which return a nul-terminated `const char*`. |
| 146 | + /// |
| 147 | + /// The pointer is guaranteed to reference the same string as [`Location::file`] with an |
| 148 | + /// additional nul-byte at the end. |
146 | 149 | #[must_use] |
147 | 150 | #[unstable(feature = "file_with_nul", issue = "141727")] |
148 | 151 | #[inline] |
149 | | - pub const fn file_with_nul(&self) -> &CStr { |
150 | | - // SAFETY: The filename is valid for `filename_len+1` bytes, so this addition can't |
151 | | - // overflow. |
152 | | - let cstr_len = unsafe { crate::mem::size_of_val_raw(self.filename).unchecked_add(1) }; |
153 | | - |
154 | | - // SAFETY: The filename is valid for `filename_len+1` bytes. |
155 | | - let slice = unsafe { crate::slice::from_raw_parts(self.filename as *const _, cstr_len) }; |
156 | | - |
157 | | - // SAFETY: The filename is guaranteed to have a trailing nul byte and no interior nul bytes. |
158 | | - unsafe { CStr::from_bytes_with_nul_unchecked(slice) } |
| 152 | + pub const fn file_ptr(&self) -> *const c_char { |
| 153 | + self.filename as *const c_char |
159 | 154 | } |
160 | 155 |
|
161 | 156 | /// Returns the line number from which the panic originated. |
|
0 commit comments