I want to have an enum where each value in the enum stores a constant array of bytes that represent the RGBA values for a color. In Java I would do this:
public enum Color {
BLACK([0x0C, 0x00, 0x05, 0xFF]),
BLUE([0x00, 0x2D, 0xFF, 0xFF]),
RED([0xFF, 0x3E, 0x00, 0xFF]);
private final byte[] rgba;
Color(byte[] rgba) {
this.rgba = rgba;
}
public int[] value() {
return rgba;
}
}
Then I could pass around Color types and just use color.value() to get the bytes out. This is what I have in Rust:
struct Color;
impl Color {
pub const BLACK: [u8; 4] = [0x0C, 0x00, 0x05, 0xFF];
pub const BLUE: [u8; 4] = [0x00, 0x2D, 0xFF, 0xFF];
pub const RED: [u8; 4] = [0xFF, 0x3E, 0x00, 0xFF];
}
But this means that anywhere I want to pass a Color the type is [u8; 4]
. I could call the struct Colors
and then do pub type Color = [u8; 4]
. Then you could have the type as Color
but then it would be Colors::BLACK
which seems weird.