[rust-dev] How to write Generic traits for enums

Felix S. Klock II pnkfelix at mozilla.com
Tue Jul 22 04:25:53 PDT 2014


Aravinda (cc’ing rust-dev)-

You didn’t show us exactly what you had tried to do to get your code to work, nor did you really describe what it is you want here.

E.g. you seem to want `get_value(MyBool(true))` to return a boolean, but since `MyBool` belongs to the `MyTypes` enum, that implies that `get_value` when applied to any variant of `MyTypes` (including `MyInt` or `MyStr`) should also return a boolean … does that seem right to you?

In any case, I suspect the missing piece of the puzzle for you is that you need to write an `impl` for the type in question.  I.e. something along the lines of:

impl MyTypes {
    fn render(&self) -> String {
        match *self {
            MyBool(x) => format!("{:b}", x),
            MyStr(ref x) => x.clone(),
            MyInt(x) => format!("{:d}", x),
        }
    }
}

(except revised from an Impl for the type to being an impl of some trait for the type).

Here is a link to a playpen with your code, and with a couple of example `impl`s for enums (like the one above) tossed in, including an impl of one instance of your `Value<S>` trait.

  http://is.gd/RofN9R

There is more discussion of writing implementations that also provides an example with a simpler enum) in the Rust tutorial, see:

  http://doc.rust-lang.org/tutorial.html#methods

Cheers,
-Felix

On 22 Jul 2014, at 11:45, Aravinda VK <hallimanearavind at gmail.com> wrote:

> Hi,
> 
> I am trying to create a generic function to return value depending on the enum passed. But I don't know to create a generic trait for enum. 
> 
> In following example, print_value works but I don't know how I can write a generic get_value function to get value from enum.
> 
> #[deriving(Show)]
> enum MyTypes{
>     MyBool(bool),
>     MyStr(String),
>     MyInt(int)
> }
> 
> fn print_value(arg: MyTypes){
>     match arg{
>         MyBool(x) => println!("Bool: {}", x),
>         MyStr(x) => println!("String: {}", x),
>         MyInt(x) => println!("Int: {}", x),
>     }
> }
> 
> 
> fn main(){
>     print_value(MyBool(true));
> 
>     // Following lines not working, how to write get_value func?
>     // let a: bool = get_value(MyBool(true));
>     // println!("{}", a);
> }
> 
> 
> In case of struct it is simple,
> 
> struct MyInt {
>     value: int
> }
> 
> struct MyBool{
>     value: bool
> }
> 
> trait Value<S>{
>     fn get(&self) -> S;
> }
> 
> impl Value<int> for MyInt{
>     fn get(&self) -> int{
>         self.value
>     }
> }
> 
> impl Value<bool> for MyBool{
>     fn get(&self) -> bool{
>         self.value
>     }
> }
> 
> fn get_value<S, T: Value<S>>(arg: T) -> S{
>     arg.get()
> }
> 
> fn main(){
>     let a: bool = get_value(MyBool{value: true});
>     println!("{}", a);
> 
>     let b: int = get_value(MyInt{value: 100});
>     println!("{}", b);
> }
> 
> Please help in writing generic function for enum.
> 
> 
> 
> -- 
> Regards 
> Aravinda | ಅರವಿಂದ 
> http://aravindavk.in
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev



More information about the Rust-dev mailing list