Your code did not work for three reasons.
SeedInput::U64
and SeedInput::String
do not represent actual Rust types, they are variants of an enum.
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
.
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
u64
.[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()
}
}
}