-
Notifications
You must be signed in to change notification settings - Fork 4
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
Added Enum to ViewGroup derive macro #21
Added Enum to ViewGroup derive macro #21
Conversation
Thanks for working on this! I think we shouldn't introduce a new macro and instead we should include enums in I'm not sure how I feel about Lastly, you don't need randomness and 5 enum variants in the example. I would suggest to keep it simple and easy to read, as it serves as a guide for users. |
macros/src/lib.rs
Outdated
) | ||
} | ||
Fields::Unit => { | ||
panic!("Can't have unit enum. Contents of enum must impl View + Drawable") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, it might make sense to support units, I think, in case you don't want to display anything for a particular case. Is there any difficulty here besides generating code that does nothing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would units just return a dummy view? I know that ViewGroups must have a length of at least 1 or the library will crash. Is there a View struct that currently exists is just a placeholder/dummy? Since we are returning a ref how would we keep that value alive for the lifetime of the enum? I would imagine it would look something like this
impl embedded_layout::view_group::ViewGroup for ... {
fn len(&self) -> usize {
match self {
...
Self::UnitEnum => 1,
}
}
fn at(&self, index: usize) -> &dyn embedded_layout::View {
match self {
...
Self::UnitEnum => {
match index {
1 => // What do we return here, a dummy view?
_ => panic!(),
}
}
}
}
fn at_mut(&mut self, index: usize) -> &mut dyn embedded_layout::View {
match self {
...
Self::UnitEnum => {
match index {
1 => // What do we return here, a dummy view?
_ => panic!(),
}
}
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bugadani I didn't address this in the code yet. Was waiting to see what you had to say about how to implement a view that has a no-op draw function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or the library will crash
Oh nevermind then, I'll do this later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposed to implementing Clone
for LinearLayout
, but it looks like it's not necessary for this PR. We can keep it, but ideally it would be nicer to add it in a different PR.
macros/src/lib.rs
Outdated
Self::#variant_name { #(#field_idents,)* } => { | ||
match index { | ||
#(#fields_index)* | ||
_ => panic!("Invalid index!"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to pick a panic message that is a bit more informative. Also, this message should be:
- generated as an
#[inline(never)]
function to avoid including the string multiple times - it should be the same as the message for
Unnamed
variants
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@StefanBossbaly much better, thanks! This comment and the one about unit enums still stand, though. I can do these changes if you want me to, let me know if you do :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bugadani This comment should be addressed. Both at()
and at_mut()
are now #[inline(never)]
and return the same message (Invalid index! Index exceeds the value returned by len()
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I don't think I communicated very well. But never mind, I don't want to force you to a lengthy back-and-forth on this.
Currently works for both Named and Unamed structures. Right now all fields have to implement View + Drawable. In addition they have to implement Clone + Copy otherwise Transform impl will fail since translate will attempt to move out a value behind a shared reference. This makes it currently incompatible with LinerarLayout. Fixes bugadani#20
689f52e
to
5951bbe
Compare
Move code into the current ViewGroup macro.
Removed RNG. Sorry got a bit carried away 😄
Moved. |
First cut of the Enum Macro.
Currently it works on named and unamed enum variants. Each field must implement View + Drawable. I included an example that use a random number generator (
tinyrng
to stick to theno-std
depandancies) to pick what variant should be displayed to prove that any can be selected.One of the problems I am currently running into is that it does now work with LinearLayout. The problem is that LinearLayout does not implement Clone + Copy which causes the impl Transform to fail since the translate is given a
&self
and usally calls translate which accepts amut self
. Have to see if there is another way to implement it but it seems like I will need to add Clone + Copy to LinearLayout (which in turn means it has to be added to Views and Chain). Not sure if that is doable on Views since it holds a&'a mut [T]
. Any feedback/suggestions is greatly appericated. Thanks.