A little confused by the `as u32` syntax

  Kiến thức lập trình

I was creating a Bit enum for a project of mine and I experienced some weird interaction with the as u32 (or any other type) syntax. I have Into<u8, u16, .. u128> implemented for my Bit enum so I mindlessly used Bit as u32 in a test of mine, sort of assuming it was syntactic sugar for .into(). I ran some tests that failed, in trying to find the bug I spotted that my Bit enum was layed out like this:

pub enum Bit {
    On,
    Off,
}

On a whim I changed it to:

pub enum Bit {
    Off,
    On,
}

Thinking nothing of it. I reran my tests to see which test failed again and to my surprise 2 extra tests passed!

I did some extra digging, switching the On and Off back and forth and changing the as u32 into an .into() call. And it seems that as u32 completely ignores any Into implementations and just converts the bits. This sorta made sense, but how does that work for u32 as f64 for example?? You can’t simply convert the bits there. What exactly does it do?

Looking at the suggested question it seems that my Enum is instead using the TryFrom implementation (this is also implemented). But that doesn’t make sense to me, why would rust use the TryFrom implementation that might panic over the specifically implemented Into? But even that explanation doesn’t make sense because the TryFrom implementation specifically maps 0 to Bit::Off, 1 to Bit::On and every other value to an error.

Normally I’d look at the docs for this but I wouldn’t know where to find the docs for this specifically since its more of a language feature rather than an implementation of something, a pointer to that also helps!

Link to the playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c06998e915b2b4aa44d58ec67d21eabb

For reference this is where I used the as u32:

fn bits_flipped(left: &BitString, right: &BitString) -> u32 {
    assert_eq!(
        left.len(),
        right.len(),
        "the length of the bitstrings is not equal. Left is {} and right is {}",
        left.len(),
        right.len()
    );

    let mut difference: u32 = 0;
    for i in 0..left.len() {
        difference += (left[i] ^ right[i]) as u32; // This line was causing issues
    }

    difference
}

This is the macro I use for the Into implementation:

macro_rules! bit_into_type {
    ($t:ty) => {
        impl Into<$t> for Bit {
            #![allow(clippy::from_over_into)]
            fn into(self) -> $t {
                match self {
                    Self::On => 1,
                    Self::Off => 0,
                }
            }
        }
    };
}

And finally this is the macro I use for the TryFrom implementation:

macro_rules! bit_try_from {
    ($t:ty) => {
        impl TryFrom<$t> for Bit {
            type Error = String;

            fn try_from(value: $t) -> Result<Self, Self::Error> {
                match value {
                    0 => Ok(Bit::Off),
                    1 => Ok(Bit::On),
                    value => Err(format!("Cannot represent {} as a single bit", value)),
                }
            }
        }
    };
}

LEAVE A COMMENT