| 1 |
#![doc = include_str!("../README.md")] |
| 2 |
#![cfg_attr(docsrs, feature(doc_auto_cfg))] |
| 3 |
#![recursion_limit = "128"] |
| 4 |
#![cfg_attr(any(not(docsrs), ci), deny(rustdoc::all))] |
| 5 |
#![forbid(non_ascii_idents, unsafe_code)] |
| 6 |
#![warn(clippy::nonstandard_macro_braces)] |
| 7 |
|
| 8 |
use proc_macro::TokenStream; |
| 9 |
use syn::parse::Error as ParseError; |
| 10 |
|
| 11 |
mod utils; |
| 12 |
|
| 13 |
#[cfg(any(feature = "add_assign", feature = "mul_assign"))] |
| 14 |
mod add_assign_like; |
| 15 |
#[cfg(any( |
| 16 |
feature = "add", |
| 17 |
feature = "add_assign", |
| 18 |
feature = "mul", |
| 19 |
feature = "mul_assign", |
| 20 |
))] |
| 21 |
mod add_helpers; |
| 22 |
#[cfg(any(feature = "add", feature = "mul"))] |
| 23 |
mod add_like; |
| 24 |
#[cfg(feature = "as_mut")] |
| 25 |
mod as_mut; |
| 26 |
#[cfg(feature = "as_ref")] |
| 27 |
mod as_ref; |
| 28 |
#[cfg(feature = "constructor")] |
| 29 |
mod constructor; |
| 30 |
#[cfg(feature = "deref")] |
| 31 |
mod deref; |
| 32 |
#[cfg(feature = "deref_mut")] |
| 33 |
mod deref_mut; |
| 34 |
#[cfg(feature = "error")] |
| 35 |
mod error; |
| 36 |
#[cfg(any(feature = "debug", feature = "display"))] |
| 37 |
mod fmt; |
| 38 |
#[cfg(feature = "from")] |
| 39 |
mod from; |
| 40 |
#[cfg(feature = "from_str")] |
| 41 |
mod from_str; |
| 42 |
#[cfg(feature = "index")] |
| 43 |
mod index; |
| 44 |
#[cfg(feature = "index_mut")] |
| 45 |
mod index_mut; |
| 46 |
#[cfg(feature = "into")] |
| 47 |
mod into; |
| 48 |
#[cfg(feature = "into_iterator")] |
| 49 |
mod into_iterator; |
| 50 |
#[cfg(feature = "is_variant")] |
| 51 |
mod is_variant; |
| 52 |
#[cfg(feature = "mul_assign")] |
| 53 |
mod mul_assign_like; |
| 54 |
#[cfg(any(feature = "mul", feature = "mul_assign"))] |
| 55 |
mod mul_helpers; |
| 56 |
#[cfg(feature = "mul")] |
| 57 |
mod mul_like; |
| 58 |
#[cfg(feature = "not")] |
| 59 |
mod not_like; |
| 60 |
#[cfg(any( |
| 61 |
feature = "debug", |
| 62 |
feature = "display", |
| 63 |
feature = "from", |
| 64 |
feature = "into", |
| 65 |
))] |
| 66 |
pub(crate) mod parsing; |
| 67 |
#[cfg(feature = "sum")] |
| 68 |
mod sum_like; |
| 69 |
#[cfg(feature = "try_into")] |
| 70 |
mod try_into; |
| 71 |
#[cfg(feature = "try_unwrap")] |
| 72 |
mod try_unwrap; |
| 73 |
#[cfg(feature = "unwrap")] |
| 74 |
mod unwrap; |
| 75 |
|
| 76 |
// This trait describes the possible return types of |
| 77 |
// the derives. A derive can generally be infallible and |
| 78 |
// return a TokenStream, or it can be fallible and return |
| 79 |
// a Result<TokenStream, syn::parse::Error>. |
| 80 |
trait Output { |
| 81 |
fn process(self) -> TokenStream; |
| 82 |
} |
| 83 |
|
| 84 |
impl Output for proc_macro2::TokenStream { |
| 85 |
fn process(self) -> TokenStream { |
| 86 |
self.into() |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl Output for Result<proc_macro2::TokenStream, ParseError> { |
| 91 |
fn process(self) -> TokenStream { |
| 92 |
match self { |
| 93 |
Ok(ts) => ts.into(), |
| 94 |
Err(e) => e.to_compile_error().into(), |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
macro_rules! create_derive( |
| 100 |
($feature:literal, $mod_:ident $(:: $mod_rest:ident)*, $trait_:ident, $fn_name: ident $(,$attribute:ident)* $(,)?) => { |
| 101 |
#[cfg(feature = $feature)] |
| 102 |
#[proc_macro_derive($trait_, attributes($($attribute),*))] |
| 103 |
#[doc = include_str!(concat!("../doc/", $feature, ".md"))] |
| 104 |
pub fn $fn_name(input: TokenStream) -> TokenStream { |
| 105 |
let ast = syn::parse(input).unwrap(); |
| 106 |
Output::process($mod_$(:: $mod_rest)*::expand(&ast, stringify!($trait_))) |
| 107 |
} |
| 108 |
} |
| 109 |
); |
| 110 |
|
| 111 |
create_derive!("add", add_like, Add, add_derive); |
| 112 |
create_derive!("add", add_like, Sub, sub_derive); |
| 113 |
create_derive!("add", add_like, BitAnd, bit_and_derive); |
| 114 |
create_derive!("add", add_like, BitOr, bit_or_derive); |
| 115 |
create_derive!("add", add_like, BitXor, bit_xor_derive); |
| 116 |
|
| 117 |
create_derive!("add_assign", add_assign_like, AddAssign, add_assign_derive,); |
| 118 |
create_derive!("add_assign", add_assign_like, SubAssign, sub_assign_derive,); |
| 119 |
create_derive!( |
| 120 |
"add_assign", |
| 121 |
add_assign_like, |
| 122 |
BitAndAssign, |
| 123 |
bit_and_assign_derive, |
| 124 |
); |
| 125 |
create_derive!( |
| 126 |
"add_assign", |
| 127 |
add_assign_like, |
| 128 |
BitOrAssign, |
| 129 |
bit_or_assign_derive, |
| 130 |
); |
| 131 |
create_derive!( |
| 132 |
"add_assign", |
| 133 |
add_assign_like, |
| 134 |
BitXorAssign, |
| 135 |
bit_xor_assign_derive, |
| 136 |
); |
| 137 |
|
| 138 |
create_derive!("as_mut", as_mut, AsMut, as_mut_derive, as_mut); |
| 139 |
|
| 140 |
create_derive!("as_ref", as_ref, AsRef, as_ref_derive, as_ref); |
| 141 |
|
| 142 |
create_derive!("constructor", constructor, Constructor, constructor_derive); |
| 143 |
|
| 144 |
create_derive!("debug", fmt::debug, Debug, debug_derive, debug); |
| 145 |
|
| 146 |
create_derive!("deref", deref, Deref, deref_derive, deref); |
| 147 |
|
| 148 |
create_derive!( |
| 149 |
"deref_mut", |
| 150 |
deref_mut, |
| 151 |
DerefMut, |
| 152 |
deref_mut_derive, |
| 153 |
deref_mut, |
| 154 |
); |
| 155 |
|
| 156 |
create_derive!("display", fmt::display, Display, display_derive, display); |
| 157 |
create_derive!("display", fmt::display, Binary, binary_derive, binary); |
| 158 |
create_derive!("display", fmt::display, Octal, octal_derive, octal); |
| 159 |
create_derive!( |
| 160 |
"display", |
| 161 |
fmt::display, |
| 162 |
LowerHex, |
| 163 |
lower_hex_derive, |
| 164 |
lower_hex, |
| 165 |
); |
| 166 |
create_derive!( |
| 167 |
"display", |
| 168 |
fmt::display, |
| 169 |
UpperHex, |
| 170 |
upper_hex_derive, |
| 171 |
upper_hex, |
| 172 |
); |
| 173 |
create_derive!( |
| 174 |
"display", |
| 175 |
fmt::display, |
| 176 |
LowerExp, |
| 177 |
lower_exp_derive, |
| 178 |
lower_exp, |
| 179 |
); |
| 180 |
create_derive!( |
| 181 |
"display", |
| 182 |
fmt::display, |
| 183 |
UpperExp, |
| 184 |
upper_exp_derive, |
| 185 |
upper_exp, |
| 186 |
); |
| 187 |
create_derive!("display", fmt::display, Pointer, pointer_derive, pointer); |
| 188 |
|
| 189 |
create_derive!("error", error, Error, error_derive, error); |
| 190 |
|
| 191 |
create_derive!("from", from, From, from_derive, from); |
| 192 |
|
| 193 |
create_derive!("from_str", from_str, FromStr, from_str_derive); |
| 194 |
|
| 195 |
create_derive!("index", index, Index, index_derive, index); |
| 196 |
|
| 197 |
create_derive!( |
| 198 |
"index_mut", |
| 199 |
index_mut, |
| 200 |
IndexMut, |
| 201 |
index_mut_derive, |
| 202 |
index_mut, |
| 203 |
); |
| 204 |
|
| 205 |
create_derive!("into", into, Into, into_derive, into); |
| 206 |
|
| 207 |
create_derive!( |
| 208 |
"into_iterator", |
| 209 |
into_iterator, |
| 210 |
IntoIterator, |
| 211 |
into_iterator_derive, |
| 212 |
into_iterator, |
| 213 |
); |
| 214 |
|
| 215 |
create_derive!( |
| 216 |
"is_variant", |
| 217 |
is_variant, |
| 218 |
IsVariant, |
| 219 |
is_variant_derive, |
| 220 |
is_variant, |
| 221 |
); |
| 222 |
|
| 223 |
create_derive!("mul", mul_like, Mul, mul_derive, mul); |
| 224 |
create_derive!("mul", mul_like, Div, div_derive, div); |
| 225 |
create_derive!("mul", mul_like, Rem, rem_derive, rem); |
| 226 |
create_derive!("mul", mul_like, Shr, shr_derive, shr); |
| 227 |
create_derive!("mul", mul_like, Shl, shl_derive, shl); |
| 228 |
|
| 229 |
create_derive!( |
| 230 |
"mul_assign", |
| 231 |
mul_assign_like, |
| 232 |
MulAssign, |
| 233 |
mul_assign_derive, |
| 234 |
mul_assign, |
| 235 |
); |
| 236 |
create_derive!( |
| 237 |
"mul_assign", |
| 238 |
mul_assign_like, |
| 239 |
DivAssign, |
| 240 |
div_assign_derive, |
| 241 |
div_assign, |
| 242 |
); |
| 243 |
create_derive!( |
| 244 |
"mul_assign", |
| 245 |
mul_assign_like, |
| 246 |
RemAssign, |
| 247 |
rem_assign_derive, |
| 248 |
rem_assign, |
| 249 |
); |
| 250 |
create_derive!( |
| 251 |
"mul_assign", |
| 252 |
mul_assign_like, |
| 253 |
ShrAssign, |
| 254 |
shr_assign_derive, |
| 255 |
shr_assign, |
| 256 |
); |
| 257 |
create_derive!( |
| 258 |
"mul_assign", |
| 259 |
mul_assign_like, |
| 260 |
ShlAssign, |
| 261 |
shl_assign_derive, |
| 262 |
shl_assign, |
| 263 |
); |
| 264 |
|
| 265 |
create_derive!("not", not_like, Not, not_derive); |
| 266 |
create_derive!("not", not_like, Neg, neg_derive); |
| 267 |
|
| 268 |
create_derive!("sum", sum_like, Sum, sum_derive); |
| 269 |
create_derive!("sum", sum_like, Product, product_derive); |
| 270 |
|
| 271 |
create_derive!("try_into", try_into, TryInto, try_into_derive, try_into); |
| 272 |
|
| 273 |
create_derive!( |
| 274 |
"try_unwrap", |
| 275 |
try_unwrap, |
| 276 |
TryUnwrap, |
| 277 |
try_unwrap_derive, |
| 278 |
try_unwrap, |
| 279 |
); |
| 280 |
|
| 281 |
create_derive!("unwrap", unwrap, Unwrap, unwrap_derive, unwrap); |