2 releases

Uses old Rust 2015

0.0.1 Nov 11, 2014
0.0.0 Nov 11, 2014

#72 in #defines

34 downloads per month

4KB
50 lines

Convertible

Checked, typesafe, ergonomic runtime conversion of types.

Overview

Defines the Convertible<From> trait, which defines a convert method from &From to Option<Self>.

Additionally, it defines the universal mixin trait Raw, which adds the to method to all types to allow for ergonomic usage of these conversions.

This can be used to implement extensible enums based on a raw representation that variants can be created from

Example

use convertible::{Convertible, Raw};

struct Raw {
    name: &'static str
}

#[deriving(PartialEq, Show)]
enum NotRaw {
    First, Second, Third
}

impl Convertible<Raw> for NotRaw {
    fn convert(raw: &Raw) -> Option<NotRaw> {
        match raw.name {
            "first" => Some(First),
            "second" => Some(Second),
            "third" => Some(Third),
            _ => None
        }
    }
}

#[test] fn test_conversion() {
    let first = Raw { name: "first" };
    let second = Raw { name: "second" };
    let malformed = Raw { name: "malformed" };

    assert_eq!(first.to::<NotRaw>(), Some(First));
    assert_eq!(second.to::<NotRaw>(), Some(Second));
    assert_eq!(malformed.to::<NotRaw>(), None);
}

No runtime deps