I have a struct which holds registers. I want my read_register
function to return a u8
for Register::V0
and Register::V1
but a u16
for Register::V2
and Register::V3
. I'm not sure how to make the function generic over the return type. I'm getting the error match arms have incompatible types
which does make sense because the types are different.
struct Registers {
v0: u8,
v1: u8,
v2: u16,
v3: u16,
}
enum Register {
V0,
V1,
V2,
V3,
}
impl Registers {
fn read_register<T>(&self, register: Register) -> T {
match register {
Register::V0 => self.v0,
Register::V1 => self.v1,
Register::V2 => self.v2,
Register::V3 => self.v3,
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…