use core::ffi::c_void;
use core::marker::PhantomData;
use core::mem;
use core::ops::Deref;
use core::ptr;
use std::os::raw::c_ulong;
use objc2::encode::EncodeReturn;
use super::{ffi, Block};
use crate::BlockArguments;
const GLOBAL_DESCRIPTOR: ffi::Block_descriptor_header = ffi::Block_descriptor_header {
reserved: 0,
size: mem::size_of::<ffi::Block_layout>() as c_ulong,
};
#[repr(C)]
pub struct GlobalBlock<A, R = ()> {
pub(crate) layout: ffi::Block_layout,
p: PhantomData<(A, R)>,
}
unsafe impl<A, R> Sync for GlobalBlock<A, R>
where
A: BlockArguments,
R: EncodeReturn,
{
}
unsafe impl<A, R> Send for GlobalBlock<A, R>
where
A: BlockArguments,
R: EncodeReturn,
{
}
impl<A, R> GlobalBlock<A, R> {
const FLAGS: ffi::block_flags = ffi::BLOCK_IS_GLOBAL | ffi::BLOCK_USE_STRET;
#[doc(hidden)]
pub const __DEFAULT_LAYOUT: ffi::Block_layout = ffi::Block_layout {
isa: ptr::null_mut(),
flags: Self::FLAGS,
reserved: 0,
invoke: None,
descriptor: &GLOBAL_DESCRIPTOR as *const ffi::Block_descriptor_header as *mut c_void,
};
#[doc(hidden)]
pub const unsafe fn from_layout(layout: ffi::Block_layout) -> Self {
Self {
layout,
p: PhantomData,
}
}
}
impl<A, R> Deref for GlobalBlock<A, R>
where
A: BlockArguments,
R: EncodeReturn,
{
type Target = Block<A, R>;
fn deref(&self) -> &Self::Target {
let ptr: *const Self = self;
let ptr: *const Block<A, R> = ptr.cast();
unsafe { ptr.as_ref().unwrap_unchecked() }
}
}
#[macro_export]
macro_rules! global_block {
(
$(#[$m:meta])*
$vis:vis static $name:ident = || $(-> $r:ty)? $body:block $(;)?
) => {
$crate::global_block!(
$(#[$m])*
$vis static $name = |,| $(-> $r)? $body
);
};
(
$(#[$m:meta])*
$vis:vis static $name:ident = |$($a:ident: $t:ty),* $(,)?| $(-> $r:ty)? $body:block $(;)?
) => {
$(#[$m])*
#[allow(unused_unsafe)]
$vis static $name: $crate::GlobalBlock<($($t,)*) $(, $r)?> = unsafe {
let mut layout = $crate::GlobalBlock::<($($t,)*) $(, $r)?>::__DEFAULT_LAYOUT;
layout.isa = &$crate::ffi::_NSConcreteGlobalBlock;
layout.invoke = ::core::option::Option::Some({
unsafe extern "C" fn inner(_: *mut $crate::ffi::Block_layout, $($a: $t),*) $(-> $r)? {
$body
}
let inner: unsafe extern "C" fn(*mut $crate::ffi::Block_layout, $($a: $t),*) $(-> $r)? = inner;
::core::mem::transmute(inner)
});
$crate::GlobalBlock::from_layout(layout)
};
};
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::format;
global_block! {
pub(super) static NOOP_BLOCK = || {};
}
global_block! {
#[allow(unused)]
static BLOCK = |x: i32, y: i32, z: i32, w: i32,| -> i32 {
x + y + z + w
};
}
#[test]
fn test_noop_block() {
unsafe { NOOP_BLOCK.call(()) };
}
#[test]
fn test_defined_in_function() {
global_block!(static MY_BLOCK = || -> i32 {
42
});
assert_eq!(unsafe { MY_BLOCK.call(()) }, 42);
}
#[cfg(feature = "apple")]
const DEBUG_BLOCKFLAGS: &str = r#"BlockFlags {
value: "00110000000000000000000000000000",
deallocating: false,
inline_layout_string: false,
small_descriptor: false,
is_noescape: false,
needs_free: false,
has_copy_dispose: false,
has_ctor: false,
is_gc: false,
is_global: true,
use_stret: true,
has_signature: false,
has_extended_layout: false,
over_referenced: false,
reference_count: 0,
..
}"#;
#[cfg(not(feature = "apple"))]
const DEBUG_BLOCKFLAGS: &str = r#"BlockFlags {
value: "00110000000000000000000000000000",
has_copy_dispose: false,
has_ctor: false,
is_global: true,
use_stret: true,
has_signature: false,
over_referenced: false,
reference_count: 0,
..
}"#;
#[test]
fn test_debug() {
let invoke = NOOP_BLOCK.layout.invoke.unwrap();
let size = mem::size_of::<ffi::Block_layout>();
let expected = format!(
"GlobalBlock {{
isa: _NSConcreteGlobalBlock,
flags: {DEBUG_BLOCKFLAGS},
reserved: 0,
invoke: Some(
{invoke:#?},
),
descriptor: BlockDescriptor {{
reserved: 0,
size: {size},
}},
..
}}"
);
assert_eq!(format!("{NOOP_BLOCK:#?}"), expected);
}
}