Skip to content

Commit e63299e

Browse files
committed
Fix block documentation
1 parent 04c7443 commit e63299e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

block2/src/global.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const GLOBAL_DESCRIPTOR: ffi::Block_descriptor_header = ffi::Block_descriptor_he
2222
///
2323
/// If [`ConcreteBlock`] is the [`Fn`]-block equivalent, this is likewise the
2424
/// [`fn`]-block equivalent.
25+
///
26+
/// [`ConcreteBlock`]: crate::ConcreteBlock
2527
#[repr(C)]
2628
pub struct GlobalBlock<A, R = ()> {
2729
layout: ffi::Block_layout,
@@ -105,7 +107,7 @@ where
105107
/// ```
106108
/// use block2::global_block;
107109
/// global_block! {
108-
/// static ADDER_BLOCK = |x: i32, y: i32,| -> i32 {
110+
/// static ADDER_BLOCK = |x: i32, y: i32| -> i32 {
109111
/// x + y
110112
/// }
111113
/// };
@@ -132,6 +134,8 @@ where
132134
/// pub static INVALID_BLOCK = |b: Box<i32>| {}
133135
/// };
134136
/// ```
137+
///
138+
/// [`Box`]: std::boxed::Box
135139
#[macro_export]
136140
macro_rules! global_block {
137141
// `||` is parsed as one token

block2/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ It is important to copy your block to the heap (with the `copy` method) before
4343
passing it to Objective-C; this is because our `ConcreteBlock` is only meant
4444
to be copied once, and we can enforce this in Rust, but if Objective-C code
4545
were to copy it twice we could have a double free.
46+
47+
As an optimization if your block doesn't capture any variables, you can use
48+
the [`global_block!`] macro to create a static block:
49+
50+
```
51+
use block2::global_block;
52+
global_block! {
53+
static MY_BLOCK = || -> f32 {
54+
10.0
55+
}
56+
};
57+
assert_eq!(unsafe { MY_BLOCK.call(()) }, 10.0);
58+
```
4659
*/
4760

4861
#![no_std]

0 commit comments

Comments
 (0)