shio-rs
shio-rs copied to clipboard
Support for boxed trait returns
What I'd like to do is something like this, so I can use some of the struct fields within the response:
extern crate shio;
use shio::prelude::*;
use shio::Handler;
struct TestStruct {}
impl TestStruct {
pub fn test_closure(&self) -> Box<Handler<Result=Response>> {
Box::new(|_ctx| Response::with("Hello World"))
}
}
fn main() {
let test_struct = TestStruct {};
Shio::default()
.route((Method::Get, "/", test_struct.test_closure()))
.run(":7878").unwrap();
}
This would allow responses to be built up using other struct fields.
But this errors out:
error[E0277]: the trait bound `std::boxed::Box<shio::Handler<Result=shio::Response>>: std::ops::Fn<(shio::Context,)>` is not satisfied
--> src/main.rs:19:10
|
19 | .route((Method::Get, "/", test_struct.test_closure()))
| ^^^^^ the trait `std::ops::Fn<(shio::Context,)>` is not implemented for `std::boxed::Box<shio::Handler<Result=shio::Response>>`
|
= note: required because of the requirements on the impl of `shio::Handler` for `std::boxed::Box<shio::Handler<Result=shio::Response>>`
= note: required because of the requirements on the impl of `std::convert::From<(shio::Method, &str, std::boxed::Box<shio::Handler<Result=shio::Response>>)>` for `shio::router::Route`
= note: required because of the requirements on the impl of `std::convert::Into<shio::router::Route>` for `(shio::Method, &str, std::boxed::Box<shio::Handler<Result=shio::Response>>)`
error: aborting due to previous error
error: Could not compile `shiotest`.
To learn more, run the command again with --verbose.
Using the new impl trait on nightly works OK though:
#![feature(conservative_impl_trait)]
extern crate shio;
use shio::prelude::*;
use shio::Handler;
struct TestStruct {}
impl TestStruct {
pub fn test_closure(&self) -> impl Handler<Result=Response> {
|_ctx| Response::with("Hello World")
}
}
fn main() {
let test_struct = TestStruct {};
Shio::default()
.route((Method::Get, "/", test_struct.test_closure()))
.run(":7878").unwrap();
}