Skip to content

Commit f7b4c78

Browse files
committed
Better describe how how to get the exact signature of blocks
1 parent 6b840c2 commit f7b4c78

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

crates/block2/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
## Unreleased - YYYY-MM-DD
88

9+
### Fixed
10+
* Slightly improved documentation around `ManualBlockEncoding`.
11+
912

1013
## 0.6.0 - 2025-01-22
1114

crates/block2/src/traits.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,25 +173,36 @@ impl_traits!(t0: T0, t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6, t7: T7, t8:
173173
/// about what exactly goes on behind the scenes in order to justify all the
174174
/// following precautions.
175175
///
176-
/// The simplest way to achieve this is to somehow obtain a block with the
177-
/// correct signature from the Objective-C runtime and [`Debug`]-display it to
178-
/// copy its `encoding` C string.
176+
/// The easiest way to do this is probably to ask Clang; the following program
177+
/// will print the signature of the block (if you're having trouble linking,
178+
/// you should be able to find the signature in the assembly output).
179179
///
180-
/// Another possibility is to help yourself with [`Encoding::to_string`][enc2s]
181-
/// in order to get the various components of the signature string separately
182-
/// and then concatenate them manually with the required numbers described
183-
/// below inserted at their correct place.
180+
/// ```objective-c
181+
/// #import <Foundation/Foundation.h>
184182
///
185-
/// Yet another possibility is to compile a small Objective-C program, with GCC
186-
/// and GNUstep for example, that simply displays the result of
187-
/// `method_getTypeEncoding(class_getClassMethod([MyClass class] @selector(sel)))`
188-
/// where `MyClass::sel` has a signature compatible with the block you want,
189-
/// after which the string can be slightly modified to fit an actual block
190-
/// instead of a method: see below.
183+
/// // Unstable API, but fine for test usage like this.
184+
/// const char * _Block_signature(void *);
185+
///
186+
/// int main() {
187+
/// // Declare the signature of your block.
188+
/// // This one takes `id` and `int`, and returns `NSString*`.
189+
/// id block = ^NSString* (id a, int b) {
190+
/// return nil;
191+
/// };
192+
///
193+
/// printf("%s\n", _Block_signature((void*)block));
194+
/// return 0;
195+
/// }
196+
/// ```
191197
///
192198
/// A more thorough but manual approach is to only follow the rules described
193199
/// below.
194200
///
201+
/// In this process, you may be able to use [`Encoding::to_string`][enc2s] in
202+
/// order to get the various components of the signature string and then
203+
/// concatenate them manually with the required numbers (described below)
204+
/// inserted at their correct place.
205+
///
195206
/// [enc2s]: objc2::encode::Encoding#impl-Display-for-Encoding
196207
/// [i442-sign-check]: https://github.com/madsmtm/objc2/issues/442#issuecomment-2284932726
197208
///

0 commit comments

Comments
 (0)