File: derive_try_from.rs

package info (click to toggle)
rust-conv 0.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 336 kB
  • sloc: makefile: 2
file content (45 lines) | stat: -rw-r--r-- 1,318 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#[macro_use] extern crate conv;

use conv::{TryFrom, Unrepresentable};

#[derive(Debug, PartialEq)]
enum Get { Up, Down, AllAround }

TryFrom! { (u8)
    enum Get {
        Up,
        /// And
        Down,
        /** And */
        AllAround
    }
}

#[derive(Debug, PartialEq)]
enum GottaGo { GetAway, Fast = 9000, Faster = 9001 }

TryFrom! { (u16)
    enum GottaGo {
        GetAway,
        Fast = 9000,
        /// This show was stupid.
        Faster = 9001
    }
}

#[test]
fn test_try_from() {
    assert_eq!(Get::try_from(0u8), Ok(Get::Up));
    assert_eq!(Get::try_from(1u8), Ok(Get::Down));
    assert_eq!(Get::try_from(2u8), Ok(Get::AllAround));
    assert_eq!(Get::try_from(3u8), Err(Unrepresentable(3u8)));

    assert_eq!(GottaGo::try_from(0u16), Ok(GottaGo::GetAway));
    assert_eq!(GottaGo::try_from(1u16), Err(Unrepresentable(1u16)));
    assert_eq!(GottaGo::try_from(2u16), Err(Unrepresentable(2u16)));
    assert_eq!(GottaGo::try_from(3u16), Err(Unrepresentable(3u16)));
    assert_eq!(GottaGo::try_from(8999u16), Err(Unrepresentable(8999u16)));
    assert_eq!(GottaGo::try_from(9000u16), Ok(GottaGo::Fast));
    assert_eq!(GottaGo::try_from(9001u16), Ok(GottaGo::Faster));
    assert_eq!(GottaGo::try_from(9002u16), Err(Unrepresentable(9002u16)));
}