injectable-rs-macros 0.1.0

Proc macros for the injectable-rs DI framework
Documentation
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! Provider code generation.
//!
//! Generates the `<Type>Provider` struct and its `Provider<T>` implementation.
//! The generated provider:
//! 1. Extracts each struct field via `Extract::extract(ctx)` (field injection)
//!    OR uses `Default::default()` (when `#[injectable(default)]` is specified)
//! 2. Constructs the struct with the extracted/defaulted values
//! 3. Calls `PostConstruct::post_construct()` if `#[injectable(has_post_construct)]`
//! 4. Returns the fully constructed value

use proc_macro2::TokenStream;
use quote::quote;

use crate::metadata::{
    extract_arc_inner, extract_arc_inner_str, extract_inject_dyn_inner, extract_inject_inner,
    extract_option_inject_dyn_inner,
};

/// How a field should be handled during injection.
///
/// `Inject<T>` fields are auto-injected.  All other field types require an
/// explicit `#[injectable(inject)]` or `#[injectable(inject(use_factory_*=…))]` annotation; omitting
/// the annotation is a compile error.
#[derive(Debug, Clone)]
pub enum FieldInjectKind {
    /// Extract the field via `Extract::extract(ctx)`.
    /// Applied automatically to `Inject<T>` fields, or explicitly via `#[injectable(inject)]`.
    Inject,
    /// Resolve the field via `ctx.resolve_external::<FieldType>()`.
    /// Use for external types (e.g. `Arc<ExternalStore>`) registered via `DynProvider` but
    /// not annotated with `#[injectable]`. Annotation: `#[injectable(inject(external))]`.
    External,
    /// Call the given **async** factory `async fn(ctx: &ResolveContext) -> Result<T, E>`.
    Factory(syn::Path),
    /// Call the given **sync** factory `fn(ctx: &ResolveContext) -> FieldType` (no `.await`).
    Provider(syn::Path),
}

/// Information about a struct field for field injection.
#[derive(Debug, Clone)]
pub struct FieldInfo {
    /// The field name (for named fields).
    pub name: Option<syn::Ident>,
    /// The field type.
    pub ty: syn::Type,
    /// The type as a string for graph metadata.
    pub ty_string: String,
    /// Whether this field should be injected or skipped (defaulted).
    pub inject_kind: FieldInjectKind,
}

