Skip to content

longfangsong/union_type

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

union_type

license Crates.io Docs.rs

Add union type support to rust!

Why we need this?

See here.

What's the result look like?

#[macro_use]
extern crate union_type;

use std::convert::TryInto;
use std::fmt::Display;

#[derive(Debug, Clone)]
struct A(String);

impl A {
    fn f(&self, a: i32) -> i32 {
        println!("from A {}", a + 1);
        a + 1
    }

    fn g<T: Display>(&self, t: T) -> String {
        self.0.clone() + &format!("{}", t)
    }
}

#[derive(Debug, Clone)]
struct B(i32);

impl B {
    fn f(&self, a: i32) -> i32 {
        println!("from B {}", a + self.0);
        a + self.0
    }

    fn g<T: Display>(&self, t: T) -> String {
        format!("{}:{}", self.0, t)
    }
}

union_type! {
    #[derive(Debug, Clone)]
    enum C {
        A,
        B
    }
    impl C {
        fn f(&self, a: i32) -> i32;
        fn g<T: Display>(&self, t: T) -> String;
    }
}

fn main() {
    let a = A("abc".to_string());
    let mut c: C = a.into();
    c.f(1);
    let b = B(99);
    c = b.into();
    c.f(2);
    println!("{:?}", c);
    println!("{}", c.g(99));
    let b: B = c.try_into().unwrap();
    println!("{:?}", b);
}

The output is:

from A 2
from B 101
B(B(99))
99:99
B(99)

About

Add union type support to rust!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages