Return type for struct with generic function parameter

I have a struct that stores a couple generic callbacks. In my test suite, I'm trying to create a specific instance of that struct in a setup function. But I can't figure out what to return when the function callback is a closure.

I made a basic repro; the crux is, what goes in <???> below?

struct Request;

struct Tracker<F: Fn(u32) -> u32> {
    elem: u32,
    func: F,

impl<F: Fn(u32) -> u32> Tracker<F> {
    fn new(elem: u32, func: F) -> Self {
        Tracker { elem, func }

fn a_thing_tracker() -> Tracker<???> {
    Tracker::new(5, |x| x + x)

fn a_thing_tracker() -> Tracker<fn(u32) -> u32>
1 Like

With a small letter it's the type of a function pointer, with a capital letter it's a trait. Some explanation here:

Though that worked with my toy example, it gave me a "expected fn pointer, found closure" error in my own code. I really can't figure out why the toy example didn't do that too.

My solution, based on your hint was to just return Tracker<impl Fn(u32) -> u32>

Does the closure from your real code capture any variables from it's environment? I think that causes trouble.

1 Like

It is not possible to name the type of a closure. Your ??? can be impl Fn(u32) -> u32 which means some type, without naming it.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.