/// Generate a provider that auto-wires from struct fields.
///
/// When a struct has `#[injectable]` but no `#[injectable(default)]`,
/// all fields must implement `Extract`. The generated provider:
/// 1. Extracts each field via `<FieldType as Extract>::extract(ctx).await?`
///    (unless the field is marked with `#[inject(skip)]`, which uses `Default::default()`)
/// 2. Constructs the struct using a struct literal
/// 3. Calls `PostConstruct::post_construct()` if `has_post_construct` is true
///
/// # Rules
///
/// - `Inject<T>` fields are auto-injected; all other fields require `#[injectable(inject)]`
///   (error is emitted at macro-expansion time, not here)
/// - `FieldInjectKind::Inject` → `<FieldType as Extract>::extract(ctx).await?`
/// - `FieldInjectKind::Factory` → async factory called with `ctx`
/// - `FieldInjectKind::Provider` → sync factory called with `&extracted_value`
/// - Unit structs (no fields) are constructed without extraction
pub fn generate_field_injection_provider(
    type_name: &syn::Ident,
    generics: &syn::Generics,
    fields: &[FieldInfo],
    scope: &str,
    has_post_construct: bool,
) -> TokenStream {
    let provider_name = provider_ident(type_name);
    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
    let is_generic = !generics.params.is_empty();

    // Generate extraction/default statements for each field
    let field_statements: Vec<TokenStream> = fields
        .iter()
        .enumerate()
        .map(|(i, field)| {
            let field_ty = &field.ty;
            match &field.inject_kind {
                FieldInjectKind::Inject => {
                    // Special path for Inject<dyn Trait>: go through resolve_external::<Arc<dyn Trait>>
                    // to avoid both the T: Sized requirement and orphan-rule violations.
                    let var_expr = if let Some(dyn_ty) = extract_inject_dyn_inner(field_ty) {
                        quote! {
                            {
                                let __arc = ctx.resolve_external::<::std::sync::Arc<#dyn_ty>>().await?;
                                injectable_rs_runtime::Inject::new(__arc)
                            }
                        }
                    } else if let Some(dyn_ty) = extract_option_inject_dyn_inner(field_ty) {
                        quote! {
                            match ctx.resolve_external::<::std::sync::Arc<#dyn_ty>>().await {
                                Ok(__arc) => Some(injectable_rs_runtime::Inject::new(__arc)),
                                Err(injectable_rs_runtime::InjectableError::MissingDependency { .. }) => None,
                                Err(__e) => return Err(__e),
                            }
                        }
                    } else {
                        quote! {
                            <#field_ty as injectable_rs_runtime::Extract>::extract(ctx).await?
                        }
                    };
                    if let Some(name) = &field.name {
                        quote! { let #name = #var_expr; }
                    } else {
                        let temp_name = syn::Ident::new(
                            &format!("__field_{}", i),
                            proc_macro2::Span::call_site(),
                        );
                        quote! { let #temp_name = #var_expr; }
                    }
                }
                FieldInjectKind::External => {
                    // Resolve via ctx.resolve_external — for types registered via DynProvider
                    // but not annotated with #[injectable].
                    let var_expr = quote! {
                        ctx.resolve_external::<#field_ty>().await?
                    };
                    if let Some(name) = &field.name {
                        quote! { let #name = #var_expr; }
                    } else {
                        let temp_name = syn::Ident::new(
                            &format!("__field_{}", i),
                            proc_macro2::Span::call_site(),
                        );
                        quote! { let #temp_name = #var_expr; }
                    }
                }
                FieldInjectKind::Factory(factory_path) => {
                    // use_factory_async: context-based factory.
                    // Signature: async fn(ctx: &ResolveContext) -> Result<T, E>
                    // For creating new values from scratch (external types, DB pools, etc.).
                    let ty_str = &field.ty_string;
                    let wrap = factory_wrap_for_field_type(&field.ty);
                    let var_expr = quote! {
                        {
                            let __v = #factory_path(ctx).await.map_err(|e|
                                injectable_rs_runtime::InjectableError::ConstructionFailed {
                                    type_name: #ty_str,
                                    reason: e.to_string(),
                                })?;
                            #wrap
                        }
                    };
                    if let Some(name) = &field.name {
                        quote! { let #name = #var_expr; }
                    } else {
                        let temp_name = syn::Ident::new(
                            &format!("__field_{}", i),
                            proc_macro2::Span::call_site(),
                        );
                        quote! { let #temp_name = #var_expr; }
                    }
                }
                FieldInjectKind::Provider(factory_path) => {
                    // use_factory_sync: context-based sync factory.
                    // Signature: fn(ctx: &ResolveContext) -> FieldType
                    let var_expr = quote! { #factory_path(ctx) };
                    if let Some(name) = &field.name {
                        quote! { let #name = #var_expr; }
                    } else {
                        let temp_name = syn::Ident::new(
                            &format!("__field_{}", i),
                            proc_macro2::Span::call_site(),
                        );
                        quote! { let #temp_name = #var_expr; }
                    }
                }
            }
        })
        .collect();

    // Generate the struct construction.
    // Do NOT include `#ty_generics` in the struct literal — `Foo<T> { field }`
    // is parsed as a chained comparison, not a struct expression. Rust infers
    // the generic arguments from the function's return type annotation.
    let construction = if fields.is_empty() {
        // Unit struct
        quote! { #type_name }
    } else if fields.first().is_some_and(|f| f.name.is_some()) {
        // Named fields: TypeName { field1, field2, ... }
        // Panic Safety: filter_map skips unnamed fields; unreachable for named structs
        // because all fields in a named struct carry Some(ident).
        let field_names: Vec<_> = fields
            .iter()
            .filter_map(|f| f.name.as_ref())
            .cloned()
            .collect();
        quote! { #type_name { #(#field_names),* } }
    } else {
        // Tuple struct: TypeName(field0, field1, ...)
        let field_refs: Vec<_> = fields
            .iter()
            .enumerate()
            .map(|(i, _)| {
                syn::Ident::new(&format!("__field_{}", i), proc_macro2::Span::call_site())
            })
            .collect();
        quote! { #type_name(#(#field_refs),*) }
    };

    // Generate dependency names for graph metadata.
    // Factory fields (use_factory_async/sync) are NOT DI dependencies — the factory
    // creates the value itself. Only Inject and plain-#[injectable(inject)] fields contribute.
    #[allow(clippy::unnecessary_filter_map)]
    let dep_strings: Vec<String> = fields
        .iter()
        .filter(|f| matches!(f.inject_kind, FieldInjectKind::Inject))
        .filter_map(|f| {
            let ty_str = &f.ty_string;
            // Use AST-based detection so all path forms are handled:
            // Inject<T>, injectable_rs::Inject<T>, Arc<T>, std::sync::Arc<T>, etc.
            // Skip `dyn Trait` inner types — they are not registered graph nodes.
            if extract_inject_dyn_inner(&f.ty).is_some()
                || extract_option_inject_dyn_inner(&f.ty).is_some()
            {
                None
            } else if let Some(inner) = extract_inject_inner(&f.ty) {
                Some(inner)
            } else if let Some(inner) = extract_arc_inner_str(&f.ty) {
                Some(inner)
            } else {
                Some(ty_str.clone())
            }
        })
        .collect();

    // Graph metadata contains concrete type names (e.g. "Database"). For generic
    // types the dep_strings contain the type parameter name ("T"), which is not
    // a registered type. Skip graph metadata for generic types entirely.
    let graph_metadata = if is_generic {
        quote! {}
    } else {
        generate_graph_metadata_from_strings(type_name, &dep_strings, scope)
    };
    let is_singleton: bool = scope != "transient";
    // InjectableArcFactory requires const function pointers and a single TypeId —
    // this only works for concrete (non-generic) types. Generic types are resolved
    // via the blanket `impl<T: Injectable> Extract for Arc<T>` instead.
    let arc_factory_submit = if is_generic {
        quote! {}
    } else {
        generate_arc_factory_submit(type_name, is_singleton)
    };
    // Hooks dispatch uses TypeId::of::<TypeName>() and Arc<TypeName>, which require
    // a concrete type. Skip for generic types (hooks can be added via #[injectable] impl).
    let hooks_dispatch = if is_generic {
        quote! { Ok(instance) }
    } else {
        generate_inventory_hooks_dispatch(type_name)
    };
    let legacy_hooks_submit = if is_generic {
        quote! {}
    } else {
        generate_legacy_hooks_submit(type_name, has_post_construct, false)
    };

    // Provider struct: plain for concrete types, PhantomData-carrying for generic types.
    let provider_struct = if is_generic {
        let phantom = phantom_for_generics(generics);
        quote! { pub struct #provider_name #impl_generics (#phantom); }
    } else {
        quote! { pub struct #provider_name; }
    };

    quote! {
        #provider_struct

        #[async_trait::async_trait]
        impl #impl_generics injectable_rs_runtime::Provider<#type_name #ty_generics>
            for #provider_name #ty_generics
        #where_clause
        {
            async fn provide(
                ctx: &injectable_rs_runtime::ResolveContext,
            ) -> injectable_rs_runtime::InjectableResult<#type_name #ty_generics> {
                #(#field_statements)*
                let instance = #construction;
                #hooks_dispatch
            }
        }

        impl #impl_generics injectable_rs_runtime::Injectable for #type_name #ty_generics
        #where_clause
        {
            type Provider = #provider_name #ty_generics;
            const IS_SINGLETON: bool = #is_singleton;
        }

        #graph_metadata
        #arc_factory_submit
        #legacy_hooks_submit
    }
}

/// Build a `PhantomData<(T1, T2, ..., &'a (), &'b (), ...)>` expression
/// from a generic parameter list, so the provider struct is well-formed.
pub(crate) fn phantom_for_generics(generics: &syn::Generics) -> proc_macro2::TokenStream {
    let types: Vec<_> = generics
        .type_params()
        .map(|tp| {
            let id = &tp.ident;
            quote! { #id }
        })
        .collect();
    let lifetimes: Vec<_> = generics
        .lifetimes()
        .map(|lp| {
            let lt = &lp.lifetime;
            quote! { &#lt () }
        })
        .collect();
    quote! { ::std::marker::PhantomData<(#(#types,)* #(#lifetimes,)*)> }
}

/// Generate an `inventory::submit!` for `InjectableArcFactory` so this
/// Injectable type can be resolved via `try_resolve_external`. This is
/// needed when the type appears as `Arc<T>` in another type's constructor
/// alongside external (DynProvider) types.
///
/// Uses named `fn` helpers (not closures) so the factory entry is
/// `const`-constructible for `inventory::submit!` static initializers.
pub(crate) fn generate_arc_factory_submit(
    type_name: &syn::Ident,
    is_singleton: bool,
) -> TokenStream {
    let type_id_fn_name = syn::Ident::new(
        &format!("__injectable_type_id_{}", type_name),
        proc_macro2::Span::call_site(),
    );
    let provide_fn_name = syn::Ident::new(
        &format!("__injectable_provide_{}", type_name),
        proc_macro2::Span::call_site(),
    );

    // Singleton: go through Extract for Arc<T> which calls resolve_singleton_arc internally.
    // Transient: call the provider directly to get a fresh Arc each time.
    // Using `Extract for Arc<T>` (a pub trait) avoids calling the pub(crate)
    // resolve_singleton_arc method from generated code in user crates.
    let resolve_expr = if is_singleton {
        quote! {
            <::std::sync::Arc<#type_name> as injectable_rs_runtime::Extract>::extract(&ctx).await
        }
    } else {
        quote! {
            <#type_name as injectable_rs_runtime::Injectable>::Provider::provide(&ctx)
                .await
                .map(::std::sync::Arc::new)
        }
    };

    quote! {
        #[doc(hidden)]
        #[allow(non_snake_case)]
        fn #type_id_fn_name() -> ::std::any::TypeId {
            // Keyed by Arc<T> so ProviderRegistry::resolve::<Arc<T>>() can find it.
            ::std::any::TypeId::of::<::std::sync::Arc<#type_name>>()
        }

        #[doc(hidden)]
        #[allow(non_snake_case)]
        fn #provide_fn_name(
            ctx: ::std::sync::Arc<injectable_rs_runtime::ResolveContext>,
        ) -> ::std::pin::Pin<Box<dyn ::std::future::Future<
            Output = injectable_rs_runtime::InjectableResult<Box<dyn ::std::any::Any + Send>>
        > + Send + 'static>> {
            Box::pin(async move {
                #resolve_expr
                    .map(|arc| -> Box<dyn ::std::any::Any + Send> { Box::new(arc) })
            })
        }

        injectable_rs_runtime::inventory::submit! {
            injectable_rs_runtime::InjectableArcFactory::new_const(
                stringify!(#type_name),
                #type_id_fn_name,
                #provide_fn_name,
            )
        }
    }
}

/// Generate the graph node metadata from owned strings.
///
/// Generates an `inventory::submit!` call that registers this type's
/// `GraphNode` for automatic collection at container build time.
/// This eliminates the need for manual registration in `container!()`.
fn generate_graph_metadata_from_strings(
    type_name: &syn::Ident,
    dependencies: &[String],
    scope: &str,
) -> TokenStream {
    let type_str = type_name.to_string();

    if dependencies.is_empty() {
        quote! {
            /// Dependency graph metadata for this injectable type.
            inventory::submit! {
                injectable_rs_graph::GraphNode::leaf_with_scope(
                    #type_str,
                    #scope,
                )
            }
        }
    } else {
        let dep_literals: Vec<_> = dependencies
            .iter()
            .map(|d| {
                let d: &str = d;
                quote! { #d }
            })
            .collect();

        let dep_const_name = syn::Ident::new(
            &format!(
                "__INJECTABLE_GRAPH_DEPS_{}",
                type_name.to_string().to_uppercase()
            ),
            proc_macro2::Span::call_site(),
        );

        quote! {
            /// Dependency list for this injectable type.
            #[allow(dead_code)]
            const #dep_const_name: &[&str] = &[#(#dep_literals),*];

            /// Dependency graph metadata for this injectable type.
            inventory::submit! {
                injectable_rs_graph::GraphNode::with_scope(
                    #type_str,
                    #dep_const_name,
                    #scope,
                )
            }
        }
    }
}

/// Determine the expression that wraps the raw factory result `__v: T` to
/// match the declared field type.
///
/// - `Inject<T>` field  → `Inject::new(Arc::new(__v))`
/// - `Arc<T>` field     → `Arc::new(__v)`
/// - plain `T` field    → `__v` (no wrapping)
///
/// Determine how to wrap the raw factory result `__v: T` to match the field type.
///
/// Detection uses the AST-based `extract_inject_inner` / `extract_arc_inner`
/// functions, which check only the final path segment by identifier. This means
/// all path forms — `Arc<T>`, `std::sync::Arc<T>`, `::std::sync::Arc<T>` —
/// are handled correctly.
fn factory_wrap_for_field_type(field_ty: &syn::Type) -> TokenStream {
    if extract_inject_inner(field_ty).is_some() {
        // Inject<T> field → wrap in Inject::new(Arc::new(__v))
        quote! { injectable_rs_runtime::Inject::new(::std::sync::Arc::new(__v)) }
    } else if extract_arc_inner(field_ty).is_some() {
        // Arc<T> field → wrap in Arc::new(__v)
        quote! { ::std::sync::Arc::new(__v) }
    } else {
        // Plain T field → use __v directly
        quote! { __v }
    }
}

/// Create the provider identifier from the type name.
fn provider_ident(type_name: &syn::Ident) -> syn::Ident {
    syn::Ident::new(
        &format!("{}Provider", type_name),
        proc_macro2::Span::call_site(),
    )
}

/// Generate the runtime inventory hooks dispatch block for field-injection providers.
///
/// Scans `InjectableHooksEntry` inventory at runtime and calls the `post_construct`
/// hook if one is registered for this type. This enables `#[injectable]` (no
/// constructor) to add `#[post_construct]` hooks to a `#[injectable]` type
/// without any extra struct annotations.
///
/// Only **post_construct** is dispatched here (no `T: Clone` required).
/// **pre_destruct** in the field-injection path is NOT supported via inventory;
/// use `#[injectable(has_pre_destruct)]` + manual `impl PreDestruct`, or use
/// `#[injectable]` with `#[injectable_ctor]`.
pub(crate) fn generate_inventory_hooks_dispatch(type_name: &syn::Ident) -> TokenStream {
    let type_str = type_name.to_string();
    quote! {
        // Scan inventory at runtime for a post_construct hook for this type.
        let mut __post_fn_opt: Option<injectable_rs_runtime::PostConstructFnPtr> = None;
        let __type_id = ::std::any::TypeId::of::<#type_name>();
        for __h in injectable_rs_runtime::inventory::iter::<injectable_rs_runtime::InjectableHooksEntry>() {
            if __h.type_id() == __type_id {
                __post_fn_opt = __h.post_construct_fn();
                break;
            }
        }

        if let Some(__post_fn) = __post_fn_opt {
            // Temporarily wrap in Arc so the type-erased hook can call &self methods.
            // Arc::try_unwrap always succeeds: the hook receives a *separate* clone
            // of the Arc via Arc::downcast, and that clone is dropped when the hook
            // future completes, leaving exactly one owner (__post_arc).
            let __post_arc: ::std::sync::Arc<#type_name> = ::std::sync::Arc::new(instance);
            // Separate the clone from the coercion to avoid type inference issues.
            let __post_arc_cloned: ::std::sync::Arc<#type_name> = ::std::sync::Arc::clone(&__post_arc);
            let __arc_any: ::std::sync::Arc<dyn ::std::any::Any + ::std::marker::Send + ::std::marker::Sync>
                = __post_arc_cloned;
            __post_fn(__arc_any).await.map_err(|e|
                injectable_rs_runtime::InjectableError::LifecycleHookFailed {
                    type_name: #type_str,
                    hook: "post_construct",
                    reason: e.to_string(),
                }
            )?;
            // The hook consumed its clone via Arc::downcast; we are the sole owner.
            let instance = match ::std::sync::Arc::try_unwrap(__post_arc) {
                Ok(v) => v,
                Err(_) => panic!(
                    "post_construct hook must not retain the Arc past its completion"
                ),
            };
            Ok(instance)
        } else {
            Ok(instance)
        }
    }
}

/// Generate a backward-compat `InjectableHooksEntry` inventory submit for types
/// that use `#[injectable(has_post_construct)]` or `has_pre_destruct` on the struct.
///
/// These types implement `PostConstruct`/`PreDestruct` manually.
/// We bridge them into the inventory system so the field-injection provider can
/// call them via the unified hooks dispatch without needing the old static flag.
pub(crate) fn generate_legacy_hooks_submit(
    type_name: &syn::Ident,
    has_post_construct: bool,
    has_pre_destruct: bool,
) -> TokenStream {
    if !has_post_construct && !has_pre_destruct {
        return quote! {};
    }

    let post_fn_name = syn::Ident::new(
        &format!("__injectable_legacy_post_{}", type_name),
        proc_macro2::Span::call_site(),
    );
    let pre_fn_name = syn::Ident::new(
        &format!("__injectable_legacy_make_pre_{}", type_name),
        proc_macro2::Span::call_site(),
    );
    let pre_adapter_name = syn::Ident::new(
        &format!("__InjectableLegacyPreDestruct_{}", type_name),
        proc_macro2::Span::call_site(),
    );

    let post_part = if has_post_construct {
        quote! {
            #[doc(hidden)]
            #[allow(non_snake_case)]
            fn #post_fn_name(
                arc: ::std::sync::Arc<dyn ::std::any::Any + ::std::marker::Send + ::std::marker::Sync>,
            ) -> ::std::pin::Pin<Box<dyn ::std::future::Future<
                Output = injectable_rs_runtime::HookResult
            > + ::std::marker::Send + 'static>> {
                let typed = ::std::sync::Arc::downcast::<#type_name>(arc)
                    .expect("InjectableHooksEntry TypeId guarantees correct type");
                Box::pin(async move {
                    injectable_rs_runtime::PostConstruct::post_construct(&*typed).await
                })
            }
        }
    } else {
        quote! {}
    };

    let pre_part = if has_pre_destruct {
        quote! {
            #[doc(hidden)]
            #[allow(non_camel_case_types)]
            struct #pre_adapter_name(::std::sync::Arc<dyn ::std::any::Any + ::std::marker::Send + ::std::marker::Sync>);

            #[async_trait::async_trait]
            impl injectable_rs_runtime::PreDestruct for #pre_adapter_name {
                async fn pre_destruct(&self) -> injectable_rs_runtime::HookResult {
                    let typed = ::std::sync::Arc::downcast::<#type_name>(
                        ::std::sync::Arc::clone(&self.0)
                    ).expect("InjectableHooksEntry TypeId guarantees correct type");
                    injectable_rs_runtime::PreDestruct::pre_destruct(&*typed).await
                }
            }

            #[doc(hidden)]
            #[allow(non_snake_case)]
            fn #pre_fn_name(
                arc: ::std::sync::Arc<dyn ::std::any::Any + ::std::marker::Send + ::std::marker::Sync>,
            ) -> ::std::sync::Arc<dyn injectable_rs_runtime::PreDestruct> {
                ::std::sync::Arc::new(#pre_adapter_name(arc))
            }
        }
    } else {
        quote! {}
    };

    let post_fn_ref = if has_post_construct {
        quote! { Some(#post_fn_name as injectable_rs_runtime::PostConstructFnPtr) }
    } else {
        quote! { None }
    };
    let pre_fn_ref = if has_pre_destruct {
        quote! { Some(#pre_fn_name as injectable_rs_runtime::MakePreDestructFnPtr) }
    } else {
        quote! { None }
    };

    quote! {
        #post_part
        #pre_part

        injectable_rs_runtime::inventory::submit! {
            injectable_rs_runtime::InjectableHooksEntry::new_const(
                || ::std::any::TypeId::of::<#type_name>(),
                #post_fn_ref,
                #pre_fn_ref,
            )
        }
    }
}