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
//! # shrinkwraprs
//!
//! Making wrapper types allows us to give more compile-time
//! guarantees about our code being correct:
//!
//! ```ignore
//! // Now we can't mix up widths and heights; the compiler will yell at us!
//! struct Width(u64);
//! struct Height(u64);
//! ```
//!
//! But... they're kind of a pain to work with. If you ever need to get at
//! that wrapped `u64`, you need to constantly pattern-match back and forth
//! to wrap and unwrap the values.
//!
//! `shrinkwraprs` aims to alleviate this pain by allowing you to derive
//! implementations of various conversion traits by deriving
//! `Shrinkwrap` and `ShrinkwrapMut`.
//!
//! ## Traits implemented
//!
//! Currently, using `#[derive(Shrinkwrap)]` will derive the following traits
//! for all structs:
//!
//! * `AsRef<InnerType>`
//! * `Borrow<InnerType>`
//! * `Deref<Target=InnerType>`
//!
//! Additionally, using `#[derive(Shrinkwrap, ShrinkwrapMut)]` will additionally
//! derive the following traits:
//!
//! * `AsMut<InnerType>`
//! * `BorrowMut<InnerType>`
//! * `DerefMut<Target=InnerType>`
//!
//! ## Cool, how do I use it?
//!
//! ```ignore
//! #[macro_use] extern crate shrinkwraprs;
//!
//! #[derive(Shrinkwrap)]
//! struct Email(String);
//!
//! fn main() {
//!     let email = Email("chiya+snacks@natsumeya.jp".into());
//!
//!     let is_discriminated_email =
//!         email.contains("+");  // Woohoo, we can use the email like a string!
//!
//!     /* ... */
//! }
//! ```
//!
//! If you have multiple fields, but there's only one field you want to be able
//! to deref/borrow as, mark it with `#[shrinkwrap(main_field)]`:
//!
//! ```ignore
//! #[derive(Shrinkwrap)]
//! struct Email {
//!     spamminess: f64,
//!     #[shrinkwrap(main_field)] addr: String
//! }
//!
//! #[derive(Shrinkwrap)]
//! struct CodeSpan(u32, u32, #[shrinkwrap(main_field)] Token);
//! ```
//!
//! If you also want to be able to modify the wrapped value directly,
//! derive `ShrinkwrapMut` as well:
//!
//! ```ignore
//! #[derive(Shrinkwrap, ShrinkwrapMut)]
//! struct InputBuffer {
//!     buffer: String
//! }
//!
//! ...
//! let mut input_buffer = /* ... */;
//! input_buffer.push_str("some values");
//! ...
//! ```

// We'll probably also want to implement some other conversion traits, namely
// `From`, plus some constructors for the type itself.
//
// Additionally, perhaps subsume some functionality from
// [`from_variants`](https://crates.io/crates/from_variants)?
//
// Note: correctness concerns arise from implementing the `Mut` traits
// willy-nilly. Probably want to lock those behind visibility barriers
// for all structs.
//
// Other ideas: a struct can only `Deref` to a single type, but it can
// be `Borrow`ed or `AsRef`ed as multiple types. Maybe generate multiple
// trait implementations for multiple-fielded structs? Would have to be
// careful to avoid type collisions.

#![cfg_attr(feature = "strict", deny(warnings))]
#![recursion_limit="128"]

extern crate proc_macro;
extern crate syn;
#[macro_use] extern crate quote;
extern crate itertools;

use proc_macro::TokenStream;
use quote::{Tokens, ToTokens};

mod ast;

#[proc_macro_derive(Shrinkwrap, attributes(shrinkwrap))]
pub fn shrinkwrap(tokens: TokenStream) -> TokenStream {
  use ast::{validate_derive_input, ShrinkwrapInput};

  let input: syn::DeriveInput = syn::parse(tokens)
    .unwrap();
  let input = validate_derive_input(input);

  let tokens = match input {
    ShrinkwrapInput::Tuple(tuple) => impl_tuple(tuple),
    ShrinkwrapInput::NaryTuple(nary_tuple) => impl_nary_tuple(nary_tuple),
    ShrinkwrapInput::Single(single) => impl_single(single),
    ShrinkwrapInput::Multi(multi) => impl_multi(multi)
  };

  tokens.to_string()
    .parse()
    .unwrap()
}

