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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
::cfg_if::cfg_if![ if #[cfg(feature = "proc-macro-hygiene")]
{
#[cfg(any())]
mod objective {
macro_rules! identity_non_null {(
$input_bytestr_literal:expr
) => (
if $input_bytestr_literal.contains(&0) {
compile_error!("input contains a null byte")
} else {
$input_bytestr_literal
}
)}
}
#[doc(hidden)]
#[proc_macro]
pub fn identity_non_null (
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream
{
let input_expr = syn::parse_macro_input!(input as syn::Expr);
match input_expr {
// if the input expression is a byte string literal,
// return it as is iff it contains no null byte, else fail
syn::Expr::Lit(
syn::ExprLit {
lit: syn::Lit::ByteStr(ref bytes),
..
}
) => {
if bytes.value().contains(&0) {
throw!(input_expr.span()=>
"input contains a null byte"
);
} else {
proc_macro::TokenStream::from(quote::quote! {
input_expr
})
}
},
// else, the macro was misused
_ => throw!(input_expr.span()=>
"expected a byte string literal"
),
}
}
}
else
{
#[cfg(any())]
mod objective {
macro_rules! const_identity_non_null {(
const $const_literal_name:ident = identity_non_null!(
$input_bytestr_literal:expr
);
) => (
const $const_literal_name: &[u8; _] = b"_";
)}
}
struct ConstIdentityNonNull {
const_literal_name: syn::Ident,
input_bytestr_literal: syn::Expr,
}
impl Parse for ConstIdentityNonNull
{
fn parse (input: syn::parse::ParseStream) -> syn::parse::Result<Self>
{
macro_rules! parse_token {[$tt:tt] => (
input.parse::<syn::Token![$tt]>()?
)}
parse_token![ const ];
let const_literal_name: syn::Ident = input.parse()?;
parse_token![ = ];
input.parse::<kw::identity_non_null>()?;
parse_token![ ! ];
let input_bytestr_literal: syn::Expr = syn::group::
parse_parens(&input)?
.content
.parse()?
;
parse_token![ ; ];
Ok(ConstIdentityNonNull {
const_literal_name,
input_bytestr_literal,
})
}
}
#[proc_macro]
pub fn const_identity_non_null (
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream
{
let ConstIdentityNonNull {
const_literal_name,
input_bytestr_literal: input_expr,
} = syn::parse_macro_input!(input);
match input_expr {
// if the input expression is a byte string literal,
// return it as is iff it contains no null byte, else fail
syn::Expr::Lit(
syn::ExprLit {
lit: syn::Lit::ByteStr(ref bytes),
..
}
) => {
if bytes.value().contains(&0) {
throw!(input_expr.span()=>
"input contains a null byte"
);
} else {
let len = syn::Expr::
Lit(syn::ExprLit {
attrs: vec![],
lit: syn::Lit::Int(syn::LitInt::new(
// value
bytes.value().len() as u64,
// suffix
syn::IntSuffix::Usize,
// Span
input_expr.span().clone(),
))
})
;
proc_macro::TokenStream::from(quote::quote! {
const #const_literal_name
: &'static [u8; #len]
= #input_expr
;
})
}
},
// else, the macro was misused
_ => throw!(input_expr.span()=>
"expected a byte string literal"
),
}
}
}];