Debian Codegraph @

tests/enum_try_from.rs

raw symbols SCIP
1 /*
2 Copyright ⓒ 2015 macro-attr contributors.
3
4 Licensed under the MIT license (see LICENSE or <http://opensource.org
5 /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
6 <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
7 files in the project carrying such notice may not be copied, modified,
8 or distributed except according to those terms.
9 */
10 #[macro_use] extern crate macro_attrrust-analyzer cargo macro-attr 0.2.0 macro_attr/;
11
12 trait TryFromrust-analyzer cargo macro-attr 0.2.0 TryFrom#<Src>: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# {
13 type Errrust-analyzer cargo macro-attr 0.2.0 TryFrom#Err#;
14 fn try_fromrust-analyzer cargo macro-attr 0.2.0 TryFrom#try_from().(src: Src) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Self, Self::Errrust-analyzer cargo macro-attr 0.2.0 TryFrom#Err#>;
15 }
16
17 macro_rules! TryFromrust-analyzer cargo macro-attr 0.2.0 TryFrom! {
18 (($prim:ty) $(pub)* enum $name:ident { $($body:tt)* }) => {
19 TryFrom! {
20 @collect_variants ($name, $prim),
21 ($($body)*,) -> ()
22 }
23 };
24
25 (
26 @collect_variants ($name:ident, $prim:ty),
27 ($(,)*) -> ($($var_names:ident,)*)
28 ) => {
29 impl TryFrom<$prim> for $name {
30 type Err = $prim;
31 fn try_from(src: $prim) -> Result<$name, $prim> {
32 $(
33 if src == $name::$var_names as $prim {
34 return Ok($name::$var_names);
35 }
36 )*
37 Err(src)
38 }
39 }
40 };
41
42 (
43 @collect_variants $fixed:tt,
44 ($var:ident $(= $_val:expr)*, $($tail:tt)*) -> ($($var_names:tt)*)
45 ) => {
46 TryFrom! {
47 @collect_variants $fixed,
48 ($($tail)*) -> ($($var_names)* $var,)
49 }
50 };
51
52 (
53 @collect_variants ($name:ident),
54 ($var:ident $_struct:tt, $($tail:tt)*) -> ($($var_names:tt)*)
55 ) => {
56 const _error: () = concat!(
57 "cannot derive TryFrom for ",
58 stringify!($name),
59 ", due to non-unitary variant ",
60 stringify!($var),
61 "."
62 );
63 };
64 }
65
66 macro_attrrust-analyzer cargo macro-attr 0.2.0 macro_attr!! {
67 #[derive(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, TryFromrust-analyzer cargo macro-attr 0.2.0 TryFrom!!(u8))]
68 enum Getrust-analyzer cargo macro-attr 0.2.0 Get# { Uprust-analyzer cargo macro-attr 0.2.0 Get#Up#, Downrust-analyzer cargo macro-attr 0.2.0 Get#Down#, AllAroundrust-analyzer cargo macro-attr 0.2.0 Get#AllAround# }
69 }
70
71 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
72 fn test_try_fromrust-analyzer cargo macro-attr 0.2.0 test_try_from().() {
73 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Getrust-analyzer cargo macro-attr 0.2.0 Get#::try_fromrust-analyzer cargo macro-attr 0.2.0 impl#[Get][`TryFrom<u8>`]try_from().(0u8), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Getrust-analyzer cargo macro-attr 0.2.0 Get#::Uprust-analyzer cargo macro-attr 0.2.0 Get#Up#));
74 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Getrust-analyzer cargo macro-attr 0.2.0 Get#::try_fromrust-analyzer cargo macro-attr 0.2.0 impl#[Get][`TryFrom<u8>`]try_from().(1u8), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Getrust-analyzer cargo macro-attr 0.2.0 Get#::Downrust-analyzer cargo macro-attr 0.2.0 Get#Down#));
75 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Getrust-analyzer cargo macro-attr 0.2.0 Get#::try_fromrust-analyzer cargo macro-attr 0.2.0 impl#[Get][`TryFrom<u8>`]try_from().(2u8), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Getrust-analyzer cargo macro-attr 0.2.0 Get#::AllAroundrust-analyzer cargo macro-attr 0.2.0 Get#AllAround#));
76 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(Getrust-analyzer cargo macro-attr 0.2.0 Get#::try_fromrust-analyzer cargo macro-attr 0.2.0 impl#[Get][`TryFrom<u8>`]try_from().(3u8), Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(3u8));
77 }