How can I define struct that holds futures of function which functions will return a future

How can I define struct that holds futures of function which functions will return a future.

async fn name () {
  async { || {

  }}
}

struct Functions {
  func: Vec<Pin<Box<dyn Future<Output = dyn Fn() -> Pin<Box<dyn Future<Output = dyn Fn() -> ()>>>>>>>
}

A few tips:

  1. Use the futures::future::BoxFuture alias instead of the Pin<Box<..>> stuff.
  2. You need to wrap the dyn Fn() in a Box too (but you don't need the Pin for Fn)
1 Like

Now I am getting this error.

missing lifetime specifier
expected named lifetime parameter

I can't help you if you don't post the full error and the associated code.

But if I replace BoxFuture wtih Pin<Box<>> then its showing

found opaque type `impl futures::Future`

That's not the full error.

now the new error

expected struct `Pin`, found opaque type

That's also not the full error. You have to post full errors if you want help.

async fn fn_name () {
  async { || {

  }}
}

type CallbackType = Pin<Box<dyn Future<Output = (bool, Option<Box<dyn Fn() -> ()>>)>>>

struct Functions {
  func: Vec<Pin<Box<CallbackType>>>
}

impl Functions {
  fn add (&mut self, callback: fn() -> CallbackType) {
    self.func.push(Box::pin(callback));
  }  
}

let f = Functions{...};
f.add(fn_name)

now when I call the name function its showing expected struct Pin, found opaque type this error.

Your IDE popups do not give you the full error. To get the full error, you should run cargo build and look at its output.

  --> src/main.rs:15:20
   |
15 |     f.add(fn_name);
   |                    ^^^ expected struct `Pin`, found opaque type

Error when ran cargo build

Please include all of the error, or post a full code snippet so I can run it myself.

try to run this

When I remove async from fn_name its working but adding it gives error

That snippet is really incomplete. For example, the let f = Functions { ... } is not inside any function, and the fields are missing. Please give me an example that I can actually paste as-is without changes and run.

please give me your github profile or link so I can invite you.

@krishnatorque, if you want help, you'll need to put in more effort. I doubt @alice is willing to join your GitHub group and read all your code to understand your problem. That's a lot of effort.

Instead, you should come up with an example that reproduces your problem. The playground is really useful for that. If you give an example of you problem on the playground, someone can probably help fix it.

1 Like

Hi @alice & @bradleyharden
Here is the repo

I am new to rust.
Please let me know if you do not understand any part of the code.

What I am trying to do is create an async function which will return another async closure & the main async function will be store in vector so it can be call in for loop with await.

You don't to make a whole repo, and that's not what they asked.

You could have just shared a playground link like this one, this is just your code, I didn't touch anything apart from adding #[tokio::main] before the main function, but at least this is "workable".

Thank you @erelde

@alice @bradleyharden

Here is the link

Is there anyone that can help me out with this please?

What I am trying to do is create an async function which will return another async closure & the main async function will be store in vector so it can be call in for loop with await .

Minimal Code link is here