Skip to content

Getting &T to work in blocks #837

@madsmtm

Description

@madsmtm

Ideally, we'd be able to write:

pub fn takes_block(block: &Block<dyn Fn(&NSString) + '_>) {
    let string = NSString::from_str("foo");
    block.call((&string,));
}

And call that with:

takes_block(&RcBlock::new(|string| {
    println!("{string}");
}));

Currently, we map such blocks with NonNull<NSString> instead, and users must do unsafe { string.as_ref() } before accessing it.

You'd think that the impl<T> IntoBlock<dyn Fn(T)> for Fn(T) { ... } that we currently have would be enough, but unfortunately, the lifetime in fn(&T) is higher-ranked, Fn(&T) desugars to for<'a> Fn(&'a T). Ideally, we'd instead write something like impl IntoBlock<dyn for<T> Fn(T)> for for<T> Fn(T), i.e. make T higher-ranked, but that's an incomplete feature in the compiler, so likely a long ways away from being possible.

Also discussed in #168, the solution that cidre goes with is unsound IIRC.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-block2Affects the `block2` crateA-frameworkAffects the framework crates and the translator for thembugSomething isn't working

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions