related - Difference between bounds inside a where clause and "inline" bounds
Having trouble understanding the following.
pub fn fn1<Fut1,Fut2>(
a:impl Fn() ->Fut1,
f: impl Fn() -> Fut2)
where
Fut1:std::future::Future+'static+std::marker::Send,
Fut2:std::future::Future,
{}
pub fn fn2<Fut1,Fut2>(
a:impl Fn() ->Fut1+'static+std::marker::Send+'static,
f: impl Fn() -> Fut2)
where
Fut1:std::future::Future+'static+std::marker::Send,
Fut2:std::future::Future
{}
Apparently those 2 functions are not the same. Running fn1
in a thread requires Fut1
to implement Send
.
F: Send + 'static,
| ^^^^ required by this bound in `std::thread::spawn`
help: consider further restricting this bound
But it already does in the where
clause I thought. It seems the where
clause just says please give this kind of object and in the fn
params I need to specify the object where
is expecting. So this must be equivalent to fn1
pub fn fn3<Fut1:std::marker::Send+std::future::Future+'static,Fut2>(
a:impl Fn() ->Fut1,
f: impl Fn() -> Fut2)
where
Fut2:std::future::Future,
{}
Then I think why do I even need a where clause when I can tell the params exactly what to return
pub fn fn4<Fut2>(
a:impl Fn() ->std::marker::Send+std::future::Future+'static,
f: impl Fn() -> Fut2)
where
Fut2:std::future::Future,
{}
But then it complains somewhere
^^^^^^^^^^^ `(dyn std::marker::Send + 'static)` is not a future
Seems like fn4
1st param is returning an unsized something. Now I see why you'd use
where Fut1:std::future::Future
But I just want an explanation. All of the errors make sense but I cannot tie all the concepts these problems represent coherently. Please leave links too.
Why the compiler requires this and cannot figure this out from the examples above.
pub fn fn5<Fut1,Fut2>(
a:impl Fn() ->Fut1+'static+std::marker::Send,
f: impl Fn() -> Fut2)
where
Fut1:std::future::Future+'static+std::marker::Send,
Fut2:std::future::Future,
{}
Param a in fn5
is actually passed as a param to another function with this signature
pub fn fn6<Fut:std::future::Future+'static>(
path: &str,
process_stream:impl Fn() -> Fut
) {
process_stream
returns an unsized something with no where
clause but the compiler doesn't complain like fn3
and fn4
. Question mark.