This is a fairly small issue I have with booleans - often I want to toggle some boolean (if it’s true, set to false, if it’s false, set to true), but it happens that the boolean is fairly nested. So I have to type something like:
a_very_long_struct.my_field.my_nested_field.data.state[idx].blah = !a_very_long_struct.my_field.my_nested_field.data.state[idx].blah;
What I’d like to have is some kind of .invert() or .toggle() function on the boolean type itself, so I don’t have to write the whole path a second time:
a_very_long_struct.my_field.my_nested_field.data.state[idx].blah.invert();
Of course I can write this function myself or use a &mut bool in front, but neither are very clean solutions:
{
let x = &mut a_very_long_struct.my_field.my_nested_field.data.state[idx].blah;
*x != *x;
} // borrow checker - need to release &mut on a_very_long_struct
or with an external crate:
fn toggle(data: &mut bool) {
*data != *data;
}
use my_crate::toggle;
toggle(&mut a_very_long_struct.my_field.my_nested_field.data.state[idx].blah);
While I can create a function like that myself, it’s a bit annoying not to have it in the standard library or implemented directly on the bool type, so I wanted to ask if it would be a good idea to implement this.