Skip to content

Commit b4278b0

Browse files
committed
Reimplement weak! using Option.
1 parent f44a015 commit b4278b0

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

library/std/src/sys/unix/weak.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,21 @@ use crate::ptr;
2929
use crate::sync::atomic::{self, AtomicPtr, Ordering};
3030

3131
// We can use true weak linkage on ELF targets.
32-
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
32+
#[cfg(all(not(any(target_os = "macos", target_os = "ios")), not(bootstrap)))]
33+
pub(crate) macro weak {
34+
(fn $name:ident($($t:ty),*) -> $ret:ty) => (
35+
let ref $name: ExternWeak<unsafe extern "C" fn($($t),*) -> $ret> = {
36+
extern "C" {
37+
#[linkage = "extern_weak"]
38+
static $name: Option<unsafe extern "C" fn($($t),*) -> $ret>;
39+
}
40+
#[allow(unused_unsafe)]
41+
ExternWeak::new(unsafe { $name })
42+
};
43+
)
44+
}
45+
46+
#[cfg(all(not(any(target_os = "macos", target_os = "ios")), bootstrap))]
3347
pub(crate) macro weak {
3448
(fn $name:ident($($t:ty),*) -> $ret:ty) => (
3549
let ref $name: ExternWeak<unsafe extern "C" fn($($t),*) -> $ret> = {
@@ -47,18 +61,39 @@ pub(crate) macro weak {
4761
#[cfg(any(target_os = "macos", target_os = "ios"))]
4862
pub(crate) use self::dlsym as weak;
4963

64+
#[cfg(not(bootstrap))]
65+
pub(crate) struct ExternWeak<F: Copy> {
66+
weak_ptr: Option<F>,
67+
}
68+
69+
#[cfg(not(bootstrap))]
70+
impl<F: Copy> ExternWeak<F> {
71+
#[inline]
72+
pub(crate) fn new(weak_ptr: Option<F>) -> Self {
73+
ExternWeak { weak_ptr }
74+
}
75+
76+
#[inline]
77+
pub(crate) fn get(&self) -> Option<F> {
78+
self.weak_ptr
79+
}
80+
}
81+
82+
#[cfg(bootstrap)]
5083
pub(crate) struct ExternWeak<F> {
5184
weak_ptr: *const libc::c_void,
5285
_marker: PhantomData<F>,
5386
}
5487

88+
#[cfg(bootstrap)]
5589
impl<F> ExternWeak<F> {
5690
#[inline]
5791
pub(crate) fn new(weak_ptr: *const libc::c_void) -> Self {
5892
ExternWeak { weak_ptr, _marker: PhantomData }
5993
}
6094
}
6195

96+
#[cfg(bootstrap)]
6297
impl<F> ExternWeak<F> {
6398
#[inline]
6499
pub(crate) fn get(&self) -> Option<F> {

0 commit comments

Comments
 (0)