Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

rust - How do I implement a trait for an enum and its respective variants?

I'm trying to use enum variants to capture data which is heterogeneous in nature (has different collections of fields) but which is of the same "type" from a protocol perspective. However, I'm not sure how to implement subtype-specific methods and traits. Here is a minimal example of how I can create an enumeration of Data and I can use enum variant constructors to specify the types, but if I implement a trait on the variant, calling that function is not something I've figured out how to do.

use std::fmt;

enum Data {
    N(NData),
    S(SData),
}

struct NData {
    numeric: u32,
}

impl fmt::Display for NData {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.numeric)
    }
}

struct SData {
    stringy: Vec<String>,
}

fn main() {
    let d_n: Data = Data::N(NData { numeric: 0x0 });
    let n = NData { numeric: 0xff };

    // Fails, fmt::Display not implemented for Data
    println!("{}", d_n);

    // Just fine!
    println!("{}", n);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One possible solution could be to implement your trait for the variants as well as for the enum, which as you can see here only calls the specific implementations of the variants:

use std::fmt;

struct NData {
    numeric: u32,
}

impl fmt::Display for NData {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.numeric)
    }
}

struct SData {
    strings: Vec<String>,
}

impl fmt::Display for SData {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.strings)
    }
}

enum Data {
    N(NData),
    S(SData),
}

impl fmt::Display for Data {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Data::N(n_data) => n_data.fmt(f),
            Data::S(s_data) => s_data.fmt(f),
        }
    }
}

fn main() {
    let n = NData { numeric: 0xff };
    let s = SData { strings: vec!["hello".to_string(), "world".to_string()] };

    println!("{}", n);
    println!("{}", s);

    let d_n = Data::N(n);
    let d_s = Data::S(s);

    println!("{}", d_n);
    println!("{}", d_s);
}

Which will produce the following output:

255
["hello", "world"]
255
["hello", "world"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...