1
1
use crate :: io:: SliceReader ;
2
+ use crate :: transformer:: IdentityAssetTransformer ;
2
3
use crate :: {
3
4
io:: {
4
5
AssetReaderError , AssetWriterError , MissingAssetWriterError ,
@@ -47,6 +48,11 @@ pub trait Process: Send + Sync + Sized + 'static {
47
48
/// an [`AssetSaver`] that allows you save any `S` asset. However you can
48
49
/// also implement [`Process`] directly if [`LoadTransformAndSave`] feels limiting or unnecessary.
49
50
///
51
+ /// If your [`Process`] does not need to transform the [`Asset`], you can use [`IdentityAssetTransformer`] as `T`.
52
+ /// This will directly return the input [`Asset`], allowing your [`Process`] to directly load and then save an [`Asset`].
53
+ /// However, this pattern should only be used for cases such as file format conversion.
54
+ /// Otherwise, consider refactoring your [`AssetLoader`] and [`AssetSaver`] to isolate the transformation step into an explicit [`AssetTransformer`].
55
+ ///
50
56
/// This uses [`LoadTransformAndSaveSettings`] to configure the processor.
51
57
///
52
58
/// [`Asset`]: crate::Asset
@@ -60,6 +66,18 @@ pub struct LoadTransformAndSave<
60
66
marker : PhantomData < fn ( ) -> L > ,
61
67
}
62
68
69
+ impl < L : AssetLoader , S : AssetSaver < Asset = L :: Asset > > From < S >
70
+ for LoadTransformAndSave < L , IdentityAssetTransformer < L :: Asset > , S >
71
+ {
72
+ fn from ( value : S ) -> Self {
73
+ LoadTransformAndSave {
74
+ transformer : IdentityAssetTransformer :: new ( ) ,
75
+ saver : value,
76
+ marker : PhantomData ,
77
+ }
78
+ }
79
+ }
80
+
63
81
/// Settings for the [`LoadTransformAndSave`] [`Process::Settings`] implementation.
64
82
///
65
83
/// `LoaderSettings` corresponds to [`AssetLoader::Settings`], `TransformerSettings` corresponds to [`AssetTransformer::Settings`],
@@ -98,30 +116,16 @@ impl<
98
116
/// This uses [`LoadAndSaveSettings`] to configure the processor.
99
117
///
100
118
/// [`Asset`]: crate::Asset
101
- pub struct LoadAndSave < L : AssetLoader , S : AssetSaver < Asset = L :: Asset > > {
102
- saver : S ,
103
- marker : PhantomData < fn ( ) -> L > ,
104
- }
105
-
106
- impl < L : AssetLoader , S : AssetSaver < Asset = L :: Asset > > From < S > for LoadAndSave < L , S > {
107
- fn from ( value : S ) -> Self {
108
- LoadAndSave {
109
- saver : value,
110
- marker : PhantomData ,
111
- }
112
- }
113
- }
119
+ #[ deprecated = "Use `LoadTransformAndSave<L, IdentityAssetTransformer<<L as AssetLoader>::Asset>, S>` instead" ]
120
+ pub type LoadAndSave < L , S > =
121
+ LoadTransformAndSave < L , IdentityAssetTransformer < <L as AssetLoader >:: Asset > , S > ;
114
122
115
123
/// Settings for the [`LoadAndSave`] [`Process::Settings`] implementation.
116
124
///
117
125
/// `LoaderSettings` corresponds to [`AssetLoader::Settings`] and `SaverSettings` corresponds to [`AssetSaver::Settings`].
118
- #[ derive( Serialize , Deserialize , Default ) ]
119
- pub struct LoadAndSaveSettings < LoaderSettings , SaverSettings > {
120
- /// The [`AssetLoader::Settings`] for [`LoadAndSave`].
121
- pub loader_settings : LoaderSettings ,
122
- /// The [`AssetSaver::Settings`] for [`LoadAndSave`].
123
- pub saver_settings : SaverSettings ,
124
- }
126
+ #[ deprecated = "Use `LoadTransformAndSaveSettings<LoaderSettings, (), SaverSettings>` instead" ]
127
+ pub type LoadAndSaveSettings < LoaderSettings , SaverSettings > =
128
+ LoadTransformAndSaveSettings < LoaderSettings , ( ) , SaverSettings > ;
125
129
126
130
/// An error that is encountered during [`Process::process`].
127
131
#[ derive( Error , Debug ) ]
@@ -213,36 +217,6 @@ where
213
217
}
214
218
}
215
219
216
- impl < Loader : AssetLoader , Saver : AssetSaver < Asset = Loader :: Asset > > Process
217
- for LoadAndSave < Loader , Saver >
218
- {
219
- type Settings = LoadAndSaveSettings < Loader :: Settings , Saver :: Settings > ;
220
- type OutputLoader = Saver :: OutputLoader ;
221
-
222
- async fn process < ' a > (
223
- & ' a self ,
224
- context : & ' a mut ProcessContext < ' _ > ,
225
- meta : AssetMeta < ( ) , Self > ,
226
- writer : & ' a mut Writer ,
227
- ) -> Result < <Self :: OutputLoader as AssetLoader >:: Settings , ProcessError > {
228
- let AssetAction :: Process { settings, .. } = meta. asset else {
229
- return Err ( ProcessError :: WrongMetaType ) ;
230
- } ;
231
- let loader_meta = AssetMeta :: < Loader , ( ) > :: new ( AssetAction :: Load {
232
- loader : std:: any:: type_name :: < Loader > ( ) . to_string ( ) ,
233
- settings : settings. loader_settings ,
234
- } ) ;
235
- let loaded_asset = context. load_source_asset ( loader_meta) . await ?;
236
- let saved_asset = SavedAsset :: < Loader :: Asset > :: from_loaded ( & loaded_asset) . unwrap ( ) ;
237
- let output_settings = self
238
- . saver
239
- . save ( writer, saved_asset, & settings. saver_settings )
240
- . await
241
- . map_err ( |error| ProcessError :: AssetSaveError ( error. into ( ) ) ) ?;
242
- Ok ( output_settings)
243
- }
244
- }
245
-
246
220
/// A type-erased variant of [`Process`] that enables interacting with processor implementations without knowing
247
221
/// their type.
248
222
pub trait ErasedProcessor : Send + Sync {
0 commit comments