-
Notifications
You must be signed in to change notification settings - Fork 19
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
self as target doesn't seem to work #25
Comments
Hmm, for some reason |
@Kobzol The problem here is that it is parsing In general, you cannot really parse a braced group right after arbitrary expressions. Try adding some separator (such as impl Trait for Struct {
delegate! {
{
fn foo ...
} => self,
} Otherwise you would have to do the parsing of the part before the braces manually (e.g., with @Boscop in the meantime, you can use |
Ooh, now it actually makes sense, thank you! I'll probably parse it manually, if it won't be too problematic, because I don't want to introduce another syntactic layer. |
So I decided to special case this, because It seems that rustc inserts
Before, when I was parsing this as impl syn::parse::Parse for DelegationTarget {
fn parse(input: ParseStream) -> Result<Self, Error> {
let cursor = input.cursor().token_stream();
Ok(if syn::parse2::<syn::Expr>(cursor).is_ok() {
DelegationTarget::Expr(input.parse()?)
} else {
DelegationTarget::SelfTarget(input.parse()?)
})
}
} The problem is that the first condition doesn't succeed. I tracked it down to this: syn::parse2::<syn::Expr>(input.cursor().token_stream()).is_ok() // false
input.parse::<syn::Expr>.is_ok() // true I printed the contents of the two token streams and they seem to be the same, so it seems like there is something wrong with my usage of the |
Sorry for my late answer, I forgot about this thread. The good thing is the results you are hitting make sense in my head; and I like the idea of using a The key thing to understand about such
So, in your case, your sequence of tokens is:
Then, the difference between:
is that the former tries to parse all the given tokens as an expression, whereas the latter only tries to parse a prefix of such tokens as one. And given the capture, The solutionThe goot thing about having waited that long to keep this thread updated, is that, in the meantime, I have stumbled upon a bunch of posts from dtolnay where they mention that this is a common case of ambiguity in the language. Because of that, I have suspected there had to be one function inside
I'm pretty sure this will neatly solve your issues 😉 |
Thank you! That is exactly what I needed. Actually if I thought of using This test broke: macro_rules! some_macro {
($delegate_to:expr) => {
impl Trait for Struct2 {
delegate! {
// '$delegate' will expand to 'self.0' before ´delegate!' is expanded.
to $delegate_to {
fn method_to_delegate(&self) -> bool;
}
}
}
};
}
some_macro! {self.0} It produces
Initially I thought that it's a regression of the new |
Indeed, the A possible fix is to use: macro_rules! some_macro {
(|$self:ident| $delegate_to:expr) => {
impl Trait for Struct2 {
delegate! {
// '$delegate' will expand to 'self.0' before ´delegate!' is expanded.
to $delegate_to {
fn method_to_delegate(&$self) -> bool;
}
}
}
};
}
some_macro! { |self| self.0 }
|
Looking forward for the merge of #26 ! 😄 |
I have a derived getter, using the
getset
crate, but I also need to impl a trait that has the same getter.This works:
But this doesn't work:
But it should also work, because the handwritten delegation from trait to
self.foo()
works :)The text was updated successfully, but these errors were encountered: