Stream: general

Topic: Unsized Locals + FnOnce


Jake Goulding (Dec 10 2019 at 21:54, on Zulip):

This compiles:

#![feature(unsized_locals)]

fn pass_fn_once(do_thing: dyn FnOnce()) {
    do_thing();
}

Is there any way to actually call it though?

centril (Dec 11 2019 at 08:07, on Zulip):
#![feature(unsized_locals)]

fn pass_fn_once(do_thing: dyn FnOnce()) {
    do_thing();
}

fn main() {
    let box_lam: Box<dyn FnOnce()> = Box::new(|| dbg!());
    let lam: dyn FnOnce() = *box_lam;
    pass_fn_once(lam);
}
Jake Goulding (Dec 11 2019 at 15:21, on Zulip):

Hmm. That feels unfortunate; is there a way to do it without heap allocation? I feel like I should be able to create an unsized local in main without it.

simulacrum (Dec 11 2019 at 15:23, on Zulip):

I imagine you could possibly &mut dyn FnOnce() and then cast that to *mut and ptr::read from that to get it onto the stack

simulacrum (Dec 11 2019 at 15:23, on Zulip):

but not sure if unsized_locals and friends are good enough for that yet :)

Jake Goulding (Dec 11 2019 at 15:24, on Zulip):

Yeah, that's always a thing I struggle with for unstable features - is that part impossible / not planned to work / doesn't work yet?

centril (Dec 11 2019 at 15:26, on Zulip):

@Jake Goulding best way to figure that out is to file an issue

centril (Dec 11 2019 at 15:26, on Zulip):

(don't post on the tracking issue)

simulacrum (Dec 11 2019 at 15:26, on Zulip):

I, uh, disagree

simulacrum (Dec 11 2019 at 15:26, on Zulip):

filing an issue to ask a question isn't great

simulacrum (Dec 11 2019 at 15:26, on Zulip):

if you have a case that should work (in your opinion) but doesn't, that's another matter

centril (Dec 11 2019 at 15:27, on Zulip):

that's the way I'd ask the question

simulacrum (Dec 11 2019 at 15:27, on Zulip):

I think in the future I would hope that just let f: dyn FnOnce() = move || {}; should basically just work

Jake Goulding (Dec 11 2019 at 15:28, on Zulip):

I'd go with more chat / forum avenues before I file an issue (if I ever got to an issue, I agree it feels wrong). Here or Discord are easy places, then graduate to IRLO if not answered.

Last update: Jan 21 2020 at 08:25UTC