Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpServer trait? #63

Closed
zhangzqs opened this issue Dec 13, 2023 · 1 comment
Closed

HttpServer trait? #63

zhangzqs opened this issue Dec 13, 2023 · 1 comment

Comments

@zhangzqs
Copy link

zhangzqs commented Dec 13, 2023

I found http client in embedded-svc it's a Connection wrapper

pub struct Client<C>(C);
impl<C> Client<C>
where
C: Connection,

But is there any http server trait?

reference to esp-idf-svc:
https://github.com/esp-rs/esp-idf-svc/blob/2191e60cb8b85fba5e64bd09a55028955d38792d/src/http/server.rs#L405-L412
https://github.com/esp-rs/esp-idf-svc/blob/2191e60cb8b85fba5e64bd09a55028955d38792d/src/http/server.rs#L438-L440

Maybe we can use trait code as follows?

pub trait Server<'a> {
    type Conn<'r>: Connection;
    type HttpServerError: std::error::Error;

    fn handler<H>(
        &mut self,
        uri: &str,
        method: Method,
        handler: H,
    ) -> Result<&mut Self, Self::HttpServerError>
    where
        H: for<'r> Handler<Self::Conn<'r>> + Send + 'a;

    fn fn_handler<F>(
        &mut self,
        uri: &str,
        method: Method,
        f: F,
    ) -> Result<&mut Self, Self::HttpServerError>
    where
        F: for<'r> Fn(Request<&mut Self::Conn<'r>>) -> HandlerResult + Send + 'a,
    {
        self.handler(uri, method, FnHandler::new(f))
    }
}
@ivmarkov
Copy link
Collaborator

Such a trait used to be there long time ago.

I have removed it, because this trait is impossible to implement without using alloc. And you need alloc to turn the generic H: Handler handler into a dyn Handler instance, by potentially using boxing, as in Box<dyn Handler>.

Since embedded-svc strives to have all its traits implementable in no_std and without heap allocations, I therefore removed it in later versions.

While this trait might be useful, its usage is very limited - as in it can only abstract the code that registers your handlers. Abstracting the handlers themselves is way more useful, as that way you can re-use them across many HTTP servers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants