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
//! # typed_index_derive
//!
//! A frequent pattern in Rust is to store objects in a vector and use integer indexes
//! as handlers to them. While using `usize` works, it could become confusing if there
//! are several flavors of indexes. To make the meaning of each index clear the newtype
//! wrappers like `FooIdx(usize)` are useful, but require a fair amount of boilerplate.
//! This crate derives the boilerplate for you:
//!
//!
//! ```rust
//! #[macro_use]
//! extern crate typed_index_derive;
//!
//! struct Spam(String);
//!
//! #[derive(
//!     // Usual derives for plain old data
//!     Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash,
//!     // this crate
//!     TypedIndex
//! )]
//! #[typed_index(Spam)] // index into `&[Spam]`
//! struct SpamIdx(usize); // could be `u32` instead of `usize`
//!
//! fn main() {
//!     let spams = vec![Spam("foo".into()), Spam("bar".into()), Spam("baz".into())];
//!
//!     // Conversions between `usize` and `SpamIdx`
//!     let idx: SpamIdx = 1.into();
//!     assert_eq!(usize::from(idx), 1);
//!
//!     // We can index `Vec<Spam>` with `SpamIdx`
//!     assert_eq!(&spams[idx].0, "bar");
//!
//!     // However, we can't index `Vec<usize>`
//!     // vec![1, 2, 3][idx]
//!     // error: slice indices are of type `usize` or ranges of `usize`
//!
//!     // Similarly to `<[Spam]>::get`, `SpamIdx::get`/`SpamIdx::get_mut`
//!     // returns `None` if it's out of bounds. Note that the receiver and
//!     // argument are flipped.
//!     let oob: SpamIdx = 92.into();
//!     assert!(oob.get(&spams).is_none());
//!
//!     // You can add/subtract `usize` from an index
//!     assert_eq!(&spams[idx - 1].0, "foo");
//!
//!     // The difference between two indices is `usize`
//!     assert_eq!(idx - idx, 0usize);
//! }
//! ```

#![recursion_limit = "256"]

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


#[proc_macro_derive(TypedIndex, attributes(typed_index))]
pub fn derive_typed_index(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input: syn::DeriveInput = syn::parse(input).unwrap();

    let name = input.ident;
    let ty_name = get_type_name(&input.attrs);
    let index_ty = get_index_ty(&input.data);

    let expanded = quote! {
        impl #name {
            pub fn get(self, xs: &[#ty_name]) -> Option<&#ty_name> {
                xs.get(self.0 as usize)
            }

            pub fn get_mut(self, xs: &mut [#ty_name]) -> Option<&mut #ty_name> {
                xs.get_mut(self.0 as usize)
            }
        }

        impl ::std::ops::Index<#name> for [#ty_name] {
            type Output = #ty_name;

            fn index(&self, index: #name) -> &#ty_name {
                &self[index.0 as usize]
            }
        }

        impl ::std::ops::IndexMut<#name> for [#ty_name] {
            fn index_mut(&mut self, index: #name) -> &mut #ty_name {
                &mut self[index.0 as usize]
            }
        }

        impl ::std::ops::Index<#name> for Vec<#ty_name> {
            type Output = #ty_name;

            fn index(&self, index: #name) -> &#ty_name {
                &self.as_slice()[index]
            }
        }

        impl ::std::ops::IndexMut<#name> for Vec<#ty_name> {
            fn index_mut(&mut self, index: #name) -> &mut #ty_name {
                &mut self.as_mut_slice()[index]
            }
        }

        impl From<#index_ty> for #name {
            fn from(val: #index_ty) -> #name { #name(val) }
        }

        impl From<#name> for #index_ty {
            fn from(val: #name) -> #index_ty { val.0 }
        }

        impl ::std::ops::Add<#index_ty> for #name {
            type Output = #name;

            fn add(self, rhs: #index_ty) -> #name {
                #name(self.0 + rhs)
            }
        }

        impl ::std::ops::AddAssign<#index_ty> for #name {
            fn add_assign(&mut self, rhs: #index_ty) {
                self.0 += rhs
            }
        }

        impl ::std::ops::Sub<#index_ty> for #name {
            type Output = #name;

            fn sub(self, rhs: #index_ty) -> #name {
                #name(self.0 - rhs)
            }
        }

        impl ::std::ops::SubAssign<#index_ty> for #name {
            fn sub_assign(&mut self, rhs: #index_ty) {
                self.0 -= rhs
            }
        }

        impl ::std::ops::Sub<#name> for #name {
            type Output = #index_ty;

            fn sub(self, rhs: #name) -> #index_ty {
                self.0 - rhs.0
            }
        }
    };

    expanded.into()
}

fn get_type_name(attrs: &[syn::Attribute]) -> syn::Ident {
    match attrs.iter().filter_map(get_typed_index_meta_items).next() {
        Some(ref mut metas) if metas.len() == 1 => {
            let meta = metas.pop().unwrap();
            match meta {
                syn::NestedMeta::Meta(syn::Meta::Word(word)) => return word,
                _ => (),
            }
        }
        _ => (),
    };
    panic!("`#[typed_index(YourType)]` attribute is mandatory")
}

fn get_index_ty(data: &syn::Data) -> syn::Type {
    match data {
        syn::Data::Struct(data) => {
            match data.fields {
                syn::Fields::Unnamed(ref fields) if fields.unnamed.len() == 1 => {
                    let field = fields.unnamed.first().unwrap().into_value();
                    return field.ty.clone();
                }
                _ => (),
            }
        }
        _ => (),
    };

    panic!("typed_index works only for newtype structs")
}

// Copy-pasted from serde
fn get_typed_index_meta_items(attr: &syn::Attribute) -> Option<Vec<syn::NestedMeta>> {
    if attr.path.segments.len() == 1 && attr.path.segments[0].ident == "typed_index" {
        match attr.interpret_meta() {
            Some(syn::Meta::List(ref meta)) => Some(meta.nested.iter().cloned().collect()),
            _ => {
                // TODO: produce an error
                None
            }
        }
    } else {
        None
    }
}