Skip to content

Commit 1b4af89

Browse files
committed
Prepare block2 for future breaking changes
I plan to convert `Block<dyn Fn() + '_>` to `Block<'_, fn()>` to allow for parameters with lifetimes (the Fn trait is invariant, while fn() types have variance). Doing this will require breaking changes in block2, so to prepare for that, we introduce the helper `DynBlock`, which should allow converting `DynBlock<dyn Fn() + '_>` to `Block<'_, fn()>` in the next version.
1 parent b9d0d9a commit 1b4af89

File tree

5 files changed

+11
-5
lines changed

5 files changed

+11
-5
lines changed

crates/block2/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,9 @@ pub type ConcreteBlock<A, R, Closure> = StackBlock<'static, A, R, Closure>;
449449
// just using `Box`, `Rc` or `Arc`, and since `__block` variables are
450450
// basically never exposed as part of a (public) function's API, we won't
451451
// implement such a thing yet.
452+
453+
/// Helper type to allow changing [`Block`] in the future without affecting
454+
/// framework crates.
455+
///
456+
/// Tracked in [#572](https://github.com/madsmtm/objc2/issues/572).
457+
pub type DynBlock<F> = crate::Block<F>;

crates/header-translator/src/id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ impl ItemIdentifier {
433433
match self.0.location.library_name() {
434434
"__bitflags__" => write!(f, "bitflags::{}", self.0.name),
435435
"__libc__" => write!(f, "libc::{}", self.0.name),
436-
"block" => write!(f, "block2::{}", self.0.name),
436+
"block" => write!(f, "block2::Dyn{}", self.0.name),
437437
_ => write!(f, "{}", self.0.name),
438438
}
439439
}

crates/header-translator/src/rust_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1988,7 +1988,7 @@ impl Ty {
19881988
arguments,
19891989
result_type,
19901990
} => {
1991-
write!(f, "block2::Block<dyn Fn(")?;
1991+
write!(f, "block2::DynBlock<dyn Fn(")?;
19921992
for arg in arguments {
19931993
write!(f, "{}, ", arg.plain())?;
19941994
}

framework-crates/objc2-foundation/src/data.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,15 @@ impl RetainedFromIterator<u8> for NSMutableData {
326326
unsafe fn with_vec<T: objc2::Message>(obj: objc2::rc::Allocated<T>, bytes: Vec<u8>) -> Retained<T> {
327327
use core::mem::ManuallyDrop;
328328

329-
use block2::{Block, RcBlock};
329+
use block2::{DynBlock, RcBlock};
330330

331331
let capacity = bytes.capacity();
332332

333333
let dealloc = RcBlock::new(move |bytes: *mut c_void, len: usize| {
334334
// Recreate the Vec and let it drop
335335
let _ = unsafe { <Vec<u8>>::from_raw_parts(bytes.cast(), len, capacity) };
336336
});
337-
let dealloc: &Block<dyn Fn(*mut c_void, usize) + 'static> = &dealloc;
337+
let dealloc: &DynBlock<dyn Fn(*mut c_void, usize) + 'static> = &dealloc;
338338

339339
let mut bytes = ManuallyDrop::new(bytes);
340340
// We intentionally extract the length before we access the

generated

Submodule generated updated 810 files

0 commit comments

Comments
 (0)