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

Support of lifetimes in message struct #18

Open
paaaav opened this issue Jun 10, 2019 · 2 comments
Open

Support of lifetimes in message struct #18

paaaav opened this issue Jun 10, 2019 · 2 comments

Comments

@paaaav
Copy link

paaaav commented Jun 10, 2019

Hello,

I'd like to specify lifetimes for a message. Manually implementing them works fine, so this is something the parser could support. See this reference

how it looks if done manually:

pub struct DropItem<'a> {
	bang: &'a mut Bang,
}

pub enum WeightMessages<'a> {
	DropItem(DropItem<'a>),
	TakeItem(TakeItem),
}

this library

pub struct DropItem<'a> {
	bang: &'a mut Bang,
}

transitions!(Weight,
  [
    (IsTaken, DropItem<'a>) => IsDropped,
    (IsDropped, DropItem<'a>) => IsDropped, 
    (IsDropped, TakeItem) => IsTaken,
    (IsTaken, TakeItem) => IsTaken
  ]
);

Thank you

@Geal
Copy link
Collaborator

Geal commented Jun 14, 2019

hello, this should work with d5b806a
Additionally, it can accept generic types, as you can see in https://github.com/rust-bakery/machine/blob/master/tests/traffic_light.rs
I would advise that you do not abuse this feature, though, because I'm not sure it can work with all syntaxes (as an example, trait bounds would probably fail)

@paaaav
Copy link
Author

paaaav commented Aug 4, 2019

Hi Geal,
this is a great start. Unfortunately I'm still keeping my own branch where I've completely removed this specific derive:

--- a/src/lib.rs
+++ b/src/lib.rs
@@ -724,7 +724,6 @@ pub fn transitions(input: proc_macro::TokenStream) -> syn::export::TokenStream {
 
     // define the state enum
     let toks = quote! {
-      #[derive(Clone,Debug,PartialEq)]
       pub enum #message_enum_ident #type_arg_toks {
         #(#variants_names(#structs_names)),*
       }

Why?

Let's say I have:

pub struct DropItem<'a> {
	object1: &'a mut Object,
	object2: &'a mut Object,
	component: &'a mut dyn Component,
}

then I need to implement Debug, Clone and PartialEq (Eq) for all of them, for every message struct. This is a lot of boilerplate. And then I still have the problem I can't do
component: &'a mut dyn Component+Clone, or deriving Clone for a trait object at all.

So that's why I've removed the derive. I haven't needed it so far.
I'm grateful that you've maintained this create and I don't know how other people would solve this -- could this derive be optional?
Thanks

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