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

Responder trait derivation for actix-web #204

Closed
zizhengtai opened this issue Jan 25, 2019 · 3 comments
Closed

Responder trait derivation for actix-web #204

zizhengtai opened this issue Jan 25, 2019 · 3 comments

Comments

@zizhengtai
Copy link
Contributor

I'm experimenting with askama and actix-web, and noticed the Responder trait derivation sometimes doesn't work. Here is an example project:

Cargo.toml:

[package]
name = "try-askama"
version = "0.1.0"
authors = ["Z"]
edition = "2018"

[dependencies]
actix-web = "0.7"
askama = { version = "0.7", features = ["with-actix-web"] }

src/main.rs:

use actix_web::{App, HttpRequest};
use askama::Template;

#[derive(Template)]
#[template(ext = "html", source = "Hello, {{ name }}!")]
struct HelloTemplate {
    name: &'static str,
}

fn index(_: &HttpRequest) -> HelloTemplate {  // <-- Compiles
// fn index(_: &HttpRequest) -> impl Template { // <-- DOESN'T compile
    HelloTemplate { name: "John" }
}

fn main() {
    App::new().resource("/", |r| r.get().f(index));
}

If I try to make the handler return impl Template (or anything like Result<impl Template>, Box<Future<impl Template, Error>>, etc.), I get this error:

error[E0277]: the trait bound `impl askama::Template: actix_web::handler::Responder` is not satisfied
  --> src/main.rs:15:42
   |
15 |     App::new().resource("/", |r| r.get().f(index));
   |                                          ^ the trait `actix_web::handler::Responder` is not implemented for `impl askama::Template`
@zzau13
Copy link
Contributor

zzau13 commented Feb 9, 2019

The correct type of return for this case is actix_web::Responder .

use actix_web::{App, HttpRequest, Responder};
use askama::Template;

#[derive(Template)]
#[template(ext = "html", source = "Hello, {{ name }}!")]
struct HelloTemplate {
    name: &'static str,
}


fn index(_: &HttpRequest) -> impl Responder { 
    HelloTemplate { name: "John" }
}

fn main() {
    App::new().resource("/", |r| r.get().f(index));
}

As you can see in the documentation, the correct trait for generate responses for clients is Responder.

@djc Can close it?

@djc
Copy link
Owner

djc commented Feb 10, 2019

@botika good call! @zizhengtai does that work for you?

@zizhengtai
Copy link
Contributor Author

Thanks @botika @djc, this should work for me.

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

No branches or pull requests

3 participants