bon_macros/builder/builder_gen/builder_derives.rs
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
use super::top_level_config::{DeriveConfig, DerivesConfig};
use super::BuilderGenCtx;
use crate::builder::builder_gen::Member;
use crate::util::prelude::*;
use darling::ast::GenericParamExt;
impl BuilderGenCtx {
pub(crate) fn builder_derives(&self) -> TokenStream {
let DerivesConfig { clone, debug } = &self.builder_type.derives;
let mut tokens = TokenStream::new();
if let Some(derive) = clone {
tokens.extend(self.derive_clone(derive));
}
if let Some(derive) = debug {
tokens.extend(self.derive_debug(derive));
}
tokens
}
/// We follow the logic of the standard `#[derive(...)]` macros such as `Clone` and `Debug`.
/// They add bounds of their respective traits to every generic type parameter on the struct
/// without trying to analyze if that bound is actually required for the derive to work, so
/// it's a conservative approach.
///
/// However, the user can also override these bounds using the `bounds(...)` attribute for
/// the specific derive.
fn where_clause_for_derive(
&self,
target_trait_bounds: &TokenStream,
derive: &DeriveConfig,
) -> TokenStream {
let derive_specific_predicates = derive
.bounds
.as_ref()
.map(ToTokens::to_token_stream)
.unwrap_or_else(|| {
let bounds = self
.generics
.decl_without_defaults
.iter()
.filter_map(syn::GenericParam::as_type_param)
.map(|param| {
let ident = ¶m.ident;
quote! {
#ident: #target_trait_bounds
}
});
quote! {
#( #bounds, )*
}
});
let inherent_item_predicates = self.generics.where_clause_predicates();
quote! {
where
#( #inherent_item_predicates, )*
#derive_specific_predicates
}
}
fn derive_clone(&self, derive: &DeriveConfig) -> TokenStream {
let bon = &self.bon;
let generics_decl = &self.generics.decl_without_defaults;
let generic_args = &self.generics.args;
let builder_ident = &self.builder_type.ident;
let phantom_field = &self.ident_pool.phantom;
let receiver_field = &self.ident_pool.receiver;
let start_fn_args_field = &self.ident_pool.start_fn_args;
let named_members_field = &self.ident_pool.named_members;
let clone = quote!(::core::clone::Clone);
let clone_receiver = self.receiver().map(|receiver| {
let ty = &receiver.without_self_keyword;
quote! {
#receiver_field: <#ty as #clone>::clone(&self.#receiver_field),
}
});
let clone_start_fn_args = self.start_fn_args().next().map(|_| {
let types = self.start_fn_args().map(|arg| &arg.base.ty.norm);
let indices = self.start_fn_args().map(|arg| &arg.index);
quote! {
// We clone named members individually instead of cloning
// the entire tuple to improve error messages in case if
// one of the members doesn't implement `Clone`. This avoids
// a sentence that say smth like
// ```
// required for `(...big type...)` to implement `Clone`
// ```
#start_fn_args_field: (
#( <#types as #clone>::clone(&self.#start_fn_args_field.#indices), )*
),
}
});
let where_clause = self.where_clause_for_derive(&clone, derive);
let state_mod = &self.state_mod.ident;
let clone_named_members = self.named_members().map(|member| {
let member_index = &member.index;
// The type hint here is necessary to get better error messages
// that point directly to the type that doesn't implement `Clone`
// in the input code using the span info from the type hint.
let ty = member.underlying_norm_ty();
quote! {
#bon::__::derives::clone_member::<#ty>(
&self.#named_members_field.#member_index
)
}
});
let clone_fields = self.custom_fields().map(|member| {
let member_ident = &member.ident;
let member_ty = &member.norm_ty;
quote! {
// The type hint here is necessary to get better error messages
// that point directly to the type that doesn't implement `Clone`
// in the input code using the span info from the type hint.
#member_ident: <#member_ty as #clone>::clone(&self.#member_ident)
}
});
let state_var = &self.state_var;
quote! {
#[automatically_derived]
impl<
#(#generics_decl,)*
#state_var: #state_mod::State
>
#clone for #builder_ident<
#(#generic_args,)*
#state_var
>
#where_clause
{
fn clone(&self) -> Self {
Self {
#phantom_field: ::core::marker::PhantomData,
#clone_receiver
#clone_start_fn_args
#( #clone_fields, )*
// We clone named members individually instead of cloning
// the entire tuple to improve error messages in case if
// one of the members doesn't implement `Clone`. This avoids
// a sentence that say smth like
// ```
// required for `(...big type...)` to implement `Clone`
// ```
#named_members_field: ( #( #clone_named_members, )* ),
}
}
}
}
}
fn derive_debug(&self, derive: &DeriveConfig) -> TokenStream {
let receiver_field = &self.ident_pool.receiver;
let start_fn_args_field = &self.ident_pool.start_fn_args;
let named_members_field = &self.ident_pool.named_members;
let bon = &self.bon;
let format_members = self.members.iter().filter_map(|member| {
match member {
Member::StartFn(member) => {
let member_index = &member.index;
let member_ident_str = member.base.ident.to_string();
let member_ty = &member.base.ty.norm;
Some(quote! {
output.field(
#member_ident_str,
#bon::__::derives::as_dyn_debug::<#member_ty>(
&self.#start_fn_args_field.#member_index
)
);
})
}
Member::Field(member) => {
let member_ident = &member.ident;
let member_ident_str = member_ident.to_string();
let member_ty = &member.norm_ty;
Some(quote! {
output.field(
#member_ident_str,
#bon::__::derives::as_dyn_debug::<#member_ty>(
&self.#member_ident
)
);
})
}
Member::Named(member) => {
let member_index = &member.index;
let member_ident_str = &member.name.snake_raw_str;
let member_ty = member.underlying_norm_ty();
Some(quote! {
if let Some(value) = &self.#named_members_field.#member_index {
output.field(
#member_ident_str,
#bon::__::derives::as_dyn_debug::<#member_ty>(value)
);
}
})
}
// The values for these members are computed only in the finishing
// function where the builder is consumed, and they aren't stored
// in the builder itself.
Member::FinishFn(_) | Member::Skip(_) => None,
}
});
let format_receiver = self.receiver().map(|receiver| {
let ty = &receiver.without_self_keyword;
quote! {
output.field(
"self",
#bon::__::derives::as_dyn_debug::<#ty>(
&self.#receiver_field
)
);
}
});
let debug = quote!(::core::fmt::Debug);
let where_clause = self.where_clause_for_derive(&debug, derive);
let state_mod = &self.state_mod.ident;
let generics_decl = &self.generics.decl_without_defaults;
let generic_args = &self.generics.args;
let builder_ident = &self.builder_type.ident;
let state_var = &self.state_var;
let builder_ident_str = builder_ident.to_string();
quote! {
#[automatically_derived]
impl<
#(#generics_decl,)*
#state_var: #state_mod::State
>
#debug for #builder_ident<
#(#generic_args,)*
#state_var
>
#where_clause
{
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
let mut output = f.debug_struct(#builder_ident_str);
#format_receiver
#(#format_members)*
output.finish()
}
}
}
}
}