-
Notifications
You must be signed in to change notification settings - Fork 3
Description
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
use bit_struct::*;
bit_struct! {
// Useless struct for demonstration purposes
pub struct Halfling(u8) {
first_half: u4,
second_half: u4
}
}
impl Display for Halfling {
// note immutability of self
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
writeln!(f, "The halves together make {}", self.first_half.get().value() + self.second_half.get().value())
}
}This will not compile because .first_half() and .second_half() take &mut self to make their GetSets, which the signature of Display::fmt doesn't permit. In this use case I don't need to modify the Halfling 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 a Get 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 return Get 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.