Skip to content

Commit fb0b37e

Browse files
committed
Move implementation details out of global_block!
1 parent ceebf7d commit fb0b37e

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

block2/src/global.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
use core::marker::PhantomData;
22
use core::mem;
33
use core::ops::Deref;
4+
use core::ptr;
45
use std::os::raw::c_ulong;
56

67
use objc2_encode::{Encode, EncodeArguments};
78

89
use super::{ffi, Block};
910
use crate::BlockArguments;
1011

11-
#[doc(hidden)]
12-
pub const __GLOBAL_DESCRIPTOR: ffi::Block_descriptor_header = ffi::Block_descriptor_header {
12+
// TODO: Should this be a static to help the compiler deduplicating them?
13+
const GLOBAL_DESCRIPTOR: ffi::Block_descriptor_header = ffi::Block_descriptor_header {
1314
reserved: 0,
1415
size: mem::size_of::<ffi::Block_layout>() as c_ulong,
1516
};
@@ -46,6 +47,20 @@ where
4647
// constructing the static in `global_block!` with an invalid `GlobalBlock`
4748
// triggers an error.
4849
impl<A, R> GlobalBlock<A, R> {
50+
// TODO: Use new ABI with BLOCK_HAS_SIGNATURE
51+
const FLAGS: ffi::block_flags = ffi::BLOCK_IS_GLOBAL | ffi::BLOCK_USE_STRET;
52+
53+
#[doc(hidden)]
54+
pub const __DEFAULT_LAYOUT: ffi::Block_layout = ffi::Block_layout {
55+
// Populated in `global_block!`
56+
isa: ptr::null_mut(),
57+
flags: Self::FLAGS,
58+
reserved: 0,
59+
// Populated in `global_block!`
60+
invoke: None,
61+
descriptor: &GLOBAL_DESCRIPTOR as *const _ as *mut _,
62+
};
63+
4964
/// Use the [`global_block`] macro instead.
5065
#[doc(hidden)]
5166
pub const unsafe fn from_layout(layout: ffi::Block_layout) -> Self {
@@ -133,21 +148,18 @@ macro_rules! global_block {
133148
$(#[$m])*
134149
#[allow(unused_unsafe)]
135150
$vis static $name: $crate::GlobalBlock<($($t,)*) $(, $r)?> = unsafe {
136-
$crate::GlobalBlock::from_layout($crate::ffi::Block_layout {
137-
isa: &$crate::ffi::_NSConcreteGlobalBlock as *const _ as *mut _,
138-
flags: $crate::ffi::BLOCK_IS_GLOBAL | $crate::ffi::BLOCK_USE_STRET,
139-
reserved: 0,
140-
invoke: {
141-
unsafe extern "C" fn inner(_: *mut $crate::ffi::Block_layout, $($a: $t),*) $(-> $r)? {
142-
$body
143-
}
144-
let inner: unsafe extern "C" fn(*mut $crate::ffi::Block_layout, $($a: $t),*) $(-> $r)? = inner;
145-
146-
// TODO: SAFETY
147-
::core::mem::transmute(inner)
148-
},
149-
descriptor: &$crate::__GLOBAL_DESCRIPTOR as *const _ as *mut _,
150-
})
151+
let mut layout = $crate::GlobalBlock::<($($t,)*) $(, $r)?>::__DEFAULT_LAYOUT;
152+
layout.isa = &$crate::ffi::_NSConcreteGlobalBlock as *const _ as *mut _;
153+
layout.invoke = Some({
154+
unsafe extern "C" fn inner(_: *mut $crate::ffi::Block_layout, $($a: $t),*) $(-> $r)? {
155+
$body
156+
}
157+
let inner: unsafe extern "C" fn(*mut $crate::ffi::Block_layout, $($a: $t),*) $(-> $r)? = inner;
158+
159+
// TODO: SAFETY
160+
::core::mem::transmute(inner)
161+
});
162+
$crate::GlobalBlock::from_layout(layout)
151163
};
152164
};
153165
}

block2/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use objc2_encode::{Encode, EncodeArguments, Encoding, RefEncode};
6969
#[macro_use]
7070
mod global;
7171

72-
pub use global::{GlobalBlock, __GLOBAL_DESCRIPTOR};
72+
pub use global::GlobalBlock;
7373

7474
/// Types that may be used as the arguments to an Objective-C block.
7575
pub trait BlockArguments: Sized {

0 commit comments

Comments
 (0)