Generic function that return different types based on an enum input

余生长醉 提交于 2020-06-28 03:43:32

问题


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 for over the input 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,
        }
    }
}

回答1:


You can't.

Sorry, but there's just no way of doing this in Rust. You'd need dependent types, which Rust doesn't have. You could perhaps return an instance of an enum that just contains two variants (one for each type). Or you could accept one callback for each "path" and let the caller decide how to resolve the problem.

fn read_register<FnU8, FnU16, R>(
    &self,
    register: Register,
    with_u8: FnU8,
    with_u16: FnU16,
) -> R
where
    FnU8: FnOnce(u8) -> R,
    FnU16: FnOnce(u16) -> R,
{
    match register {
        Register::V0 => with_u8(self.v0),
        Register::V1 => with_u8(self.v1),
        Register::V2 => with_u16(self.v2),
        Register::V3 => with_u16(self.v3),
    }
}


来源:https://stackoverflow.com/questions/43816946/generic-function-that-return-different-types-based-on-an-enum-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!