Skip to content

Commit

Permalink
Preserve backwards compatibility for old rust versions
Browse files Browse the repository at this point in the history
As mentioned in #66, the new release will just be a patch release. To
be on the safe side, we can preserve backwards compatibility by working
around the following two incompatibilities:

* omitting the field name when initializing `Sel`, which requires rust 1.17
* using `AtomicPtr::new` in a constant, which requires rust 1.24
  • Loading branch information
SSheldon committed Jul 7, 2018
1 parent 6584f53 commit df36b02
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
14 changes: 6 additions & 8 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ macro_rules! class {
#[inline(always)]
fn get_class(name: &str) -> Option<&'static $crate::runtime::Class> {
unsafe {
static CLASS: ::std::sync::atomic::AtomicPtr<$crate::runtime::Class> =
::std::sync::atomic::AtomicPtr::new(0 as *mut _);
static CLASS: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::ATOMIC_USIZE_INIT;
// `Relaxed` should be fine since `objc_getClass` is thread-safe.
let ptr = CLASS.load(::std::sync::atomic::Ordering::Relaxed);
let ptr = CLASS.load(::std::sync::atomic::Ordering::Relaxed) as *const $crate::runtime::Class;
if ptr.is_null() {
let cls = $crate::runtime::objc_getClass(name.as_ptr() as *const _);
CLASS.store(cls as *mut _, ::std::sync::atomic::Ordering::Relaxed);
CLASS.store(cls as usize, ::std::sync::atomic::Ordering::Relaxed);
if cls.is_null() { None } else { Some(&*cls) }
} else {
Some(&*ptr)
Expand All @@ -47,14 +46,13 @@ macro_rules! sel_impl {
#[inline(always)]
fn register_sel(name: &str) -> $crate::runtime::Sel {
unsafe {
static SEL: ::std::sync::atomic::AtomicPtr<::std::os::raw::c_void> =
::std::sync::atomic::AtomicPtr::new(0 as *mut _);
let ptr = SEL.load(::std::sync::atomic::Ordering::Relaxed);
static SEL: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::ATOMIC_USIZE_INIT;
let ptr = SEL.load(::std::sync::atomic::Ordering::Relaxed) as *const ::std::os::raw::c_void;
// It should be fine to use `Relaxed` ordering here because `sel_registerName` is
// thread-safe.
if ptr.is_null() {
let sel = $crate::runtime::sel_registerName(name.as_ptr() as *const _);
SEL.store(sel.as_ptr() as *mut _, ::std::sync::atomic::Ordering::Relaxed);
SEL.store(sel.as_ptr() as usize, ::std::sync::atomic::Ordering::Relaxed);
sel
} else {
$crate::runtime::Sel::from_ptr(ptr)
Expand Down
2 changes: 1 addition & 1 deletion src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Sel {
#[inline]
pub unsafe fn from_ptr(ptr: *const c_void) -> Sel {
Sel {
ptr,
ptr: ptr,
}
}

Expand Down

0 comments on commit df36b02

Please sign in to comment.