Re: [PATCH v7] rust: add new macro for common bitmap operations

From: Kari Argillander
Date: Thu Jan 01 2026 - 17:56:36 EST


to 1.1.2026 klo 23.39 Kari Argillander (kari.argillander@xxxxxxxxx) kirjoitti:
>
> to 1.1.2026 klo 22.23 Daniel Almeida (daniel.almeida@xxxxxxxxxxxxx) kirjoitti:
> > > On 1 Jan 2026, at 16:14, Kari Argillander <kari.argillander@xxxxxxxxx> wrote:
> > >
> > > On Thu, 1 Jan 2026 at 20:21, Filipe Xavier <felipeaggger@xxxxxxxxx> wrote:
> > >
> > >> +/// // Combine multiple permissions using operation OR (`|`).
> > >> +/// let read_write: Permissions = Permission::Read | Permission::Write;
> > >> +///
> > >> +/// assert!(read_write.contains(Permission::Read));
> > >> +/// assert!(read_write.contains(Permission::Write));
> > >> +/// assert!(!read_write.contains(Permission::Execute));
> > >> +/// assert!(read_write.contains_any(Permission::Read | Permission::Execute));
> > >> +/// assert!(read_write.contains_all(Permission::Read | Permission::Write));
> > >> +///
> > >> +/// // Removing a permission with operation AND (`&`).
> > >> +/// let read_only: Permissions = read_write & Permission::Read;
> > >> +/// assert!(read_only.contains(Permission::Read));
> > >> +/// assert!(!read_only.contains(Permission::Write));
> > >> +///
> > >> +/// // Toggling permissions with XOR (`^`).
> > >> +/// let toggled: Permissions = read_only ^ Permission::Read;
> > >> +/// assert!(!toggled.contains(Permission::Read));
> > >> +///
> > >> +/// // Inverting permissions with negation (`!`).
> > >> +/// let negated = !read_only;
> > >> +/// assert!(negated.contains(Permission::Write));
> > >> +/// assert!(!negated.contains(Permission::Read));
> > >
> > > I really like the OR (`|`) operator. Personally, I don’t like anything
> > > else. When
> > > a function or expression gets longer, something like let negated = !read_only;
> > > already feels strange to read.
> > >
> > > My suggestion is that everything else should be functions, so we would write:
> > >
> > > ```rust
> > > let xxx = Permission::Read | Permission::Write;
> > > xxx.remove(Permission::Write | Permission::Read);
> > > xxx.toggle(Permission::Execute);
> > > xxx.invert();
> > > ```
> > >
> > > This way we avoid expressions like:
> > >
> > > ```rust
> > > let a = !(!xxx | yyy ^ zzz);
> > > ```
> > >
> > “
> > > At that point, no one understands what is really happening anymore. People start
> > > wondering whether operator precedence or evaluation order matters, and may even
> > > have to go read the source code to figure it out.
> >
> > I disagree. How would that look like given your suggestion?
> >
> > >
> > > By only supporting OR (|) as an operator and expressing everything else as
> > > explicit function calls, the code becomes much easier to read, review, and
> > > maintain.
> > >
> > > Argillander
> >
> > How is A | B readable but not A & B or A ^ B?
>
> Because that is just a way to express multiple / group of Flags and nothing
> more. And thinking of this it could be even & operator. So we only allow
> creating / enabling / disabling / toggling for a group of flags. Everything
> still needs to be explicit. So we would end up with something like
>
> ```rust
> Flags::new(A | B);
> toggle(A | B);
> disable(A | B); // Or remove() (I do not have opinion)
> enable(A | B);
>
> // I would even consider & operator here as then you read it quite nicely
> // toggle (A and B)
> toggle(A & B);
> ```

I tried implementing and my suggestion is something like this:

```rust
let mut f = Flags::new(Flag::Read & Flag::Write);

f.enable(Flag::Write & Flag::Execute)
.toggle(Flag::Write & Flag::Execute)
.invert()
.invert();

// ALL-of-these (built with &)
assert!(!f.contains(Flag::Write & Flag::Execute));

// ANY-of-these (built with |)
// Could also be contains_any() (Personally not fan).
// Or we can just do f.contains(Flag::Read) || f.contains(Flag::Execute)
// But this is quite nice imo
assert!(f.contains(Flag::Read | Flag::Execute));

// Or single flag
assert!(f.contains(Flag::Read));

// By design these won't build as does not make sense.
// let f = Flags::new(Flag::Read | Flag::Write);
// f.enable(Flag::Read | Flag::Write);
// f.toggle(Flag::Read | Flag::Write);
```

and you can try it yourself if wanted in [1]. I only played with the interface
and this is not ready for the kernel by any means.

[1]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=b90f6e6e92b02ed6b90cbf7e094e5d48