79290828

Date: 2024-12-18 11:00:48
Score: 1.5
Natty:
Report link

Your code did not work for three reasons.

  1. SeedInput::U64 and SeedInput::String do not represent actual Rust types, they are variants of an enum.

  2. Your SeedInput enum does not carry any data, as pointed out by justinas. When you call self.to_le_bytes() inside your match expression, you are calling it on SeedInput, which is zero-sized. What you should be doing is calling it on an instance of u64.

  3. There are ownership issues with your code also. You aim to return &[u8], which would work well with the String variant, but cannot work with the u64 variant. This is because u64::to_le_bytes() returns a [u8; 8], which is then owned by the current function, SeedInput::to_bytes(). It is therefore a temporary value, and you cannot return a reference to it.

The only way to make this work is by returning a Vec<u8> from SeedInput::to_bytes().

Why?

Because

  1. you cannot return a reference, because that is not possible with u64.
  2. you cannot return an owned [u8; {const}], because String is dynamically sized, you cannot know how big of an array you will need.
enum SeedInput {
    U64(u64),
    String(String)
}

impl SeedInput {
    pub fn to_bytes(&self) -> Vec<u8> {
        match self {
            Self::U64(num) => num.to_le_bytes().to_vec(),
            Self::String(str) => str.as_bytes().to_vec()
        }
    }
}
Reasons:
  • Blacklisted phrase (1): did not work
  • Blacklisted phrase (0.5): Why?
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Levente Bokor