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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
//! [![pipeline status](https://gitlab.com/mexus/fields-converter/badges/master/pipeline.svg)](https://gitlab.com/mexus/fields-converter/commits/master)
//! [![crates.io](https://img.shields.io/crates/v/fields-converter-derive.svg)](https://crates.io/crates/fields-converter-derive)
//! [![docs.rs](https://docs.rs/fields-converter-derive/badge.svg)](https://docs.rs/fields-converter-derive)
//!
//! Collection of procedural macros to allow you "copy", "move" and "duplicate" your structs
//! fields-wise.
//!
//! Here's an ultimate example to give you a feel for what you can do with this crate:
//!
//! ```
//! #[macro_use(Duplicate, MoveFields, CloneFields, EqFields, OrdFields)]
//! extern crate fields_converter_derive;
//! extern crate clone_fields;
//! use clone_fields::{CloneInto, CloneFrom};
//!
//! #[derive(Duplicate, MoveFields, CloneFields, EqFields, OrdFields, Debug)]
//! #[destinations("Copied")]
//! #[add_derives(Clone, Debug, PartialEq)]
//! struct Origin<'a, T> {
//! field1: String,
//! field2: T,
//! field3: &'a str,
//! }
//!
//! fn main() {
//! let source = Origin {
//! field1: "lol".into(),
//! field2: 9907,
//! field3: "testing",
//! };
//! // Let's create a `Copied` type from the `Source` (here `CloneFields` shines).
//! let copied: Copied<_> = source.clone_into();
//! // Now let's clone it using the `add_derives(Clone)`
//! let cloned = copied.clone();
//! // `assert_eq` requires `Debug` and `PartialEq` traits, which are implemented thanks to
//! // `add_derives(Debug, PartialEq)`.
//! assert_eq!(copied, cloned);
//! // .. and compare it to the source object (thanks `EqFields`!).
//! assert_eq!(source, cloned);
//! // Ok, let change a field and see that `<` also works (`OrdFields`).
//! let greater = Copied {
//! field2: source.field2 + 1,
//! ..cloned
//! };
//! assert!(source < greater);
//! // ... and vice versa:
//! assert!(greater > source);
//! // And, finally, let's move `source` into a `Copied` object, conversion sponsored by
//! // `MoveFieds`.
//! let moved: Copied<_> = source.into();
//! }
//! ```
#![recursion_limit = "128"]
#[macro_use]
extern crate quote;
#[macro_use]
extern crate syn;
extern crate proc_macro;
extern crate proc_macro2;
mod clone_fields;
mod compare_fields;
mod construct_type;
mod input_data;
mod move_fields;
mod struct_data;
use self::{
clone_fields::clone_fields,
compare_fields::{eq_fields, ord_fields},
construct_type::construct_type,
input_data::InputData,
move_fields::move_fields,
};
use proc_macro::TokenStream;
/// A derive macro for `CloneInto` and `CloneFrom` traits.
///
/// To automagically derive the traits for your type against a `DesiredTypeName` add the
/// following attributes to it:
/// * `#[derive(CloneFields)]`,
/// * and `#[destinations("DesiredTypeName")]`.
///
/// ... and the macro will generate an implementations of `CloneInto<DesiredTypeName>` and
/// `CloneFrom<DesiredTypeName>` for you type then.
///
/// You can add more than one type, like `#[destinations("Type1", "Type2", ...)]`.
///
/// It is possible to use structs with fields with different types, the only requirement is that
/// respective types should be "clonable" with the `CloneFrom` and `CloneInto` traits.
///
/// Please refer to [clone-fields](https://docs.rs/clone-fields) docs for more info on why do you
/// ;) *implied you need it*
///
/// ```
/// #[macro_use(CloneFields)]
/// extern crate fields_converter_derive;
/// extern crate clone_fields;
/// use clone_fields::{CloneInto, CloneFrom};
///
/// mod ext {
/// #[derive(Debug)]
/// pub struct ExternalType<'a, T> {
/// pub field1: String,
/// pub field2: T,
/// pub field3: &'a str,
/// }
/// }
///
/// #[derive(CloneFields, Debug)]
/// #[destinations("ext::ExternalType")]
/// struct MyType<'a, T> {
/// field1: String,
/// field2: T,
/// field3: &'a str,
/// }
///
/// fn main() {
/// let source = ext::ExternalType {
/// field1: "lol".into(),
/// field2: 9907,
/// field3: "testing",
/// };
/// let my: MyType<_> = source.clone_into();
/// assert_eq!(my.field1, source.field1);
/// assert_eq!(my.field2, source.field2);
/// assert_eq!(my.field3, source.field3);
/// }
/// ```
#[proc_macro_derive(CloneFields, attributes(destinations))]
pub fn clone_fields_derive(input: TokenStream) -> TokenStream {
let InputData {
mut struct_data,
destination_types,
..
} = parse_macro_input!(input as InputData);
struct_data.add_bound("Clone");
let impls = destination_types
.iter()
.map(|ty| clone_fields(&struct_data, ty));
quote!(#(#impls)*).into()
}
/// A derive macro for `Into` and `From` traits, converting the structures field by field.
///
/// To automagically derive the traits for your type against a `DesiredTypeName` add the
/// following attributes to it:
/// * `#[derive(MoveFields)]`,
/// * and `#[destinations("DesiredTypeName")]`.
///
/// ... and the macro will generate an implementations of `Into<DesiredTypeName>` and
/// `From<DesiredTypeName>` for you type then.
///
/// You can add more than one type, like `#[destinations("Type1", "Type2", ...)]`.
///
/// It is possible to use structs with fields with different types, the only requirement is that
/// respective types should be "convertible" with the `From` and `Into` traits.
///
/// ```
/// #[macro_use(MoveFields)]
/// extern crate fields_converter_derive;
///
/// #[derive(MoveFields)]
/// #[destinations("ext::Remote")]
/// struct Local<'a, T, S: 'a> {
/// x: T,
/// y: &'a S,
/// }
///
/// mod ext {
/// pub struct Remote<'a, T, S: 'a> {
/// // All the fields of the `Remote` type need to be public since in our derived
/// // implementations we construct the `Local` type by assigning (and converting)
/// // each field.
/// pub x: T,
/// // Generics and lifetimes are fully supported, fear not!
/// pub y: &'a S,
/// }
/// }
///
/// fn main() {
/// let remote = ext::Remote{x: 14, y: &String::from("wow")};
/// let local = Local::from(remote);
/// assert_eq!(local.x, 14);
/// assert_eq!(local.y, &"wow");
/// let remote2: ext::Remote<_, _> = local.into();
/// assert_eq!(remote2.x, 14);
/// assert_eq!(remote2.y, &"wow");
/// }
/// ```
#[proc_macro_derive(MoveFields, attributes(destinations))]
pub fn move_fields_derive(input: TokenStream) -> TokenStream {
let InputData {
struct_data,
destination_types,
..
} = parse_macro_input!(input as InputData);
let impls = destination_types
.iter()
.map(|ty| move_fields(&struct_data, ty));
quote!(#(#impls)*).into()
}
/// A derive macro to duplicate types.
///
/// To automagically derive the traits for your type against a `DesiredTypeName` add the
/// following attributes to it:
/// * `#[derive(Duplicate)]`,
/// * and `#[destinations("DesiredDuplicateName")]`.
///
/// You can optionally add a `#[add_derives(Derive1, Derive2, ...)]` attribute to add a
/// `#[derive(Derive1, Derive2, ...)]` attribute to the generated types.
///
/// More than one destination type is supporeted, like `#[destinations("Type1", "Type2", ...)]`.
///
/// ```
/// #[macro_use(Duplicate, MoveFields)]
/// extern crate fields_converter_derive;
///
/// #[derive(Duplicate, MoveFields)]
/// #[destinations("Copied")]
/// #[add_derives(Clone, PartialEq, Debug)]
/// struct Origin<'a, T> {
/// field1: String,
/// field2: T,
/// field3: &'a str,
/// }
///
/// fn main() {
/// let source = Origin {
/// field1: "lol".into(),
/// field2: 9907,
/// field3: "testing",
/// };
/// let copied: Copied<_> = source.into();
/// let cloned = copied.clone();
/// assert_eq!(copied, cloned);
/// }
/// ```
#[proc_macro_derive(Duplicate, attributes(destinations, add_derives))]
pub fn duplicate_derive(input: TokenStream) -> TokenStream {
let InputData {
struct_data,
destination_types,
derives,
} = parse_macro_input!(input as InputData);
let impls = destination_types
.iter()
.map(|ty| construct_type(&struct_data, ty, &derives));
quote!(#(#impls)*).into()
}
/// A derive macro for `PartialEq` trait.
///
/// To automagically derive the trait for your type against a `DesiredTypeName` add the
/// following attributes to it:
/// * `#[derive(EqFields)]`,
/// * and `#[destinations("DesiredTypeName")]`.
///
/// ... and the macro will generate an implementations of `PartialEq<DesiredTypeName>` for you type
/// then.
///
/// You can add more than one type, like `#[destinations("Type1", "Type2", ...)]`.
///
/// It is possible to use structs with fields with different types, the only requirement is that
/// respective types should be comparable with the `PartialEq` trait.
/// ```
/// #[macro_use(EqFields)]
/// extern crate fields_converter_derive;
///
/// #[derive(Debug)]
/// struct ExternalType<'a, T> {
/// field1: String,
/// field2: T,
/// field3: &'a str,
/// }
///
/// #[derive(EqFields, Debug)]
/// #[destinations("ExternalType")]
/// struct MyType<'a, T> {
/// field1: String,
/// field2: T,
/// field3: &'a str,
/// }
///
/// fn main() {
/// let source = ExternalType {
/// field1: "lol".into(),
/// field2: 9907,
/// field3: "testing",
/// };
/// let mut my = MyType {
/// field1: "lol".into(),
/// field2: 9907,
/// field3: "testing",
/// };
/// assert_eq!(my, source);
/// assert_eq!(source, my);
/// my.field2 += 1;
/// assert_ne!(source, my);
/// assert_ne!(my, source);
/// }
/// ```
#[proc_macro_derive(EqFields, attributes(destinations))]
pub fn eq_fields_derive(input: TokenStream) -> TokenStream {
let InputData {
mut struct_data,
destination_types,
..
} = parse_macro_input!(input as InputData);
struct_data.add_bound("PartialEq");
let impls = destination_types
.iter()
.map(|ty| eq_fields(&struct_data, ty));
quote!(#(#impls)*).into()
}
/// A derive macro for `PartialOrd` trait.
///
/// To automagically derive the trait for your type against a `DesiredTypeName` add the
/// following attributes to it:
/// * `#[derive(EqFields, OrdFields)]`,
/// * and `#[destinations("DesiredTypeName")]`.
///
/// ... and the macro will generate an implementations of `PartialEq<DesiredTypeName>` for you type
/// then. Yes, `EqFields` is a prerequisite for `OrdFields`, the same as `PartialEq` is a
/// prerequisite for `PartialOrd`.
///
/// You can add more than one type, like `#[destinations("Type1", "Type2", ...)]`.
///
/// It is possible to use structs with fields with different types, the only requirement is that
/// respective types should be comparable with the `PartialEq` trait.
/// ```
/// #[macro_use(EqFields, OrdFields)]
/// extern crate fields_converter_derive;
///
/// #[derive(Debug)]
/// struct ExternalType<'a, T> {
/// field1: String,
/// field2: T,
/// field3: &'a str,
/// }
///
/// #[derive(EqFields, OrdFields, Debug)]
/// #[destinations("ExternalType")]
/// struct MyType<'a, T: PartialOrd> {
/// field1: String,
/// field2: T,
/// field3: &'a str,
/// }
///
/// fn main() {
/// let source = ExternalType {
/// field1: "lol".into(),
/// field2: 9907,
/// field3: "testing",
/// };
/// let mut my = MyType {
/// field1: "lol".into(),
/// field2: 9907,
/// field3: "testing",
/// };
/// assert_eq!(my, source);
/// assert_eq!(source, my);
/// assert!(!(my < source));
/// assert!(!(my > source));
/// assert!(!(source < my));
/// assert!(!(source > my));
/// my.field2 += 1;
/// assert!(my > source);
/// assert!(source < my);
/// my.field1 = "a".into();
/// assert!(my < source);
/// assert!(source > my);
/// }
/// ```
#[proc_macro_derive(OrdFields, attributes(destinations))]
pub fn ord_fields_derive(input: TokenStream) -> TokenStream {
let InputData {
mut struct_data,
destination_types,
..
} = parse_macro_input!(input as InputData);
struct_data.add_bound("PartialOrd");
let impls = destination_types
.iter()
.map(|ty| ord_fields(&struct_data, ty));
quote!(#(#impls)*).into()
}