diff options
| author | Peter Collingbourne <pcc@google.com> | 2022-11-23 18:16:28 -0800 |
|---|---|---|
| committer | Peter Collingbourne <pcc@google.com> | 2022-12-05 15:05:43 -0800 |
| commit | b4278b02a7e6ad814c09bbc6c066c1713171fe82 (patch) | |
| tree | 234e2f7e866fe33060609c4a201b674dc97fadcc | |
| parent | f44a0153bc4efbb93933c9859168315883d6edc2 (diff) | |
| download | rust-b4278b02a7e6ad814c09bbc6c066c1713171fe82.tar.gz rust-b4278b02a7e6ad814c09bbc6c066c1713171fe82.zip | |
Reimplement weak! using Option.
| -rw-r--r-- | library/std/src/sys/unix/weak.rs | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/library/std/src/sys/unix/weak.rs b/library/std/src/sys/unix/weak.rs index e4ff21b25bd..f5a4ce929b2 100644 --- a/library/std/src/sys/unix/weak.rs +++ b/library/std/src/sys/unix/weak.rs @@ -29,7 +29,21 @@ use crate::ptr; use crate::sync::atomic::{self, AtomicPtr, Ordering}; // We can use true weak linkage on ELF targets. -#[cfg(not(any(target_os = "macos", target_os = "ios")))] +#[cfg(all(not(any(target_os = "macos", target_os = "ios")), not(bootstrap)))] +pub(crate) macro weak { + (fn $name:ident($($t:ty),*) -> $ret:ty) => ( + let ref $name: ExternWeak<unsafe extern "C" fn($($t),*) -> $ret> = { + extern "C" { + #[linkage = "extern_weak"] + static $name: Option<unsafe extern "C" fn($($t),*) -> $ret>; + } + #[allow(unused_unsafe)] + ExternWeak::new(unsafe { $name }) + }; + ) +} + +#[cfg(all(not(any(target_os = "macos", target_os = "ios")), bootstrap))] pub(crate) macro weak { (fn $name:ident($($t:ty),*) -> $ret:ty) => ( let ref $name: ExternWeak<unsafe extern "C" fn($($t),*) -> $ret> = { @@ -47,11 +61,31 @@ pub(crate) macro weak { #[cfg(any(target_os = "macos", target_os = "ios"))] pub(crate) use self::dlsym as weak; +#[cfg(not(bootstrap))] +pub(crate) struct ExternWeak<F: Copy> { + weak_ptr: Option<F>, +} + +#[cfg(not(bootstrap))] +impl<F: Copy> ExternWeak<F> { + #[inline] + pub(crate) fn new(weak_ptr: Option<F>) -> Self { + ExternWeak { weak_ptr } + } + + #[inline] + pub(crate) fn get(&self) -> Option<F> { + self.weak_ptr + } +} + +#[cfg(bootstrap)] pub(crate) struct ExternWeak<F> { weak_ptr: *const libc::c_void, _marker: PhantomData<F>, } +#[cfg(bootstrap)] impl<F> ExternWeak<F> { #[inline] pub(crate) fn new(weak_ptr: *const libc::c_void) -> Self { @@ -59,6 +93,7 @@ impl<F> ExternWeak<F> { } } +#[cfg(bootstrap)] impl<F> ExternWeak<F> { #[inline] pub(crate) fn get(&self) -> Option<F> { |
