|
19 | 19 | //! function is called. In the worst case, multiple threads may all end up
|
20 | 20 | //! importing the same function unnecessarily.
|
21 | 21 |
|
22 |
| -use crate::ffi::{c_void, CStr}; |
| 22 | +use crate::ffi::{c_void, cstr, CStr}; |
23 | 23 | use crate::ptr::NonNull;
|
24 | 24 | use crate::sync::atomic::Ordering;
|
25 | 25 | use crate::sys::c;
|
@@ -67,38 +67,6 @@ unsafe extern "C" fn init() {
|
67 | 67 | load_synch_functions();
|
68 | 68 | }
|
69 | 69 |
|
70 |
| -/// Helper macro for creating CStrs from literals and symbol names. |
71 |
| -macro_rules! ansi_str { |
72 |
| - (sym $ident:ident) => {{ |
73 |
| - #[allow(unused_unsafe)] |
74 |
| - crate::sys::compat::const_cstr_from_bytes(concat!(stringify!($ident), "\0").as_bytes()) |
75 |
| - }}; |
76 |
| - ($lit:literal) => {{ crate::sys::compat::const_cstr_from_bytes(concat!($lit, "\0").as_bytes()) }}; |
77 |
| -} |
78 |
| - |
79 |
| -/// Creates a C string wrapper from a byte slice, in a constant context. |
80 |
| -/// |
81 |
| -/// This is a utility function used by the [`ansi_str`] macro. |
82 |
| -/// |
83 |
| -/// # Panics |
84 |
| -/// |
85 |
| -/// Panics if the slice is not null terminated or contains nulls, except as the last item |
86 |
| -pub(crate) const fn const_cstr_from_bytes(bytes: &'static [u8]) -> &'static CStr { |
87 |
| - if !matches!(bytes.last(), Some(&0)) { |
88 |
| - panic!("A CStr must be null terminated"); |
89 |
| - } |
90 |
| - let mut i = 0; |
91 |
| - // At this point `len()` is at least 1. |
92 |
| - while i < bytes.len() - 1 { |
93 |
| - if bytes[i] == 0 { |
94 |
| - panic!("A CStr must not have interior nulls") |
95 |
| - } |
96 |
| - i += 1; |
97 |
| - } |
98 |
| - // SAFETY: The safety is ensured by the above checks. |
99 |
| - unsafe { crate::ffi::CStr::from_bytes_with_nul_unchecked(bytes) } |
100 |
| -} |
101 |
| - |
102 | 70 | /// Represents a loaded module.
|
103 | 71 | ///
|
104 | 72 | /// Note that the modules std depends on must not be unloaded.
|
@@ -161,7 +129,7 @@ macro_rules! compat_fn_with_fallback {
|
161 | 129 |
|
162 | 130 | fn load_from_module(module: Option<Module>) -> F {
|
163 | 131 | unsafe {
|
164 |
| - static SYMBOL_NAME: &CStr = ansi_str!(sym $symbol); |
| 132 | + static SYMBOL_NAME: &CStr = cstr!(stringify!($symbol)); |
165 | 133 | if let Some(f) = module.and_then(|m| m.proc_address(SYMBOL_NAME)) {
|
166 | 134 | PTR.store(f.as_ptr(), Ordering::Relaxed);
|
167 | 135 | mem::transmute(f)
|
@@ -224,9 +192,9 @@ macro_rules! compat_fn_optional {
|
224 | 192 | /// Load all needed functions from "api-ms-win-core-synch-l1-2-0".
|
225 | 193 | pub(super) fn load_synch_functions() {
|
226 | 194 | fn try_load() -> Option<()> {
|
227 |
| - const MODULE_NAME: &CStr = ansi_str!("api-ms-win-core-synch-l1-2-0"); |
228 |
| - const WAIT_ON_ADDRESS: &CStr = ansi_str!("WaitOnAddress"); |
229 |
| - const WAKE_BY_ADDRESS_SINGLE: &CStr = ansi_str!("WakeByAddressSingle"); |
| 195 | + const MODULE_NAME: &CStr = cstr!("api-ms-win-core-synch-l1-2-0"); |
| 196 | + const WAIT_ON_ADDRESS: &CStr = cstr!("WaitOnAddress"); |
| 197 | + const WAKE_BY_ADDRESS_SINGLE: &CStr = cstr!("WakeByAddressSingle"); |
230 | 198 |
|
231 | 199 | // Try loading the library and all the required functions.
|
232 | 200 | // If any step fails, then they all fail.
|
|
0 commit comments