#[proc_macro_derive(ShrinkwrapMut, attributes(shrinkwrap))]
pub fn shrinkwrap_mut(tokens: TokenStream) -> TokenStream {
  use ast::{validate_derive_input, ShrinkwrapInput};

  let input: syn::DeriveInput = syn::parse(tokens)
    .unwrap();
  let input = validate_derive_input(input);

  let tokens = match input {
    ShrinkwrapInput::Tuple(tuple) => impl_tuple_mut(tuple),
    ShrinkwrapInput::NaryTuple(nary_tuple) => impl_nary_tuple_mut(nary_tuple),
    ShrinkwrapInput::Single(single) => impl_single_mut(single),
    ShrinkwrapInput::Multi(multi) => impl_multi_mut(multi)
  };

  tokens.to_string()
    .parse()
    .unwrap()
}

// When generating our code, we need to be careful not to leak anything we
// don't intend to, into the surrounding code. For example, we don't use
// imports unless they're inside a scope, because otherwise we'd be inserting
// invisible imports whenever a user used #[derive(Shrinkwrap)].

struct GenBorrowInfo {
  /// What should the `impl` keyword look like? `impl`, `impl<T>`, `impl<'a, T>`, etc.
  impl_prefix: Tokens,
  /// Should also include any generic parameters for the struct.
  struct_name: Tokens,
  inner_type: Tokens,
  /// An expression that takes in `self` and *moves* the inner field as its return value.
  borrow_expr: Tokens
}

fn impl_immut_borrows(info: &GenBorrowInfo) -> Tokens {
  let &GenBorrowInfo {
    ref impl_prefix,
    ref struct_name,
    ref inner_type,
    ref borrow_expr
  } = info;

  quote! {
    #impl_prefix ::std::ops::Deref for #struct_name {
      type Target = #inner_type;
      fn deref(&self) -> &Self::Target {
        &#borrow_expr
      }
    }

    #impl_prefix ::std::borrow::Borrow<#inner_type> for #struct_name {
      fn borrow(&self) -> &#inner_type {
        &#borrow_expr
      }
    }

    #impl_prefix ::std::convert::AsRef<#inner_type> for #struct_name {
      fn as_ref(&self) -> &#inner_type {
        &#borrow_expr
      }
    }
  }
}

// We separate out mutable borrow traits from the immutable borrows because
// later we might want to differ whether we implement mutable borrows based
// on struct visibility.

fn impl_mut_borrows(info: &GenBorrowInfo) -> Tokens {
  let &GenBorrowInfo {
    ref impl_prefix,
    ref struct_name,
    ref inner_type,
    ref borrow_expr
  } = info;

  quote! {
    #impl_prefix ::std::ops::DerefMut for #struct_name {
      fn deref_mut(&mut self) -> &mut Self::Target {
        &mut #borrow_expr
      }
    }

    #impl_prefix ::std::borrow::BorrowMut<#inner_type> for #struct_name {
      fn borrow_mut(&mut self) -> &mut #inner_type {
        &mut #borrow_expr
      }
    }

    #impl_prefix ::std::convert::AsMut<#inner_type> for #struct_name {
      fn as_mut(&mut self) -> &mut #inner_type {
        &mut #borrow_expr
      }
    }
  }
}

