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
/*! Getset, we're ready to go! A procedural macro for generating the most basic getters and setters on fields. Getters are generated as `fn field(&self) -> &type`, while setters are generated as `fn field(&mut self, val: type)`. These macros are not intended to be used on fields which require custom logic inside of their setters and getters. Just write your own in that case! ```rust use getset::{Getters, MutGetters, CopyGetters, Setters}; #[derive(Getters, Setters, MutGetters, CopyGetters, Default)] pub struct Foo<T> where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[get] #[set] #[get_mut] private: T, /// Doc comments are supported! /// Multiline, even. #[get_copy = "pub"] #[set = "pub"] #[get_mut = "pub"] public: T, } fn main() { let mut foo = Foo::default(); foo.set_private(1); (*foo.private_mut()) += 1; assert_eq!(*foo.private(), 2); } ``` The above structure definition generates the following output with `cargo expand`. ```rust,ignore #![feature(prelude_import)] #![no_std] #[prelude_import] use ::std::prelude::v1::*; #[macro_use] extern crate std as std; use getset::{Getters, MutGetters, CopyGetters, Setters}; pub struct Foo<T> where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[get] #[set] #[get_mut] private: T, /// Doc comments are supported! /// Multiline, even. #[get_copy = "pub"] #[set = "pub"] #[get_mut = "pub"] public: T, } impl<T> Foo<T> where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[inline(always)] fn private(&self) -> &T { &self.private } /// Doc comments are supported! /// Multiline, even. #[inline(always)] pub fn public(&self) -> T { self.public } } impl<T> Foo<T> where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[inline(always)] fn set_private(&mut self, val: T) -> &mut Self { self.private = val; self } /// Doc comments are supported! /// Multiline, even. #[inline(always)] pub fn set_public(&mut self, val: T) -> &mut Self { self.public = val; self } } impl<T> Foo<T> where T: Copy + Clone + Default, { /// Doc comments are supported! /// Multiline, even. #[inline(always)] fn private_mut(&mut self) -> &mut T { &mut self.private } /// Doc comments are supported! /// Multiline, even. #[inline(always)] pub fn public_mut(&mut self) -> &mut T { &mut self.public } } #[automatically_derived] #[allow(unused_qualifications)] impl<T: ::core::default::Default> ::core::default::Default for Foo<T> where T: Copy + Clone + Default, { #[inline] fn default() -> Foo<T> { Foo { private: ::core::default::Default::default(), public: ::core::default::Default::default(), } } } ``` Attributes can be set on struct level for all fields in struct as well. Field level attributes take precedence. ```rust #[macro_use] extern crate getset; mod submodule { #[derive(Getters, CopyGetters, Default)] #[get_copy = "pub"] // By default add a pub getting for all fields. pub struct Foo { public: i32, #[get_copy] // Override as private private: i32, } fn demo() { let mut foo = Foo::default(); foo.private(); } } fn main() { let mut foo = submodule::Foo::default(); foo.public(); } ``` For some purposes, it's useful to have the `get_` prefix on the getters for either legacy of compatability reasons. It is done with `get-prefix`. ```rust #[macro_use] extern crate getset; #[derive(Getters, Default)] pub struct Foo { #[get = "pub with_prefix"] field: bool, } fn main() { let mut foo = Foo::default(); let val = foo.get_field(); } ``` */ extern crate proc_macro; extern crate syn; #[macro_use] extern crate quote; extern crate proc_macro2; use proc_macro::TokenStream; use proc_macro2::TokenStream as TokenStream2; use syn::{DataStruct, DeriveInput, Meta}; mod generate; use crate::generate::{GenMode, GenParams}; #[proc_macro_derive(Getters, attributes(get, with_prefix))] pub fn getters(input: TokenStream) -> TokenStream { // Parse the string representation let ast: DeriveInput = syn::parse(input).expect("Couldn't parse for getters"); let params = GenParams { attribute_name: "get", fn_name_prefix: "", fn_name_suffix: "", global_attr: parse_global_attr(&ast.attrs, "get"), }; // Build the impl let gen = produce(&ast, &GenMode::Get, ¶ms); // Return the generated impl gen.into() } #[proc_macro_derive(CopyGetters, attributes(get_copy, with_prefix))] pub fn copy_getters(input: TokenStream) -> TokenStream { // Parse the string representation let ast: DeriveInput = syn::parse(input).expect("Couldn't parse for getters"); let params = GenParams { attribute_name: "get_copy", fn_name_prefix: "", fn_name_suffix: "", global_attr: parse_global_attr(&ast.attrs, "get_copy"), }; // Build the impl let gen = produce(&ast, &GenMode::GetCopy, ¶ms); // Return the generated impl gen.into() } #[proc_macro_derive(MutGetters, attributes(get_mut))] pub fn mut_getters(input: TokenStream) -> TokenStream { // Parse the string representation let ast: DeriveInput = syn::parse(input).expect("Couldn't parse for getters"); let params = GenParams { attribute_name: "get_mut", fn_name_prefix: "", fn_name_suffix: "_mut", global_attr: parse_global_attr(&ast.attrs, "get_mut"), }; // Build the impl let gen = produce(&ast, &GenMode::GetMut, ¶ms); // Return the generated impl gen.into() } #[proc_macro_derive(Setters, attributes(set))] pub fn setters(input: TokenStream) -> TokenStream { // Parse the string representation let ast: DeriveInput = syn::parse(input).expect("Couldn't parse for setters"); let params = GenParams { attribute_name: "set", fn_name_prefix: "set_", fn_name_suffix: "", global_attr: parse_global_attr(&ast.attrs, "set"), }; // Build the impl let gen = produce(&ast, &GenMode::Set, ¶ms); // Return the generated impl gen.into() } fn parse_global_attr(attrs: &[syn::Attribute], attribute_name: &str) -> Option<Meta> { attrs .iter() .filter_map(|v| { let meta = v.parse_meta().expect("attribute"); if meta.path().is_ident(attribute_name) { Some(meta) } else { None } }) .last() } fn produce(ast: &DeriveInput, mode: &GenMode, params: &GenParams) -> TokenStream2 { let name = &ast.ident; let generics = &ast.generics; let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); // Is it a struct? if let syn::Data::Struct(DataStruct { ref fields, .. }) = ast.data { let generated = fields .iter() .map(|f| generate::implement(f, mode, params)) .collect::<Vec<_>>(); quote! { impl #impl_generics #name #ty_generics #where_clause { #(#generated)* } } } else { // Nope. This is an Enum. We cannot handle these! panic!("#[derive(Getters)] is only defined for structs, not for enums!"); } }