bind expects NewService but I give this structure and Result of Service instead. It is accepted by the compiler.
I thought it is a closure without a single argument, but I doubt now... I have read the book, but maybe I missed this bit
Could you please clarify what is the purpose of ||? how is the variable casting processed here to satisfy type constraints (Result of Service to NewService)? what happens under the hood?
In this signature the new_service argument can have any type S as long as S implements NewService. The rustdoc for NewService shows that there is at least one implementation:
impl<F, R> NewService for F where
F: Fn() -> Result<R, hyper::Error>,
R: Service;
So any type F can be used as an implementation of NewService, as long as F implements Fn() -> Result<R, Error> which is the trait for Rust closures that do not mutate any internal state, take 0 arguments, and return a Result<R, Error>.
The types signatures are pretty messy but to summarize:
|| Ok(HelloWorld) is a closure that takes 0 arguments and returns Result<R, hyper::Error>. The type R is inferred based on the type of HelloWorld.
Due to the impl NewService for F shown above, the closure implements the NewService trait.
Since the closure implements the NewService trait, it can be passed as the new_service argument of type S to the Http::bind method.