-
Notifications
You must be signed in to change notification settings - Fork 3
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
Unable to use bit structs in a read-only context. #17
Comments
When I started making You can always copy honestly, I'd argue that since proc macros are much better supported in IDEs we could use proc macros for a lot of this and release a major breaking change where we rework everything. What are your thoughts regarding this? |
So you mean like this? use bit_struct::*;
impl Display for Halfling {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let temp = &mut self.clone();
writeln!(f, "The halves together make {}", temp.first_half.get().value() + temp.second_half.get().value())
}
} I mean it does work but it does look a little funky. I don't see how proc macros could help here, but then again I'm not experienced in writing them at all, so if you have an idea go for it! I can't think of a reasonable syntax for separating get and set just yet but I think I'll sleep on it :) |
I think the issue with not being able to have a Also I think you can do fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let temp = *self;
writeln!(f, "The halves together make {}", temp.first_half.get().value() + temp.second_half.get().value()) which is slightly cleaner |
That does work for the time being. |
Good afternoon! I've been writing something heavily using this library but I came across a roadblock.
Problem
The way
GetSet
are created means that I need mutability of a bit struct to be able to read from it, which is not necessary nor possible in some cases.Example
This will not compile because
.first_half()
and.second_half()
take&mut self
to make theirGetSet
s, which the signature ofDisplay::fmt
doesn't permit. In this use case I don't need to modify theHalfling
at all and can't, but it insists it must be mutable.Solution?
I understand why
GetSet
needs to be constructed with a mutable reference, so I propose aGet
or similarly named struct with read-only privileges that will work in this context. How that would work, I'm not sure.By convention it would make sense to change all generated field
GetSet
functions to mut_[field name] and make the existing ones returnGet
structs instead, but this would break everything wholesale. How would you prefer to implement this? I would be willing to do the legwork in a PR.The text was updated successfully, but these errors were encountered: