Skip to content

Commit

Permalink
Rename block layout to block header
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 7, 2024
1 parent d730c13 commit 7e51d60
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions crates/block2/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ pub(crate) const BLOCK_FIELD_IS_WEAK: c_int = 16;
/// called from __block (byref) copy/dispose support routines.
pub(crate) const BLOCK_BYREF_CALLER: c_int = 128;

/// The expected layout of every block.
/// The expected header of every block.
#[repr(C)]
#[doc(alias = "__block_literal")]
#[doc(alias = "Block_layout")]
#[doc(alias = "Block_basic")]
#[allow(missing_debug_implementations)]
pub struct BlockLayout {
pub struct BlockHeader {
/// Class pointer.
///
/// Always initialised to &_NSConcreteStackBlock for blocks that are
Expand Down
18 changes: 9 additions & 9 deletions crates/block2/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use core::mem;

use objc2::encode::{EncodeArgument, EncodeReturn, Encoding, RefEncode};

use crate::abi::BlockLayout;
use crate::debug::debug_block_layout;
use crate::abi::BlockHeader;
use crate::debug::debug_block_header;

/// Types that may be used as the arguments of an Objective-C block.
///
Expand Down Expand Up @@ -91,10 +91,10 @@ block_args_impl!(
#[repr(C)]
pub struct Block<A, R> {
_inner: [u8; 0],
// We store `BlockLayout` + the closure captures, but `Block` has to
// We store `BlockHeader` + the closure captures, but `Block` has to
// remain an empty type otherwise the compiler thinks we only have
// provenance over `BlockLayout`.
_layout: PhantomData<BlockLayout>,
// provenance over `BlockHeader`.
_header: PhantomData<BlockHeader>,
// To get correct variance on args and return types
_p: PhantomData<fn(A) -> R>,
}
Expand All @@ -115,9 +115,9 @@ impl<A: BlockArguments, R: EncodeReturn> Block<A, R> {
/// caller must ensure that calling it will not cause a data race.
pub unsafe fn call(&self, args: A) -> R {
let ptr: *const Self = self;
let layout = unsafe { ptr.cast::<BlockLayout>().as_ref().unwrap_unchecked() };
let header = unsafe { ptr.cast::<BlockHeader>().as_ref().unwrap_unchecked() };
// TODO: Is `invoke` actually ever null?
let invoke = layout.invoke.unwrap_or_else(|| unreachable!());
let invoke = header.invoke.unwrap_or_else(|| unreachable!());

unsafe { A::__call_block(invoke, ptr as *mut Self, args) }
}
Expand All @@ -127,8 +127,8 @@ impl<A, R> fmt::Debug for Block<A, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("Block");
let ptr: *const Self = self;
let layout = unsafe { ptr.cast::<BlockLayout>().as_ref().unwrap() };
debug_block_layout(layout, &mut f);
let header = unsafe { ptr.cast::<BlockHeader>().as_ref().unwrap() };
debug_block_header(header, &mut f);
f.finish_non_exhaustive()
}
}
14 changes: 7 additions & 7 deletions crates/block2/src/concrete_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::os::raw::c_ulong;

use objc2::encode::{EncodeArgument, EncodeReturn, Encoding, RefEncode};

use crate::abi::{BlockDescriptorCopyDispose, BlockDescriptorPtr, BlockFlags, BlockLayout};
use crate::debug::debug_block_layout;
use crate::abi::{BlockDescriptorCopyDispose, BlockDescriptorPtr, BlockFlags, BlockHeader};
use crate::debug::debug_block_header;
use crate::{ffi, Block, BlockArguments, RcBlock};

mod private {
Expand Down Expand Up @@ -166,7 +166,7 @@ concrete_block_impl!(
#[repr(C)]
pub struct ConcreteBlock<A, R, F> {
p: PhantomData<Block<A, R>>,
pub(crate) layout: BlockLayout,
pub(crate) header: BlockHeader,
pub(crate) closure: F,
}

Expand Down Expand Up @@ -215,7 +215,7 @@ impl<A, R, F> ConcreteBlock<A, R, F> {
/// Unsafe because the caller must ensure the invoke function takes the
/// correct arguments.
unsafe fn with_invoke(invoke: unsafe extern "C" fn(), closure: F) -> Self {
let layout = BlockLayout {
let header = BlockHeader {
isa: unsafe { ptr::addr_of!(ffi::_NSConcreteStackBlock) },
flags: Self::FLAGS,
reserved: MaybeUninit::new(0),
Expand All @@ -226,7 +226,7 @@ impl<A, R, F> ConcreteBlock<A, R, F> {
};
Self {
p: PhantomData,
layout,
header,
closure,
}
}
Expand All @@ -246,7 +246,7 @@ impl<A, R, F: 'static> ConcreteBlock<A, R, F> {

impl<A, R, F: Clone> Clone for ConcreteBlock<A, R, F> {
fn clone(&self) -> Self {
unsafe { Self::with_invoke(self.layout.invoke.unwrap(), self.closure.clone()) }
unsafe { Self::with_invoke(self.header.invoke.unwrap(), self.closure.clone()) }
}
}

Expand All @@ -272,7 +272,7 @@ unsafe extern "C" fn block_context_copy<B>(_dst: *mut c_void, _src: *mut c_void)
impl<A, R, F: fmt::Debug> fmt::Debug for ConcreteBlock<A, R, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("ConcreteBlock");
debug_block_layout(&self.layout, &mut f);
debug_block_header(&self.header, &mut f);
f.field("closure", &self.closure);
f.finish()
}
Expand Down
18 changes: 9 additions & 9 deletions crates/block2/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::fmt::{Debug, DebugStruct, Error, Formatter};
use core::ptr;
use std::ffi::CStr;

use crate::abi::{BlockDescriptorPtr, BlockFlags, BlockLayout};
use crate::abi::{BlockDescriptorPtr, BlockFlags, BlockHeader};
use crate::ffi;

#[derive(Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -33,17 +33,17 @@ impl Debug for Isa {
}
}

pub(crate) fn debug_block_layout(layout: &BlockLayout, f: &mut DebugStruct<'_, '_>) {
f.field("isa", &Isa(layout.isa));
f.field("flags", &layout.flags);
f.field("reserved", &layout.reserved);
f.field("invoke", &layout.invoke);
pub(crate) fn debug_block_header(header: &BlockHeader, f: &mut DebugStruct<'_, '_>) {
f.field("isa", &Isa(header.isa));
f.field("flags", &header.flags);
f.field("reserved", &header.reserved);
f.field("invoke", &header.invoke);
f.field(
"descriptor",
&BlockDescriptorHelper {
has_copy_dispose: layout.flags.0 & BlockFlags::BLOCK_HAS_COPY_DISPOSE.0 != 0,
has_signature: layout.flags.0 & BlockFlags::BLOCK_HAS_SIGNATURE.0 != 0,
descriptor: layout.descriptor,
has_copy_dispose: header.flags.0 & BlockFlags::BLOCK_HAS_COPY_DISPOSE.0 != 0,
has_signature: header.flags.0 & BlockFlags::BLOCK_HAS_SIGNATURE.0 != 0,
descriptor: header.descriptor,
},
);
}
Expand Down
28 changes: 14 additions & 14 deletions crates/block2/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use std::os::raw::c_ulong;

use objc2::encode::EncodeReturn;

use crate::abi::{BlockDescriptor, BlockDescriptorPtr, BlockFlags, BlockLayout};
use crate::debug::debug_block_layout;
use crate::abi::{BlockDescriptor, BlockDescriptorPtr, BlockFlags, BlockHeader};
use crate::debug::debug_block_header;
use crate::{Block, BlockArguments};

// TODO: Should this be a static to help the compiler deduplicating them?
const GLOBAL_DESCRIPTOR: BlockDescriptor = BlockDescriptor {
reserved: 0,
size: mem::size_of::<BlockLayout>() as c_ulong,
size: mem::size_of::<BlockHeader>() as c_ulong,
};

/// An Objective-C block that does not capture its environment.
Expand All @@ -30,7 +30,7 @@ const GLOBAL_DESCRIPTOR: BlockDescriptor = BlockDescriptor {
/// [`global_block!`]: crate::global_block
#[repr(C)]
pub struct GlobalBlock<A, R = ()> {
pub(crate) layout: BlockLayout,
pub(crate) header: BlockHeader,
p: PhantomData<(A, R)>,
}

Expand Down Expand Up @@ -58,7 +58,7 @@ impl<A, R> GlobalBlock<A, R> {
BlockFlags(BlockFlags::BLOCK_IS_GLOBAL.0 | BlockFlags::BLOCK_USE_STRET.0);

#[doc(hidden)]
pub const __DEFAULT_LAYOUT: BlockLayout = BlockLayout {
pub const __DEFAULT_HEADER: BlockHeader = BlockHeader {
// Populated in `global_block!`
isa: ptr::null_mut(),
flags: Self::FLAGS,
Expand All @@ -72,9 +72,9 @@ impl<A, R> GlobalBlock<A, R> {

/// Use the [`global_block`] macro instead.
#[doc(hidden)]
pub const unsafe fn from_layout(layout: BlockLayout) -> Self {
pub const unsafe fn from_header(header: BlockHeader) -> Self {
Self {
layout,
header,
p: PhantomData,
}
}
Expand All @@ -98,7 +98,7 @@ where
impl<A, R> fmt::Debug for GlobalBlock<A, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("GlobalBlock");
debug_block_layout(&self.layout, &mut f);
debug_block_header(&self.header, &mut f);
f.finish_non_exhaustive()
}
}
Expand Down Expand Up @@ -183,9 +183,9 @@ macro_rules! global_block {
$(#[$m])*
#[allow(unused_unsafe)]
$vis static $name: $crate::GlobalBlock<($($t,)*) $(, $r)?> = unsafe {
let mut layout = $crate::GlobalBlock::<($($t,)*) $(, $r)?>::__DEFAULT_LAYOUT;
layout.isa = ::core::ptr::addr_of!($crate::ffi::_NSConcreteGlobalBlock);
layout.invoke = ::core::option::Option::Some({
let mut header = $crate::GlobalBlock::<($($t,)*) $(, $r)?>::__DEFAULT_HEADER;
header.isa = ::core::ptr::addr_of!($crate::ffi::_NSConcreteGlobalBlock);
header.invoke = ::core::option::Option::Some({
unsafe extern "C" fn inner(_: *mut $crate::GlobalBlock<($($t,)*) $(, $r)?>, $($a: $t),*) $(-> $r)? {
$body
}
Expand All @@ -196,7 +196,7 @@ macro_rules! global_block {
unsafe extern "C" fn(),
>(inner)
});
$crate::GlobalBlock::from_layout(layout)
$crate::GlobalBlock::from_header(header)
};
};
}
Expand Down Expand Up @@ -267,8 +267,8 @@ mod tests {

#[test]
fn test_debug() {
let invoke = NOOP_BLOCK.layout.invoke.unwrap();
let size = mem::size_of::<BlockLayout>();
let invoke = NOOP_BLOCK.header.invoke.unwrap();
let size = mem::size_of::<BlockHeader>();
let expected = format!(
"GlobalBlock {{
isa: _NSConcreteGlobalBlock,
Expand Down
8 changes: 4 additions & 4 deletions crates/block2/src/rc_block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use core::fmt;
use core::ops::Deref;

use crate::abi::BlockLayout;
use crate::debug::debug_block_layout;
use crate::abi::BlockHeader;
use crate::debug::debug_block_header;
use crate::{ffi, Block};

/// A reference-counted Objective-C block.
Expand Down Expand Up @@ -64,8 +64,8 @@ impl<A, R> Drop for RcBlock<A, R> {
impl<A, R> fmt::Debug for RcBlock<A, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("RcBlock");
let layout = unsafe { self.ptr.cast::<BlockLayout>().as_ref().unwrap() };
debug_block_layout(layout, &mut f);
let header = unsafe { self.ptr.cast::<BlockHeader>().as_ref().unwrap() };
debug_block_header(header, &mut f);
f.finish_non_exhaustive()
}
}
4 changes: 2 additions & 2 deletions crates/tests/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ extern "C" {
}

#[no_mangle]
extern "C" fn debug_block(layout: *mut c_void) {
let block: &Block<(), ()> = unsafe { &*(layout as *const Block<(), ()>) };
extern "C" fn debug_block(block: *mut c_void) {
let block: &Block<(), ()> = unsafe { &*(block as *const Block<(), ()>) };
std::println!("{block:#?}");
}

Expand Down

0 comments on commit 7e51d60

Please sign in to comment.