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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! Derives for converting primitives to and from simple enums and newtype structs.
//! This crate is useful for converting between Rust and C representations.
//!
//! ## Simple enums
//!
//! A _simple_ enum is one that does not have variants containing data. In other words, it's a
//! C-like/POD enum (as opposed to a tagged union).
//! Simple enums can be converted from unsigned integer types no larger than the `min_ty` and to
//! unsigned integer types no smaller than the `min_ty`.
//! The default `min_ty` is the smallest type that can fit the number of enum variants.
//! For example, an enum with 255 variants would have `min_ty = u8` but one with 256 would have
//! `min_ty = u16`. The `min_ty` can be configured using the `#[prim(ty = "<min_ty>")]` attribute.
//!
//! ### Example
//!
//! ```
//! #[macro_use]
//! extern crate proper;
//!
//! use std::convert::TryFrom;
//!
//! #[derive(Prim)]
//! enum EnumU8 {
//! Variant0,
//! Variant1,
//! }
//!
//! #[derive(Prim)]
//! #[prim(ty = "u16")]
//! enum EnumU16 {
//! Variant0,
//! Variant1,
//! }
//!
//! let e_u8 = EnumU8::try_from(42u8)?;
//! let e_u8 = EnumU8::try_from(42u16)?; // won't compile!
//! let e_u16 = EnumU16::try_from(42u16)?; // compiles!
//!
//! let prim = e_u8 as u32;
//! let prim = e_u16 as u8;
//! ```
//!
//! ## Newtype structs
//!
//! Newtype structs can be created from an integer type with the same sign and bitwidth
//! no larger than the contained type.
//! Inversely, newtype structs can be converted to any integer type with the same sign and bitwidth
//! as the contained type.
//! For instance, `MyType(u16)` can be created from `u8` and `u16` but not `u32`; and converted to
//! `u16` and `u32`, but not `u8`.
//!
//! ### Example
//!
//! ```
//! #[macro_use]
//! extern crate proper;
//!
//! #[derive(Prim)]
//! struct FileDescriptor(u32);
//!
//! let fd = FileDescriptor::from(42u8);
//! ```
extern crate proc_macro;
extern crate quote;
extern crate syn;
use Spanned;
use PrimAttrs;
use IntTy;