Skip to content

Commit 802799a

Browse files
authored
Fix wrong pointer type of static _dispatch_main_q binding (#343)
While looking at a copy of this code (in another project that uses `objc2`, which does not yet define `dispatch` integration) this strange cast immediately stands out. `_dispatch_main_q` is defined here as a `dispatch_queue_t`, the `queue` argument in `dispatch_data_create()` is also defined as a `dispatch_queue_t`, yet there is a "borrow" (get *pointer to*) operation before passing it into the function. Instead, this `static` field is supposed to be defined as the `Object` itself, so that any user could take a _pointer to it_ if they wish or need it so. The same is seen in [the `dispatch` crate]: `_dispatch_main_q` is defined as a `dispatch_object_s`, and `dispatch_queue_t` is a _typedef_ to a `*mut dispatch_object_s` to match the above convention. Note that this is not a bugfix, but merely a readability improvement. Use of `mut` here is debatable. [the `dispatch` crate]: https://github.com/SSheldon/rust-dispatch/blob/f540a2d8ccaebf0e87f5805033b9e287e8d01ba5/src/ffi.rs#L33
1 parent 05987d1 commit 802799a

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/device.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ use block::Block;
1111
use log::warn;
1212
use objc::runtime::{NO, YES};
1313

14-
use std::{ffi::CStr, os::raw::c_char, path::Path, ptr};
14+
use std::{
15+
ffi::CStr,
16+
os::raw::c_char,
17+
path::Path,
18+
ptr::{self, addr_of_mut},
19+
};
1520

1621
/// Available on macOS 10.11+, iOS 8.0+, tvOS 9.0+
1722
///
@@ -1475,16 +1480,22 @@ type dispatch_block_t = *const Block<(), ()>;
14751480
const DISPATCH_DATA_DESTRUCTOR_DEFAULT: dispatch_block_t = ptr::null();
14761481

14771482
#[cfg_attr(
1478-
all(feature = "link", any(target_os = "macos", target_os = "ios", target_os = "visionos")),
1483+
all(
1484+
feature = "link",
1485+
any(target_os = "macos", target_os = "ios", target_os = "visionos")
1486+
),
14791487
link(name = "System", kind = "dylib")
14801488
)]
14811489
#[cfg_attr(
1482-
all(feature = "link", not(any(target_os = "macos", target_os = "ios", target_os = "visionos"))),
1490+
all(
1491+
feature = "link",
1492+
not(any(target_os = "macos", target_os = "ios", target_os = "visionos"))
1493+
),
14831494
link(name = "dispatch", kind = "dylib")
14841495
)]
14851496
#[allow(improper_ctypes)]
14861497
extern "C" {
1487-
static _dispatch_main_q: dispatch_queue_t;
1498+
static mut _dispatch_main_q: Object;
14881499

14891500
fn dispatch_data_create(
14901501
buffer: *const std::ffi::c_void,
@@ -1719,7 +1730,7 @@ impl DeviceRef {
17191730
let data = dispatch_data_create(
17201731
library_data.as_ptr() as *const std::ffi::c_void,
17211732
library_data.len() as crate::c_size_t,
1722-
&_dispatch_main_q as *const _ as dispatch_queue_t,
1733+
addr_of_mut!(_dispatch_main_q),
17231734
DISPATCH_DATA_DESTRUCTOR_DEFAULT,
17241735
);
17251736

0 commit comments

Comments
 (0)