BitAnd where Rhs is not Self

According to the docs for core::ops::BitAnd

Note that Rhs is Self by default, but this is not mandatory.

Here's what I'm trying to accomplish:

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Bit {
    Suid = 0o4000,
    Sgid = 0o2000,
    Sticky = 0o1000,
    URead = 0o400,
    UWrite = 0o200,
    UExec = 0o100,
    GRead = 0o40,
    GWrite = 0o20,
    GExec = 0o10,
    ORead = 0o4,
    OWrite = 0o2,
    OExec = 0o1,
}

impl BitAnd for Bit {
    type Output = u32;

    fn bitand(self, rhs: u32) -> Self::Output {
        self as u32 & rhs
    }
}

The compiler does not want to accept it unless rhs is Self.

error[E0053]: method `bitand` has an incompatible type for trait
  --> src/mode/mod.rs:32:26
   |
32 |     fn bitand(self, rhs: u32) -> Self::Output {
   |                          ^^^
   |                          |
   |                          expected enum `Bit`, found `u32`
   |                          help: change the parameter type to match the trait: `Bit`
   |
   = note: expected fn pointer `fn(Bit, Bit) -> _`
              found fn pointer `fn(Bit, u32) -> _`

I realize that I can just do the cast every time I want to do any bitwise operations. I was just trying to make the code look cleaner. Is there a way to implement these comparison operator traits where Rhs is not Self (as the docs seem to imply) or should I just accept having to explicitly cast the enum for every operation?

Look at the trait definition/documentation. As the docs you quoted themselves assert, the Rhs being Self is a default. You want impl BitAnd<u32> for Bit.

Thanks. That did it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.