Closed MrJohz closed 3 years ago
This is something the derive macro doesn't handle yet. The correct use, I believe, would be:
#[derive(ramhorns::Content)]
struct SamplePage {
username: String,
age: u64,
}
#[derive(ramhorns::Content)]
struct PageWithMetadata<T> { // no trait bounds here
#[ramhorns(flatten)]
page: T,
metadata: (),
}
The derive macro would then have to conditionally impl Content for PageWithMetadata<T> where T: Content
. This is not particularly hard to do, it just needs to keep track of generic types and make sure the bounds are applied to them in the produced code.
That makes sense. I can try and put together a pull request for that, if that would be helpful?
That makes sense. I can try and put together a pull request for that, if that would be helpful?
I'm more than happy to accept a PR for this :). The generics are currently only copied dumbly into the output stream currently. This works with lifetimes, but obviously breaks here, so that's the part needs to be more robust. Field rendering should work fine without changes I believe.
I have a use-case where I would like to wrap the content for individual pages with a generic "page + metadata" content struct (e.g. to provide i18n, script information, etc).
However, this fails with a series of fairly-indecipherable errors:
Reading briefly through the source, it seems like the generic part is not being parsed or broken down further, and simply being quoted in when necessary, which works for simple type parameters (e.g.
<T>
) but not when there are bounds on those types.I also tried this by using a
where T: ramhorns::Content
block:Is this supported at all/is there an obvious alternative way to achieve this? My other way of solving this would be by putting the metadata inside the
SamplePage
struct, but this involves more repetition and messy APIs, so I was hoping to avoid this.Thanks!