Closed je3f0o closed 1 week ago
From what I see the main problem is making an object safe async trait object. A typical hashmap can only store values with the same size and in your case different callback functions can have different sizes.
In other word you want to store your callbacks as Box<dyn MyService<Req>>
to unify their size into boxed trait object but this is not possible with safe Rust right now due to async trait being not object safe (you can find reference here). In xitca-web
this limitation is carefully mitigated with extra trait.xitca-web
's Router hide this trait from user but it's the trait it directly stores and interact with.
For a solution of your problem I can suggest two approaches:
a. Remove xitca-web
from your dependency and utilize xitca-http
, xitca-service
and xitca-server
directly to work with your own router type. You can reference xitca_http::util::router::Router
type and it's implements to introduce your own router logic. In general xitca-web
is just a composed application with the xitca-xxx
and http-xxx
crates inside this project. You can use this example as a reference where axum
runs on top of this approach.
b. Make a MyServiceDyn
trait like xitca_service::object::ServiceObj
to enable object safety for your MyService
trait where your router stores Box<dyn MyServiceDyn>
.
Hello,
full source code.
router.rs:
Question
This is the idea. I'm trying to implement my own router. Look example usage from
fn init()
. Bottom last fn. I'm trying to copy fromfn_service
. But my rust kung-fu is not enough (skill issue) XD. In the example secondregister(Route{...})
is interesting.If I switch
callback
comment it works. Because previous registeredRoute
and this callback have same fn signatures. Other callback generates compile time error:I don't know I'm explained well or not... But my question is how to store
FnService<F>
inHashMap
?