|
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, |
@@ -132,27 +132,22 @@ impl<'a> Location<'a> { |
132 | 132 | #[stable(feature = "panic_hooks", since = "1.10.0")] |
133 | 133 | #[rustc_const_stable(feature = "const_location_fields", since = "1.79.0")] |
134 | 134 | pub const fn file(&self) -> &str { |
135 | | - // SAFETY: The filename is valid. |
| 135 | + // SAFETY: The compiler generates a pointer to a valid readable filename. |
136 | 136 | unsafe { &*self.filename } |
137 | 137 | } |
138 | 138 |
|
139 | | - /// Returns the name of the source file as a nul-terminated `CStr`. |
| 139 | + /// Returns the name of the source file as a nul-terminated string. |
140 | 140 | /// |
141 | 141 | /// This is useful for interop with APIs that expect C/C++ `__FILE__` or |
142 | 142 | /// `std::source_location::file_name`, both of which return a nul-terminated `const char*`. |
| 143 | + /// |
| 144 | + /// The pointer is guaranteed to reference the same string as [`Location::file`] with an |
| 145 | + /// additional nul-byte at the end. |
143 | 146 | #[must_use] |
144 | 147 | #[unstable(feature = "file_with_nul", issue = "141727")] |
145 | 148 | #[inline] |
146 | | - pub const fn file_with_nul(&self) -> &CStr { |
147 | | - // SAFETY: The filename is valid for `filename_len+1` bytes, so this addition can't |
148 | | - // overflow. |
149 | | - let cstr_len = unsafe { crate::mem::size_of_val_raw(self.filename).unchecked_add(1) }; |
150 | | - |
151 | | - // SAFETY: The filename is valid for `filename_len+1` bytes. |
152 | | - let slice = unsafe { crate::slice::from_raw_parts(self.filename as *const _, cstr_len) }; |
153 | | - |
154 | | - // SAFETY: The filename is guaranteed to have a trailing nul byte and no interior nul bytes. |
155 | | - unsafe { CStr::from_bytes_with_nul_unchecked(slice) } |
| 149 | + pub const fn file_ptr(&self) -> *const c_char { |
| 150 | + self.filename as *const c_char |
156 | 151 | } |
157 | 152 |
|
158 | 153 | /// Returns the line number from which the panic originated. |
|
0 commit comments