Help required for writing nested generic function

I am trying to write a thread pool, in which tasks for the pool is posted by tasks themselves so It should be something like this

struct Task<TaskFunc: Fn(&Self) + Send+'static>{
    func: TaskFunc,
    tx: std::sync::mpsc::Sender<Self>,
fn func<T: Fn(&Task<T>)+Send>(_t: &Task<T>){}
fn executor<TaskFunc: Fn(&Task<TaskFunc>) + Send+'static> (f: TaskFunc){}

fn main() {

for this i get an error saying

error[E0631]: type mismatch in function arguments
  --> src/
6  | fn func<T: Fn(&Task<T>)+Send>(_t: &Task<T>){
   | ------------------------------------------- found signature of `for<'r> fn(&'r Task<_>) -> _`
16 |     executor(func);
   |     -------- ^^^^ expected signature of `for<'r> fn(&'r Task<for<'s> fn(&'s Task<_>) {func::<_>}>) -> _`
   |     |
   |     required by a bound introduced by this call
note: required by a bound in `executor`
  --> src/
10 | fn executor<TaskFunc: Fn(&Task<TaskFunc>) + Send+'static> (f: TaskFunc){
   |                       ^^^^^^^^^^^^^^^^^^^ required by this bound in `executor`

For more information about this error, try `rustc --explain E0631`.
error: could not compile `tmp` due to previous error

what does this for mean? please help I couldn't understand this error

Here, since fn executor<TaskFunc> (f: TaskFunc){} is a no-op, we could discard the bound of TaskFunc, imply executor for all possible type.

Then, the problem is clear:

You should specific the type of func.

error[E0282]: type annotations needed
9 |     executor(func);
  |              ^^^^ cannot infer type of the type parameter `T` declared on the function `func`
help: consider specifying the generic argument
9 |     executor(func::<T>);
  |                  +++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.

Thanks this helped me resolve the issue, but I ended up using fn type instead of generics. It looks like

struct Task{
func: fn(&Self),
fn func1(_t:&Task) {}

// fn func2<T: Send+ Fn()>(_t: &Task<T>){}
// fn executor<T>(f: T){
// let task = Task{
// func: func1,
// tx: todo!(),
// };
// }

fn main() {
let t1 = Task{ func: func1};
let cl = |tsk:&Task|{todo!()};
let task = Task{ func: cl};
// executor(func1);

i thought cl will be closure so it won't be fn(&Task) type, little confusing

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.