-
Notifications
You must be signed in to change notification settings - Fork 569
Remove lenses
module from derived lenses
#340
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
Conversation
#341 is an even better solution to this problem. |
Does it make sense for this to get merged anyway, while we discuss #341? One other option for this, possible, would be to generate |
Ooh, that would work around the hygiene issues, at the cost of making the types anonymous. |
Yea, I'm not sure that's a huge concern? To do associated types we'd need to create a trait for each lens? |
There's no way to do associated types without polluting the namespace with extraneous type and/or module definitions, which isn't necessarily a disaster but makes for bad error messages. |
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 think we should get this in for now to unblock, and then we can go from there.
looks like this needs a rebase though 😢 |
a94916d
to
1acfb32
Compare
lenses
module from derived lenses14c618f
to
7cab11a
Compare
Added a different approach, taking advantage of the loophole in rustc's public-in-private check that lets through things that are public only in a private module. This lets us use associated constants which, while still lacking externally accessible type names, are very convenient to reference. |
Hmm, error messages still aren't great:
What do you folks think? |
Allows lenses to be derived for multiple structs in a single module without generating illegal duplicate module definitions.
druid-derive/src/lens.rs
Outdated
let associated_items = fields.iter().map(|f| { | ||
let field_name = &f.ident; | ||
quote! { | ||
pub const #field_name: #field_name = #field_name; |
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.
If this is only ever accessed via the associated const, we're less concerned about having the actual name of the struct match the name of the field. I'm thinking about the possible hygene concerns about having a struct named, sat, value
or inner
or similar just sort of hanging out in the top level of a module? one option could be to create a per-type module to stash these in, so that you have something like,
//
#[derive(Lens)]
struct MyData {
value: usize,
name: String,
}
pub mod my_data_lenses {
pub struct name;
pub struct value;
}
impl MyData {
const name: super::my_data_lenses::name = super::my_data_lenses::name;
}
again not sure this is necessary, just thinking out loud.
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.
That's actually exactly what this does! Rolling back to the original commit for now, though, since it's an unambiguous win. Will open a new PR for further discussion.
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.
okay this comment may be misguided, ignore as needed :)
lenses
module from derived lenses
I think lenses are going to necessarily have some issues around how errors are presented. A lot of this will come down to how we explain lenses in the docs. I think it's fair to expect that anybody who actually uses lenses will have had to read something; and as long as our error message can convey that the issue is with a generated lens, so that the user knows what questions to ask, I think that's acceptable. |
Allows lenses to be derived for multiple structs in a single module without generating illegal duplicate module definitions.
I'd have preferred to make the lenses associated types, but there's no current way to accomplish that without also introducing ugly generated type names into the namespace that would inevitably pop up in error messages.