Skip to main content

daab/
arc.rs

1
2//!
3//! Alias module for using `Rc` to wrap `Blueprint` and artifacts.
4//!
5
6
7use std::fmt::Debug;
8use std::any::Any;
9
10use cfg_if::cfg_if;
11
12#[cfg(feature = "diagnostics")]
13use crate::Doctor;
14
15use crate::BlueprintDyn;
16use crate::Never;
17
18
19/// Type for wrapping a `T` as part of `CanType` as `Can`.
20///
21/// This is just an alias for `Arc<T>`.
22///
23pub type BinType<T> = std::sync::Arc<T>;
24
25/// Can for wrappers of this module.
26///
27/// This is just an alias for `Arc<dyn Any + Send + Sync>`.
28///
29pub type CanType = BinType<dyn Any + Send + Sync>;
30
31/// The wrapping type for builders.
32///
33/// Here it is the same as `BinType`.
34///
35pub type BuilderBinType<T> = BinType<T>;
36
37/// The can type for builders.
38///
39pub type BuilderCan = CanType;
40
41/// Wraps a Builder as a blueprint for its artifact from the `Cache`.
42///
43pub type Blueprint<B> = crate::Blueprint<B, CanType>;
44
45cfg_if! {
46	if #[cfg(feature = "unsized")] {
47		/// The unsized variant of `Blueprint`.
48		///
49		pub type BlueprintUnsized<B> = crate::blueprint::BlueprintUnsized<B, CanType>;
50	}
51}
52
53/// An `Blueprint` with a `dyn Builder<Artifact=Artifact>`.
54pub type DynamicBlueprint<Artifact, Err=Never, DynState=()> =
55	BlueprintDyn<CanType, BuilderCan, Artifact, Err, DynState>;
56
57pub type ConstBuilder<T> = crate::utils::ConstBuilder<CanType, BuilderCan, BinType<T>, T>;
58pub type ConfigurableBuilder<T> = crate::utils::ConfigurableBuilder<CanType, BuilderCan, T>;
59
60
61/// Allows to resolve any `Blueprint` into its artifact. Usable within a
62/// builders `build` function.
63///
64/// This resolver uses `Arc` for storing builders and artifacts.
65///
66pub type Resolver<'a, T = ()> = crate::Resolver<'a, CanType, CanType, T>;
67
68/*
69/// Allows to resolve any `Blueprint` into its artifact-builder. Usable
70/// within a super-builders `build` function.
71///
72/// This resolver uses `Arc` for storing builders and `Blueprint` for
73/// storing artifacts, i.e. the artifacts are builders them self.
74///
75pub type SuperResolver<'a, T = ()> = crate::Resolver<'a, BuilderArtifact<CanType>, CanType, T>;
76*/
77
78cfg_if::cfg_if!{
79	if #[cfg(feature = "diagnostics")] {
80		/// Allows to resolve any `Blueprint` into its artifact.
81		///
82		/// This cache uses `Arc` for storing builders and artifacts.
83		///
84		pub type Cache<T = dyn Doctor<CanType, CanType>> =
85			crate::Cache<CanType, CanType, T>;
86
87	} else {
88		/// Allows to resolve any `Blueprint` into its artifact.
89		///
90		/// This cache uses `Arc` for storing builders and artifacts.
91		///
92		pub type Cache = crate::Cache<CanType, CanType>;
93	}
94}
95
96/// The ownable and storable variant of the Cache.
97///
98pub type CacheOwned = crate::CacheOwned<CanType, CanType>;
99
100
101/*
102/// Allows to resolve any `Blueprint` into its artifact-builder.
103///
104/// This cache uses `Arc` for storing builders and `Blueprint` for
105/// storing artifacts, i.e. the artifacts are builders them self.
106///
107#[cfg(not(feature = "diagnostics"))]
108pub type SuperCache = crate::Cache<BuilderArtifact<CanType>, CanType>;
109
110/// Allows to resolve any `Blueprint` into its artifact-builder.
111///
112/// This cache uses `Arc` for storing builders and `Blueprint` for
113/// storing artifacts, i.e. the artifacts are builders them self.
114///
115#[cfg(feature = "diagnostics")]
116pub type SuperCache<T = dyn Doctor<BuilderArtifact<CanType>, CanType>> = crate::Cache<BuilderArtifact<CanType>, CanType, T>;
117*/
118
119/// Functional builder wrapper.
120///
121pub type FunctionalBuilder<F, S = ()> =
122	crate::utils::FunctionalBuilder<CanType, BuilderCan, F, S>;
123
124
125/// A simplified builder interface, intended for implementing builders.
126///
127/// For this trait exists a generic `impl Builder`.
128///
129pub trait SimpleBuilder: Debug + 'static {
130	/// The artifact type as produced by this builder.
131	///
132	type Artifact : Debug + Send + Sync + 'static;
133
134	/// Produces an artifact using the given `Resolver` for resolving
135	/// dependencies.
136	///
137	fn build(&self, resolver: &mut Resolver) -> Self::Artifact;
138}
139
140// Generic impl for legacy builder
141impl<B: ?Sized + SimpleBuilder> Builder for B {
142	type Artifact = B::Artifact;
143
144	type DynState = ();
145
146	type Err = Never;
147
148	fn build(&self, cache: &mut Resolver) -> Result<BinType<Self::Artifact>, Never> {
149		Ok(BinType::new(self.build(cache)))
150	}
151
152	fn init_dyn_state(&self) -> Self::DynState {
153		// Intensional empty, just return a fresh `()`
154	}
155}
156
157
158/// A Builder using `Arc` for `Blueprint` and artifacts.
159///
160pub trait Builder: Debug + 'static {
161	/// The artifact type as produced by this builder.
162	///
163	type Artifact : Debug + Send + Sync + 'static;
164
165	/// Type of the dynamic state of this builder.
166	///
167	type DynState : Debug + 'static;
168
169	/// Error type returned by this Builder in case of failure to produce an
170	/// Artifact.
171	type Err : Debug + 'static;
172
173	/// Produces an artifact using the given `Resolver` for resolving
174	/// dependencies.
175	///
176	fn build(&self, resolver: &mut Resolver<Self::DynState>)
177		-> Result<BinType<Self::Artifact>, Self::Err>;
178
179	/// Return an inital dynamic state for this builder.
180	///
181	fn init_dyn_state(&self) -> Self::DynState;
182}
183
184impl<B: ?Sized + Builder> crate::Builder<CanType, CanType> for B {
185	type Artifact = B::Artifact;
186	type DynState = B::DynState;
187	type Err = B::Err;
188
189	fn build(&self, cache: &mut Resolver<Self::DynState>)
190			-> Result<<CanType as crate::canning::Can<Self::Artifact>>::Bin, Self::Err> {
191
192		self.build(cache)
193	}
194
195	fn init_dyn_state(&self) -> Self::DynState {
196		self.init_dyn_state()
197	}
198}
199
200/*
201/// A builder of builders using `Arc`s.
202///
203/// This cache uses `Arc` for storing builders and `Blueprint` for
204/// storing artifacts, i.e. the artifacts are builders them self.
205///
206pub trait SuperBuilder: Debug + Send + Sync + 'static {
207	/// The artifact type as produced by this builder. It is supposed to be
208	/// another `Builder` (or `SuperBuilder`).
209	///
210	type Artifact : Debug + Send + Sync + 'static;
211
212	/// Type of the dynamic state of this builder.
213	///
214	type DynState : Debug + 'static;
215
216	/// Error type returned by this Builder in case of failure to produce an
217	/// Artifact.
218	type Err : Debug + 'static;
219
220	/// Produces an artifact using the given `Resolver` for resolving
221	/// dependencies.
222	///
223	fn build(&self, resolver: &mut SuperResolver<Self::DynState>)
224		-> Result<Self::Artifact, Self::Err>;
225
226	/// Return an inital dynamic state for this builder.
227	///
228	fn init_dyn_state(&self) -> Self::DynState;
229}
230
231impl<B: ?Sized + SuperBuilder> crate::Builder<BuilderArtifact<CanType>, CanType> for B {
232	type Artifact = B::Artifact;
233	type DynState = B::DynState;
234	type Err = B::Err;
235
236	fn build(&self, cache: &mut SuperResolver<Self::DynState>)
237			-> Result<Self::Artifact, Self::Err> {
238
239		self.build(cache)
240	}
241
242	fn init_dyn_state(&self) -> Self::DynState {
243		self.init_dyn_state()
244	}
245}
246*/
247
248
249#[cfg(test)]
250mod test {
251	include!("test_impl.rs");
252}
253
254#[cfg(test)]
255mod test_cloned {
256	include!("test_impl_cloned.rs");
257}
258
259
260