From 7e51d60e09cddafb65dea8285feed8ad9d1ff92b Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 7 Jan 2024 10:52:08 +0100 Subject: [PATCH] Rename block layout to block header --- crates/block2/src/abi.rs | 4 ++-- crates/block2/src/block.rs | 18 +++++++++--------- crates/block2/src/concrete_block.rs | 14 +++++++------- crates/block2/src/debug.rs | 18 +++++++++--------- crates/block2/src/global.rs | 28 ++++++++++++++-------------- crates/block2/src/rc_block.rs | 8 ++++---- crates/tests/src/ffi.rs | 4 ++-- 7 files changed, 47 insertions(+), 47 deletions(-) diff --git a/crates/block2/src/abi.rs b/crates/block2/src/abi.rs index cbdd89c84..87bd0e147 100644 --- a/crates/block2/src/abi.rs +++ b/crates/block2/src/abi.rs @@ -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 diff --git a/crates/block2/src/block.rs b/crates/block2/src/block.rs index d575cd37d..79f07cc10 100644 --- a/crates/block2/src/block.rs +++ b/crates/block2/src/block.rs @@ -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. /// @@ -91,10 +91,10 @@ block_args_impl!( #[repr(C)] pub struct Block { _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, + // provenance over `BlockHeader`. + _header: PhantomData, // To get correct variance on args and return types _p: PhantomData R>, } @@ -115,9 +115,9 @@ impl Block { /// 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::().as_ref().unwrap_unchecked() }; + let header = unsafe { ptr.cast::().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) } } @@ -127,8 +127,8 @@ impl fmt::Debug for Block { 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::().as_ref().unwrap() }; - debug_block_layout(layout, &mut f); + let header = unsafe { ptr.cast::().as_ref().unwrap() }; + debug_block_header(header, &mut f); f.finish_non_exhaustive() } } diff --git a/crates/block2/src/concrete_block.rs b/crates/block2/src/concrete_block.rs index d1ede14e7..c648a0aeb 100644 --- a/crates/block2/src/concrete_block.rs +++ b/crates/block2/src/concrete_block.rs @@ -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 { @@ -166,7 +166,7 @@ concrete_block_impl!( #[repr(C)] pub struct ConcreteBlock { p: PhantomData>, - pub(crate) layout: BlockLayout, + pub(crate) header: BlockHeader, pub(crate) closure: F, } @@ -215,7 +215,7 @@ impl ConcreteBlock { /// 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), @@ -226,7 +226,7 @@ impl ConcreteBlock { }; Self { p: PhantomData, - layout, + header, closure, } } @@ -246,7 +246,7 @@ impl ConcreteBlock { impl Clone for ConcreteBlock { 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()) } } } @@ -272,7 +272,7 @@ unsafe extern "C" fn block_context_copy(_dst: *mut c_void, _src: *mut c_void) impl fmt::Debug for ConcreteBlock { 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() } diff --git a/crates/block2/src/debug.rs b/crates/block2/src/debug.rs index 3cd579b2a..503db5ef3 100644 --- a/crates/block2/src/debug.rs +++ b/crates/block2/src/debug.rs @@ -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)] @@ -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, }, ); } diff --git a/crates/block2/src/global.rs b/crates/block2/src/global.rs index 84f9a3212..2ef393a1b 100644 --- a/crates/block2/src/global.rs +++ b/crates/block2/src/global.rs @@ -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::() as c_ulong, + size: mem::size_of::() as c_ulong, }; /// An Objective-C block that does not capture its environment. @@ -30,7 +30,7 @@ const GLOBAL_DESCRIPTOR: BlockDescriptor = BlockDescriptor { /// [`global_block!`]: crate::global_block #[repr(C)] pub struct GlobalBlock { - pub(crate) layout: BlockLayout, + pub(crate) header: BlockHeader, p: PhantomData<(A, R)>, } @@ -58,7 +58,7 @@ impl GlobalBlock { 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, @@ -72,9 +72,9 @@ impl GlobalBlock { /// 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, } } @@ -98,7 +98,7 @@ where impl fmt::Debug for GlobalBlock { 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() } } @@ -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 } @@ -196,7 +196,7 @@ macro_rules! global_block { unsafe extern "C" fn(), >(inner) }); - $crate::GlobalBlock::from_layout(layout) + $crate::GlobalBlock::from_header(header) }; }; } @@ -267,8 +267,8 @@ mod tests { #[test] fn test_debug() { - let invoke = NOOP_BLOCK.layout.invoke.unwrap(); - let size = mem::size_of::(); + let invoke = NOOP_BLOCK.header.invoke.unwrap(); + let size = mem::size_of::(); let expected = format!( "GlobalBlock {{ isa: _NSConcreteGlobalBlock, diff --git a/crates/block2/src/rc_block.rs b/crates/block2/src/rc_block.rs index 1291d3ba2..d7ce12de5 100644 --- a/crates/block2/src/rc_block.rs +++ b/crates/block2/src/rc_block.rs @@ -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. @@ -64,8 +64,8 @@ impl Drop for RcBlock { impl fmt::Debug for RcBlock { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut f = f.debug_struct("RcBlock"); - let layout = unsafe { self.ptr.cast::().as_ref().unwrap() }; - debug_block_layout(layout, &mut f); + let header = unsafe { self.ptr.cast::().as_ref().unwrap() }; + debug_block_header(header, &mut f); f.finish_non_exhaustive() } } diff --git a/crates/tests/src/ffi.rs b/crates/tests/src/ffi.rs index 517aa6806..def91c157 100644 --- a/crates/tests/src/ffi.rs +++ b/crates/tests/src/ffi.rs @@ -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:#?}"); }