#[allow(unused_variables)]
fn impl_tuple(input: ast::Tuple) -> Tokens {
  let ast::Tuple { details, inner_type } = input;
  let ast::StructDetails { ident, visibility } = details;

  let gen_info = GenBorrowInfo {
    impl_prefix: quote!( impl ),
    struct_name: quote!( #ident ),
    inner_type: quote!( #inner_type ),
    borrow_expr: quote!( self.0 )
  };

  let mut tokens = Tokens::new();

  impl_immut_borrows(&gen_info)
    .to_tokens(&mut tokens);

  tokens
}

#[allow(unused_variables)]
fn impl_nary_tuple(input: ast::NaryTuple) -> Tokens {
  let ast::NaryTuple { details, inner_field_index, inner_type } = input;
  let ast::StructDetails { ident, visibility } = details;

  let gen_info = GenBorrowInfo {
    impl_prefix: quote!( impl ),
    struct_name: quote!( #ident ),
    inner_type: quote!( #inner_type ),
    borrow_expr: quote!( self.#inner_field_index )
  };

  let mut tokens = Tokens::new();

  impl_immut_borrows(&gen_info)
    .to_tokens(&mut tokens);

  tokens
}

#[allow(unused_variables)]
fn impl_single(input: ast::Single) -> Tokens {
  let ast::Single { details, inner_field, inner_type, inner_visibility } = input;
  let ast::StructDetails { ident, visibility } = details;

  let gen_info = GenBorrowInfo {
    impl_prefix: quote!( impl ),
    struct_name: quote!( #ident ),
    inner_type: quote!( #inner_type ),
    borrow_expr: quote!( self.#inner_field )
  };

  let mut tokens = Tokens::new();

  impl_immut_borrows(&gen_info)
    .to_tokens(&mut tokens);

  tokens
}

#[allow(unused_variables)]
fn impl_multi(input: ast::Multi) -> Tokens {
  let ast::Multi { details, inner_field, inner_type, inner_visibility } = input;
  let ast::StructDetails { ident, visibility } = details;

  let gen_info = GenBorrowInfo {
    impl_prefix: quote!( impl ),
    struct_name: quote!( #ident ),
    inner_type: quote!( #inner_type ),
    borrow_expr: quote!( self.#inner_field )
  };

  let mut tokens = Tokens::new();

  impl_immut_borrows(&gen_info)
    .to_tokens(&mut tokens);

  tokens
}

#[allow(unused_variables)]
fn impl_tuple_mut(input: ast::Tuple) -> Tokens {
  let ast::Tuple { details, inner_type } = input;
  let ast::StructDetails { ident, visibility } = details;

  let gen_info = GenBorrowInfo {
    impl_prefix: quote!( impl ),
    struct_name: quote!( #ident ),
    inner_type: quote!( #inner_type ),
    borrow_expr: quote!( self.0 )
  };

  let mut tokens = Tokens::new();

  impl_mut_borrows(&gen_info)
    .to_tokens(&mut tokens);

  tokens
}

#[allow(unused_variables)]
fn impl_nary_tuple_mut(input: ast::NaryTuple) -> Tokens {
  let ast::NaryTuple { details, inner_field_index, inner_type } = input;
  let ast::StructDetails { ident, visibility } = details;

  let gen_info = GenBorrowInfo {
    impl_prefix: quote!( impl ),
    struct_name: quote!( #ident ),
    inner_type: quote!( #inner_type ),
    borrow_expr: quote!( self.#inner_field_index )
  };

  let mut tokens = Tokens::new();

  impl_mut_borrows(&gen_info)
    .to_tokens(&mut tokens);

  tokens
}

#[allow(unused_variables)]
fn impl_single_mut(input: ast::Single) -> Tokens {
  let ast::Single { details, inner_field, inner_type, inner_visibility } = input;
  let ast::StructDetails { ident, visibility } = details;

  let gen_info = GenBorrowInfo {
    impl_prefix: quote!( impl ),
    struct_name: quote!( #ident ),
    inner_type: quote!( #inner_type ),
    borrow_expr: quote!( self.#inner_field )
  };

  let mut tokens = Tokens::new();

  impl_mut_borrows(&gen_info)
    .to_tokens(&mut tokens);

  tokens
}

#[allow(unused_variables)]
fn impl_multi_mut(input: ast::Multi) -> Tokens {
  let ast::Multi { details, inner_field, inner_type, inner_visibility } = input;
  let ast::StructDetails { ident, visibility } = details;

  let gen_info = GenBorrowInfo {
    impl_prefix: quote!( impl ),
    struct_name: quote!( #ident ),
    inner_type: quote!( #inner_type ),
    borrow_expr: quote!( self.#inner_field )
  };

  let mut tokens = Tokens::new();

  impl_mut_borrows(&gen_info)
    .to_tokens(&mut tokens);

  tokens
}