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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//! Type-erased instance factories for capability registration.
//!
//! These closures produce fresh instances of an extension's concrete type
//! on demand. They are the bridge between the extension's instance policy
//! (`Cloned` vs `Fresh`, chosen at the builder) and the capability
//! registry, which consumes them to mint per-node capability entries.
//!
//! Living in [`crate::capability`] rather than [`crate::extension`]
//! breaks the module-level cycle between the two folders: the capability
//! registry (and [`ExtensionCapabilities`](super::ExtensionCapabilities))
//! needs these types to describe registration fn pointers, while the
//! extension builder needs them to encode instance policy. Placing them
//! with the registry side makes the dependency one-way:
//! `extension -> capability`.
use Any;
// -- Shared -------------------------------------------------------------------
/// Produces instances of a shared extension's concrete type for capability
/// consumers.
///
/// The wrapper is type-erased over the extension's concrete type `E`. When
/// the engine wires a consumer's capability binding, the generated
/// registration glue (see `extension_capabilities!`) downcasts the returned
/// `Box<dyn Any + Send>` back to `E` and wraps it in the requested
/// capability trait object.
///
/// The instance policy chosen at the builder (`.cloned(...)` vs
/// `.fresh(...)`) is baked into the stored closure:
/// - **Cloned** -- the closure captures a prototype `E: Clone`
/// and returns `e.clone()` on each call.
/// - **Fresh** -- the closure captures the user-supplied
/// `Fn() -> E` and invokes it on each call.
///
/// `SharedInstanceFactory` is [`Clone`]: one extension may provide
/// multiple capabilities, and each capability registration needs its
/// own copy of the factory for its produce closure.
/// Object-safe `Fn + Clone` helper for [`SharedInstanceFactory`].
///
/// `Box<dyn Fn>` is not `Clone`, so we thread cloning through an
/// object-safe `clone_box` method. The blanket impl below covers any
/// closure that is `Fn + Send + Clone + 'static`.
// -- Local --------------------------------------------------------------------
/// Produces instances of a local (!Send) extension's concrete type for
/// capability consumers. See [`SharedInstanceFactory`] for background.
///
/// The instance policy chosen at the builder (`.cloned(...)` vs
/// `.constructed(...)`) is baked into the stored closure. Both policies
/// mint a fresh `Box<E>` per call, mirroring the shared-side contract:
/// - **Cloned** -- the closure captures a prototype `E: Clone`
/// and returns `Box::new(e.clone())` on each call.
/// - **Constructed** -- the closure captures the user-supplied
/// `Fn() -> E` and boxes its output on each call.
///
/// `LocalInstanceFactory` is [`Clone`] for the same reason as its
/// shared counterpart.
/// Object-safe `Fn + Clone` helper for [`LocalInstanceFactory`].