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
#![allow(nonstandard_style, unused_imports)]
use ::core::{
mem,
ops::Not as _,
};
use ::proc_macro::{
TokenStream,
};
use ::proc_macro2::{
Span,
TokenStream as TokenStream2,
TokenTree as TT,
};
use ::quote::{
format_ident,
quote,
quote_spanned,
ToTokens,
};
use ::syn::{*,
parse::{Parse, Parser, ParseStream},
punctuated::Punctuated,
Result,
spanned::Spanned,
};
use input_bytes::Input;
mod input_bytes;
#[proc_macro] pub
fn concat_bytes (
input: TokenStream,
) -> TokenStream
{
concat_bytes_impl(input.into())
.unwrap_or_else(|err| {
let mut errors =
err .into_iter()
.map(|err| Error::new(
err.span(),
format_args!("`#[byte_strings::concat_bytes]`: {}", err),
))
;
let mut err = errors.next().unwrap();
errors.for_each(|cur| err.combine(cur));
err.to_compile_error()
})
.into()
}
fn concat_bytes_impl (
input: TokenStream2,
) -> Result<TokenStream2>
{
let Input(_, ref mut bytes) = parse2(input)?;
let byte_string_literal = LitByteStr::new(bytes, Span::call_site());
Ok(byte_string_literal.into_token_stream())
}
#[proc_macro] pub
fn c_str (
input: TokenStream,
) -> TokenStream
{
c_str_impl(input.into())
.unwrap_or_else(|err| {
let mut errors =
err .into_iter()
.map(|err| Error::new(
err.span(),
format_args!("`#[byte_strings::c_str]`: {}", err),
))
;
let mut err = errors.next().unwrap();
errors.for_each(|cur| err.combine(cur));
err.to_compile_error()
})
.into()
}
fn c_str_impl (
input: TokenStream2,
) -> Result<TokenStream2>
{
let Input(ref crate_, ref mut bytes) = parse2(input)?;
match bytes.iter().position(|&b| b == b'\0') {
| Some(i) if i < bytes.len() - 1 => {
return Err(Error::new(
Span::call_site(),
format!("Inner null byte at index {}", i),
));
},
| None => {
bytes.reserve_exact(1);
bytes.push(b'\0');
},
| Some(_last_byte) => {
},
}
let byte_string_literal = LitByteStr::new(bytes, Span::call_site());
Ok(quote!(
{
let stmt_expr_attr_workaround;
#[allow(unused_unsafe)] {
stmt_expr_attr_workaround = unsafe {
#crate_::__::std::ffi::CStr::from_bytes_with_nul_unchecked(
#byte_string_literal
)
};
}
stmt_expr_attr_workaround
}
))
}