File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ const GLOBAL_DESCRIPTOR: ffi::Block_descriptor_header = ffi::Block_descriptor_he
22
22
///
23
23
/// If [`ConcreteBlock`] is the [`Fn`]-block equivalent, this is likewise the
24
24
/// [`fn`]-block equivalent.
25
+ ///
26
+ /// [`ConcreteBlock`]: crate::ConcreteBlock
25
27
#[ repr( C ) ]
26
28
pub struct GlobalBlock < A , R = ( ) > {
27
29
layout : ffi:: Block_layout ,
@@ -105,7 +107,7 @@ where
105
107
/// ```
106
108
/// use block2::global_block;
107
109
/// global_block! {
108
- /// static ADDER_BLOCK = |x: i32, y: i32, | -> i32 {
110
+ /// static ADDER_BLOCK = |x: i32, y: i32| -> i32 {
109
111
/// x + y
110
112
/// }
111
113
/// };
@@ -132,6 +134,8 @@ where
132
134
/// pub static INVALID_BLOCK = |b: Box<i32>| {}
133
135
/// };
134
136
/// ```
137
+ ///
138
+ /// [`Box`]: std::boxed::Box
135
139
#[ macro_export]
136
140
macro_rules! global_block {
137
141
// `||` is parsed as one token
Original file line number Diff line number Diff line change @@ -43,6 +43,19 @@ It is important to copy your block to the heap (with the `copy` method) before
43
43
passing it to Objective-C; this is because our `ConcreteBlock` is only meant
44
44
to be copied once, and we can enforce this in Rust, but if Objective-C code
45
45
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
+ ```
46
59
*/
47
60
48
61
#![ no_std]
You can’t perform that action at this time.
0 commit comments