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

Generic template types #11

Closed
dtolnay opened this issue Mar 6, 2017 · 12 comments
Closed

Generic template types #11

dtolnay opened this issue Mar 6, 2017 · 12 comments

Comments

@dtolnay
Copy link
Contributor

dtolnay commented Mar 6, 2017

How feasible would it be to support generic template types?

{{ t }}
#[derive(Template)]
#[template(path = "test")]
struct Test<T> {
    t: T,
}
error[E0243]: wrong number of type arguments: expected 1, found 0
 --> src/main.rs:7:10
  |
7 | #[derive(Template)]
  |          ^^^^^^^^ expected 1 type argument

error[E0243]: wrong number of type arguments: expected 1, found 0
 --> src/main.rs:7:10
  |
7 | #[derive(Template)]
  |          ^^^^^^^^ expected 1 type argument

error: aborting due to 2 previous errors
@djc
Copy link
Owner

djc commented Mar 6, 2017

Should be straightforward. I only handle the lifetimes part of Generics so far because I was lazy/test-driven.

@dtolnay
Copy link
Contributor Author

dtolnay commented Mar 6, 2017

Nice. I think as long as you detect this case and fail with a better message, this can wait until after you announce the project.

@djc
Copy link
Owner

djc commented Mar 6, 2017

Nah, might as well fix it now. Shouldn't be too hard to at least fix the simple cases.

@djc
Copy link
Owner

djc commented Mar 8, 2017

BTW, I think your test as written will not work. There needs to be a trait bound T: std::fmt::Display added, but I assume that would be acceptable. I have a fix for this which I'll try to push tonight.

@dtolnay
Copy link
Contributor Author

dtolnay commented Mar 8, 2017

Askama should infer the bound, similar to what we do in Serde. This works:

#[derive(Serialize, Deserialize)]
struct Test<T> {
    t: T,
}

@djc
Copy link
Owner

djc commented Mar 8, 2017

How does that work in Serde? I'm not sure it would work in Askama, or at least it seems like it would make Askama an order of magnitude more complex than it currently is. This is because Askama templates can require arbitrary complexity from types of fields in the template (which I think is quite a bit different from Serde).

@djc
Copy link
Owner

djc commented Mar 8, 2017

(That is to say, it would be fairly easy for this specific case, but it seems Hard for the general case.)

@dtolnay
Copy link
Contributor Author

dtolnay commented Mar 8, 2017

You're right, I did not consider that arbitrary expressions make it not feasible to infer bounds.

Either of these should work when using {{ t }}, right?

#[derive(Template)]
#[template(path = "test")]
struct Test<T: Display> {
    t: T,
}
#[derive(Template)]
#[template(path = "test")]
struct Test<T> where T: Display {
    t: T,
}

@djc
Copy link
Owner

djc commented Mar 8, 2017

Yup, I have that covered.

@djc
Copy link
Owner

djc commented Mar 8, 2017

BTW, I was using to_tokens() to copy over the trait bounds and noticed that the spacing doesn't look so nice, is that worth a bug? I can understand if it's too hard to fix generically in syn or quote.

@dtolnay
Copy link
Contributor Author

dtolnay commented Mar 8, 2017

to_tokens() is not designed to generate human-readable output. It is for handing tokens to the compiler. If the user wants to see the generated code they can run cargo expand and see pretty formatted output. Syn / quote should not need to re-implement a pretty-printer for that.

@djc djc closed this as completed in c927d51 Mar 8, 2017
@djc
Copy link
Owner

djc commented Mar 8, 2017

(Added test case in f71b2f0, which I tagged with the wrong issue number by mistake.)

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

2 participants