-
Notifications
You must be signed in to change notification settings - Fork 90
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
Lifetime support? #21
Comments
Thanks for your feedback. I'll look into it when I find some spare time. |
Colin, I remember there were some issues with matching lifetimes in macros Colin Kiegel notifications@github.com schrieb am Mo. 15. Aug. 2016 um
|
Yeah, I remember the same thing, i.e. we can't match specifically on On Mon, Aug 15, 2016 at 9:45 AM Pascal Hertleif notifications@github.com
|
Hm, looks like I was to quick on this. I was sure I tested this on stable and beta, too - but travis proves me wrong... So this is working on nightly, but not on beta + stable yet. https://travis-ci.org/colin-kiegel/rust-derive-builder/jobs/152447585#L253 Rust expects an identifier when expanding a generic struct, and we can't coerce a lifetime into an identifier. Too bad. This looks like a bug which has been fixed in rustc 1.12, i.e. we have to wait for 2 releases. I'll revert the change and rebase my pull request (again) later. @killercup: SORRY about the little mess ^^ |
@mre: You are lucky - there is a workaround with derive_builder v0.1.0 and stable rust and it is not too bad actually. Just use a generic struct A full example looks like this #[macro_use] extern crate custom_derive;
#[macro_use] extern crate derive_builder;
custom_derive! {
#[derive(Debug, Builder)]
struct Channel<T> {
token: T,
special_info: i32,
// .. a whole bunch of other fields ..
}
}
impl<T> Channel<T> {
pub fn new<X: Into<T>>(token: X) -> Self {
Channel {
token: token.into(),
special_info: 0,
}
}
}
impl<T: ::std::fmt::Display> Channel<T> {
// All that's left to do for you is writing a method,
// which actually *does* something. :-)
pub fn build(&self) -> String {
format!("{}. The meaning of life is {}.", self.token, self.special_info)
}
}
fn main() {
let ch: Channel<&'static str> = Channel::new("Hello world!");
let x: String = ch.special_info(42u8).build();
println!("{:?}", x);
} |
@colin-kiegel thanks for that! I will try it out as soon as I get a chance. |
@mre: Yes, the workaround supports multiple lifetimes, because the lifetime is hidden in a type parameter: You can both
However, you can only access methods of your generic type, if they are defined in a trait and if you add the appropriate trait boundary (e.g. the boundary |
Nice to know. Thanks for your work. |
reopening .. github didn't get the meaning of 'revert fix #21' ^^ Btw. the fix currently works on nightly and beta (=rustc 1.12). I.e. with the next stable release we should be able to roll it out. :-) |
What's the status on this? Can fda318a be used with rust 1.13? |
The patch on `src/derive_builder.rs` is quite straightforward, but the test had to be adapted a little. Mainly, the `Lorem` struct had to derive `Clone` and the created variable must call `clone()`, like it is done in `tests/generic_structs.rs`. See issue colin-kiegel#21. Original commit: fda318a Reverting commit: 5b44190.
v0.3.0 released |
Just tried using rust-derive-builder for a struct with lifetimes:
I get the following error:
Can lifetimes be easily added? If not, are there any workarounds?
The text was updated successfully, but these errors were encountered: