druid_derive/
lib.rs

1// Copyright 2019 The Druid Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! derive macros for Druid.
16
17#![deny(clippy::trivially_copy_pass_by_ref)]
18#![doc(
19    html_logo_url = "https://raw.githubusercontent.com/linebender/druid/screenshots/images/doc_logo.png"
20)]
21
22extern crate proc_macro;
23
24mod attr;
25mod data;
26mod lens;
27
28use proc_macro::TokenStream;
29use syn::parse_macro_input;
30
31/// Generates implementations of the `Data` trait.
32///
33/// This macro supports a `data` field attribute with the following arguments:
34///
35/// - `#[data(ignore)]` makes the generated `Data::same` function skip comparing this field.
36/// - `#[data(same_fn="foo")]` uses the function `foo` for comparing this field. `foo` should
37///    be the name of a function with signature `fn(&T, &T) -> bool`, where `T` is the type of
38///    the field.
39/// - `#[data(eq)]` is shorthand for `#[data(same_fn = "PartialEq::eq")]`
40///
41/// # Example
42///
43/// ```rust
44/// use druid_derive::Data;
45///
46/// #[derive(Clone, Data)]
47/// struct State {
48///     number: f64,
49///     // `Vec` doesn't implement `Data`, so we need to either ignore it or supply a `same_fn`.
50///     #[data(eq)]
51///     // same as #[data(same_fn="PartialEq::eq")]
52///     indices: Vec<usize>,
53///     // This is just some sort of cache; it isn't important for sameness comparison.
54///     #[data(ignore)]
55///     cached_indices: Vec<usize>,
56/// }
57/// ```
58#[proc_macro_derive(Data, attributes(data))]
59pub fn derive_data(input: TokenStream) -> TokenStream {
60    let input = parse_macro_input!(input as syn::DeriveInput);
61    data::derive_data_impl(input)
62        .unwrap_or_else(|err| err.to_compile_error())
63        .into()
64}
65
66/// Generates lenses to access the fields of a struct.
67///
68/// An associated constant is defined on the struct for each field,
69/// having the same name as the field.
70///
71/// This macro supports a `lens` field attribute with the following arguments:
72///
73/// - `#[lens(ignore)]` skips creating a lens for one field.
74/// - `#[lens(name="foo")]` gives the lens the specified name (instead of the default, which is to
75///    create a lens with the same name as the field).
76///
77/// # Example
78///
79/// ```rust
80/// use druid_derive::Lens;
81///
82/// #[derive(Lens)]
83/// struct State {
84///     // The Lens derive will create a `State::text` constant implementing
85///     // `druid::Lens<State, String>`
86///     text: String,
87///     // The Lens derive will create a `State::lens_number` constant implementing
88///     // `druid::Lens<State, f64>`
89///     #[lens(name = "lens_number")]
90///     number: f64,
91///     // The Lens derive won't create anything for this field.
92///     #[lens(ignore)]
93///     blah: f64,
94/// }
95/// ```
96#[proc_macro_derive(Lens, attributes(lens))]
97pub fn derive_lens(input: TokenStream) -> TokenStream {
98    let input = parse_macro_input!(input as syn::DeriveInput);
99    lens::derive_lens_impl(input)
100        .unwrap_or_else(|err| err.to_compile_error())
101        .into()
102}