bevy_asset 0.19.0

Provides asset functionality for Bevy Engine
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
use crate::{
    io::{AssetWriterError, MissingAssetSourceError, MissingAssetWriterError, Writer},
    meta::{AssetAction, AssetMeta, AssetMetaDyn, Settings},
    transformer::TransformedAsset,
    Asset, AssetContainer, AssetId, AssetLoader, AssetPath, AssetServer, ErasedLoadedAsset, Handle,
    LabeledAsset, UntypedAssetId, UntypedHandle,
};
use alloc::{boxed::Box, string::ToString, sync::Arc, vec::Vec};
use atomicow::CowArc;
use bevy_ecs::error::BevyError;
use bevy_platform::collections::{hash_map::Entry, HashMap};
use bevy_reflect::TypePath;
use bevy_tasks::{BoxedFuture, ConditionalSendFuture};
use core::{any::TypeId, borrow::Borrow, ops::Deref};
use futures_lite::AsyncWriteExt;
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Saves an [`Asset`] of a given [`AssetSaver::Asset`] type. [`AssetSaver::OutputLoader`] will then be used to load the saved asset
/// in the final deployed application. The saver should produce asset bytes in a format that [`AssetSaver::OutputLoader`] can read.
///
/// This trait is generally used in concert with [`AssetWriter`](crate::io::AssetWriter) to write assets as bytes.
///
/// For a version of this trait that can load assets, see [`AssetLoader`].
///
/// Note: This is currently only leveraged by the [`AssetProcessor`](crate::processor::AssetProcessor), and does not provide a
/// suitable interface for general purpose asset persistence. See [github issue #11216](https://github.com/bevyengine/bevy/issues/11216).
///
pub trait AssetSaver: TypePath + Send + Sync + 'static {
    /// The top level [`Asset`] saved by this [`AssetSaver`].
    type Asset: Asset;
    /// The settings type used by this [`AssetSaver`].
    type Settings: Settings + Default + Serialize + for<'a> Deserialize<'a>;
    /// The type of [`AssetLoader`] used to load this [`Asset`]
    type OutputLoader: AssetLoader;
    /// The type of [error](`std::error::Error`) which could be encountered by this saver.
    type Error: Into<BevyError>;

    /// Saves the given runtime [`Asset`] by writing it to a byte format using `writer`. The passed in `settings` can influence how the
    /// `asset` is saved.
    fn save(
        &self,
        writer: &mut Writer,
        asset: SavedAsset<'_, '_, Self::Asset>,
        settings: &Self::Settings,
        asset_path: AssetPath<'_>,
    ) -> impl ConditionalSendFuture<
        Output = Result<<Self::OutputLoader as AssetLoader>::Settings, Self::Error>,
    >;
}

/// A type-erased dynamic variant of [`AssetSaver`] that allows callers to save assets without knowing the actual type of the [`AssetSaver`].
pub trait ErasedAssetSaver: Send + Sync + 'static {
    /// Saves the given runtime [`ErasedLoadedAsset`] by writing it to a byte format using `writer`. The passed in `settings` can influence how the
    /// `asset` is saved.
    fn save<'a>(
        &'a self,
        writer: &'a mut Writer,
        asset: &'a ErasedLoadedAsset,
        settings: &'a dyn Settings,
        asset_path: AssetPath<'a>,
    ) -> BoxedFuture<'a, Result<(), BevyError>>;

    /// The type name of the [`AssetSaver`].
    fn type_name(&self) -> &'static str;
}

impl<S: AssetSaver> ErasedAssetSaver for S {
    fn save<'a>(
        &'a self,
        writer: &'a mut Writer,
        asset: &'a ErasedLoadedAsset,
        settings: &'a dyn Settings,
        asset_path: AssetPath<'a>,
    ) -> BoxedFuture<'a, Result<(), BevyError>> {
        Box::pin(async move {
            let settings = settings
                .downcast_ref::<S::Settings>()
                .expect("AssetLoader settings should match the loader type");
            let saved_asset = SavedAsset::<S::Asset>::from_loaded(asset).unwrap();
            if let Err(err) = self.save(writer, saved_asset, settings, asset_path).await {
                return Err(err.into());
            }
            Ok(())
        })
    }
    fn type_name(&self) -> &'static str {
        core::any::type_name::<S>()
    }
}

/// An [`Asset`] (and any labeled "sub assets") intended to be saved.
#[derive(Clone)]
pub struct SavedAsset<'a, 'b, A: Asset> {
    value: &'a A,
    labeled_assets: Moo<'b, Vec<LabeledSavedAsset<'a>>>,
    label_to_asset_index: Moo<'b, HashMap<CowArc<'a, str>, usize>>,
    /// The mapping from a subasset asset IDs to their index in [`Self::labeled_assets`].
    ///
    /// This is entirely redundant with [`Self::labeled_assets`], but it allows looking up the
    /// labeled asset by its asset ID.
    asset_id_to_asset_index: Moo<'b, HashMap<UntypedAssetId, usize>>,
}

impl<A: Asset> Deref for SavedAsset<'_, '_, A> {
    type Target = A;

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

impl<'a, 'b, A: Asset> SavedAsset<'a, 'b, A> {
    fn from_value_and_labeled_saved_assets(
        value: &'a A,
        labeled_saved_assets: &'b Vec<LabeledSavedAsset<'a>>,
        label_to_asset_index: &'b HashMap<CowArc<'a, str>, usize>,
        asset_id_to_asset_index: &'b HashMap<UntypedAssetId, usize>,
    ) -> Self {
        Self {
            value,
            labeled_assets: Moo::Borrowed(labeled_saved_assets),
            label_to_asset_index: Moo::Borrowed(label_to_asset_index),
            asset_id_to_asset_index: Moo::Borrowed(asset_id_to_asset_index),
        }
    }

    fn from_value_and_labeled_assets(
        value: &'a A,
        labeled_assets: &'a [LabeledAsset],
        label_to_asset_index: &'a HashMap<CowArc<'static, str>, usize>,
        asset_id_to_asset_index: &'a HashMap<UntypedAssetId, usize>,
    ) -> Self {
        Self {
            value,
            labeled_assets: Moo::Owned(
                labeled_assets
                    .iter()
                    .map(LabeledSavedAsset::from_labeled_asset)
                    .collect(),
            ),
            label_to_asset_index: Moo::Owned(
                label_to_asset_index
                    .iter()
                    .map(|(label, &index)| (CowArc::Borrowed(label.borrow()), index))
                    .collect(),
            ),
            asset_id_to_asset_index: Moo::Borrowed(asset_id_to_asset_index),
        }
    }

    /// Creates a new [`SavedAsset`] from `asset` if its internal value matches `A`.
    pub fn from_loaded(asset: &'a ErasedLoadedAsset) -> Option<Self> {
        let value = asset.value.downcast_ref::<A>()?;
        Some(Self::from_value_and_labeled_assets(
            value,
            &asset.labeled_assets,
            &asset.label_to_asset_index,
            &asset.asset_id_to_asset_index,
        ))
    }

    /// Creates a new [`SavedAsset`] from the a [`TransformedAsset`]
    pub fn from_transformed(asset: &'a TransformedAsset<A>) -> Self {
        Self::from_value_and_labeled_assets(
            &asset.value,
            &asset.labeled_assets,
            &asset.label_to_asset_index,
            &asset.asset_id_to_asset_index,
        )
    }

    /// Creates a new [`SavedAsset`] holding only the provided value with no labeled assets.
    pub fn from_asset(value: &'a A) -> Self {
        Self {
            value,
            labeled_assets: Moo::Owned(Vec::default()),
            label_to_asset_index: Moo::Owned(HashMap::default()),
            asset_id_to_asset_index: Moo::Owned(HashMap::default()),
        }
    }

    /// Casts this typed asset into its type-erased form.
    pub fn upcast(self) -> ErasedSavedAsset<'a, 'a>
    where
        'b: 'a,
    {
        ErasedSavedAsset {
            value: self.value,
            labeled_assets: self.labeled_assets,
            label_to_asset_index: self.label_to_asset_index,
            asset_id_to_asset_index: self.asset_id_to_asset_index,
        }
    }

    /// Retrieves the value of this asset.
    #[inline]
    pub fn get(&self) -> &'a A {
        self.value
    }

    /// Returns the labeled asset, if it exists and matches this type.
    pub fn get_labeled<B: Asset>(&self, label: impl AsRef<str>) -> Option<SavedAsset<'a, '_, B>> {
        let index = self.label_to_asset_index.get(label.as_ref())?;
        let labeled = &self.labeled_assets[*index];
        labeled.asset.downcast()
    }

    /// Returns the type-erased labeled asset, if it exists and matches this type.
    pub fn get_erased_labeled(&self, label: impl AsRef<str>) -> Option<&ErasedSavedAsset<'a, '_>> {
        let index = self.label_to_asset_index.get(label.as_ref())?;
        let labeled = &self.labeled_assets[*index];
        Some(&labeled.asset)
    }

    /// Returns the labeled asset given its asset ID if it exists and matches the type.
    ///
    /// This can be used to get the asset from its handle since `&Handle` implements
    /// [`Into<AssetId<B>>`].
    pub fn get_labeled_by_id<B: Asset>(
        &self,
        id: impl Into<AssetId<B>>,
    ) -> Option<SavedAsset<'a, '_, B>> {
        let index = self.asset_id_to_asset_index.get(&id.into().untyped())?;
        let labeled = &self.labeled_assets[*index];
        labeled.asset.downcast()
    }

    /// Returns the type-erased labeled asset given its asset ID if it exists.
    ///
    /// This can be used to get the asset from its handle since `&UntypedHandle` implements
    /// [`Into<UntypedAssetId>`].
    pub fn get_erased_labeled_by_id(
        &self,
        id: impl Into<UntypedAssetId>,
    ) -> Option<&ErasedSavedAsset<'a, '_>> {
        let index = self.asset_id_to_asset_index.get(&id.into())?;
        let labeled = &self.labeled_assets[*index];
        Some(&labeled.asset)
    }

    /// Returns the [`UntypedHandle`] of the labeled asset with the provided 'label', if it exists.
    pub fn get_untyped_handle(&self, label: impl AsRef<str>) -> Option<UntypedHandle> {
        let index = self.label_to_asset_index.get(label.as_ref())?;
        let labeled = &self.labeled_assets[*index];
        Some(labeled.handle.clone())
    }

    /// Returns the [`Handle`] of the labeled asset with the provided 'label', if it exists and is an asset of type `B`
    pub fn get_handle<B: Asset>(&self, label: impl AsRef<str>) -> Option<Handle<B>> {
        let index = self.label_to_asset_index.get(label.as_ref())?;
        let labeled = &self.labeled_assets[*index];
        if let Ok(handle) = labeled.handle.clone().try_typed::<B>() {
            return Some(handle);
        }
        None
    }

    /// Iterate over all labels for "labeled assets" in the loaded asset
    pub fn iter_labels(&self) -> impl Iterator<Item = &str> {
        self.label_to_asset_index.keys().map(|s| &**s)
    }
}

#[derive(Clone)]
pub struct ErasedSavedAsset<'a: 'b, 'b> {
    value: &'a dyn AssetContainer,
    labeled_assets: Moo<'b, Vec<LabeledSavedAsset<'a>>>,
    label_to_asset_index: Moo<'b, HashMap<CowArc<'a, str>, usize>>,
    /// The mapping from a subasset asset IDs to their index in [`Self::labeled_assets`].
    ///
    /// This is entirely redundant with [`Self::labeled_assets`], but it allows looking up the
    /// labeled asset by its asset ID.
    asset_id_to_asset_index: Moo<'b, HashMap<UntypedAssetId, usize>>,
}

impl<'a> ErasedSavedAsset<'a, '_> {
    fn from_loaded(asset: &'a ErasedLoadedAsset) -> Self {
        Self {
            value: &*asset.value,
            labeled_assets: Moo::Owned(
                asset
                    .labeled_assets
                    .iter()
                    .map(LabeledSavedAsset::from_labeled_asset)
                    .collect(),
            ),
            label_to_asset_index: Moo::Owned(
                asset
                    .label_to_asset_index
                    .iter()
                    .map(|(label, &index)| (CowArc::Borrowed(label.borrow()), index))
                    .collect(),
            ),
            asset_id_to_asset_index: Moo::Borrowed(&asset.asset_id_to_asset_index),
        }
    }
}

impl<'a> ErasedSavedAsset<'a, '_> {
    /// Attempts to downcast this erased asset into type `A`.
    ///
    /// Returns [`None`] if the asset is the wrong type.
    pub fn downcast<'b, A: Asset>(&'b self) -> Option<SavedAsset<'a, 'b, A>> {
        let value = self.value.downcast_ref::<A>()?;
        Some(SavedAsset::from_value_and_labeled_saved_assets(
            value,
            &self.labeled_assets,
            &self.label_to_asset_index,
            &self.asset_id_to_asset_index,
        ))
    }
}

/// Container for a single labeled asset (which also includes its labeled assets, for nested
/// assets).
#[derive(Clone)]
struct LabeledSavedAsset<'a> {
    /// The asset and its labeled assets.
    asset: ErasedSavedAsset<'a, 'a>,
    /// The handle of this labeled asset.
    handle: UntypedHandle,
}

impl<'a> LabeledSavedAsset<'a> {
    /// Creates an instance that corresponds to the same data as [`LabeledAsset`].
    fn from_labeled_asset(asset: &'a LabeledAsset) -> Self {
        Self {
            asset: ErasedSavedAsset::from_loaded(&asset.asset),
            handle: asset.handle.clone(),
        }
    }
}

/// A builder for creating [`SavedAsset`] instances (for use with asset saving).
///
/// This is commonly used in tandem with [`save_using_saver`].
pub struct SavedAssetBuilder<'a> {
    /// The labeled assets for this saved asset.
    labeled_assets: Vec<LabeledSavedAsset<'a>>,
    /// Maps the labels of subassets to their index in [`Self::labeled_assets`].
    label_to_asset_index: HashMap<CowArc<'a, str>, usize>,
    /// The mapping from a subasset asset IDs to their index in [`Self::labeled_assets`].
    ///
    /// This is entirely redundant with [`Self::labeled_assets`], but it allows looking up the
    /// labeled asset by its asset ID.
    asset_id_to_asset_index: HashMap<UntypedAssetId, usize>,
    /// The asset path (with no label) that this saved asset is "tied" to.
    ///
    /// All labeled assets will use this asset path (with their substituted labels). Note labeled
    /// assets **of labeled assets** may not use the same asset path (to represent nested-loaded
    /// assets).
    asset_path: AssetPath<'static>,
    /// The asset server to use for creating handles.
    asset_server: AssetServer,
}

impl<'a> SavedAssetBuilder<'a> {
    /// Creates a new builder for the given `asset_path` and using the `asset_server` to back its
    /// handles.
    pub fn new(asset_server: AssetServer, mut asset_path: AssetPath<'static>) -> Self {
        asset_path.remove_label();
        Self {
            asset_server,
            asset_path,
            labeled_assets: Default::default(),
            label_to_asset_index: Default::default(),
            asset_id_to_asset_index: Default::default(),
        }
    }

    /// Adds a labeled asset, creates a handle for it, and returns the handle (for use in creating
    /// an asset).
    ///
    /// This is primarily used when **constructing** a new asset to be saved. Since assets commonly
    /// store handles to their subassets, this function returns a handle that can be stored in your
    /// root asset.
    ///
    /// If you already have a root asset instance (which already contains a subasset handle), use
    /// [`Self::add_labeled_asset_with_existing_handle`] instead.
    #[must_use]
    pub fn add_labeled_asset_with_new_handle<'b: 'a, A: Asset>(
        &mut self,
        label: impl Into<CowArc<'b, str>>,
        asset: SavedAsset<'a, 'a, A>,
    ) -> Handle<A> {
        let label = label.into();
        let handle = Handle::Strong(
            self.asset_server
                .read_infos()
                .handle_providers
                .get(&TypeId::of::<A>())
                .expect("asset type has been initialized")
                .reserve_handle_internal(
                    false,
                    Some(self.asset_path.clone().with_label(label.to_string())),
                    None,
                ),
        );
        self.add_labeled_asset_with_existing_handle(label, asset, handle.clone());
        handle
    }

    /// Adds a labeled asset with a pre-existing handle.
    ///
    /// This is primarily used when attempting to save a (root) asset that you already have an
    /// instance of. Since this root asset instance already must have its fields populated
    /// (including any subasset handles), this function allows you to record the subasset that
    /// should be associated with that handle.
    ///
    /// If you do not have a root asset instance (you're creating one from scratch), use
    /// [`Self::add_labeled_asset_with_new_handle`] instead.
    pub fn add_labeled_asset_with_existing_handle<'b: 'a, A: Asset>(
        &mut self,
        label: impl Into<CowArc<'b, str>>,
        asset: SavedAsset<'a, 'a, A>,
        handle: Handle<A>,
    ) {
        self.add_labeled_asset_with_existing_handle_erased(
            label.into(),
            asset.upcast(),
            handle.untyped(),
        );
    }

    /// Same as [`Self::add_labeled_asset_with_new_handle`], but type-erased to allow for dynamic
    /// types.
    #[must_use]
    pub fn add_labeled_asset_with_new_handle_erased<'b: 'a>(
        &mut self,
        label: impl Into<CowArc<'b, str>>,
        asset: ErasedSavedAsset<'a, 'a>,
    ) -> UntypedHandle {
        let label = label.into();
        let handle = UntypedHandle::Strong(
            self.asset_server
                .read_infos()
                .handle_providers
                .get(&asset.value.type_id())
                .expect("asset type has been initialized")
                .reserve_handle_internal(
                    false,
                    Some(self.asset_path.clone().with_label(label.to_string())),
                    None,
                ),
        );
        self.add_labeled_asset_with_existing_handle_erased(label, asset, handle.clone());
        handle
    }

    /// Same as [`Self::add_labeled_asset_with_existing_handle`], but type-erased to allow for
    /// dynamic types.
    pub fn add_labeled_asset_with_existing_handle_erased<'b: 'a>(
        &mut self,
        label: impl Into<CowArc<'b, str>>,
        asset: ErasedSavedAsset<'a, 'a>,
        handle: UntypedHandle,
    ) {
        // TODO: Check asset and handle have the same type.
        let labeled = LabeledSavedAsset { asset, handle };
        match self.label_to_asset_index.entry(label.into()) {
            Entry::Occupied(entry) => {
                let labeled_entry = &mut self.labeled_assets[*entry.get()];
                if labeled.handle != labeled_entry.handle {
                    self.asset_id_to_asset_index
                        .remove(&labeled_entry.handle.id());
                    self.asset_id_to_asset_index
                        .insert(labeled.handle.id(), *entry.get());
                }
                *labeled_entry = labeled;
            }
            Entry::Vacant(entry) => {
                entry.insert(self.labeled_assets.len());
                self.asset_id_to_asset_index
                    .insert(labeled.handle.id(), self.labeled_assets.len());
                self.labeled_assets.push(labeled);
            }
        }
    }

    /// Creates the final saved asset from this builder.
    pub fn build<'b, A: Asset>(self, asset: &'b A) -> SavedAsset<'b, 'b, A>
    where
        'a: 'b,
    {
        SavedAsset {
            value: asset,
            labeled_assets: Moo::Owned(self.labeled_assets),
            label_to_asset_index: Moo::Owned(self.label_to_asset_index),
            asset_id_to_asset_index: Moo::Owned(self.asset_id_to_asset_index),
        }
    }
}

/// An alternative to [`Cow`] but simplified to just a `T` or `&T`.
///
/// Associated types are **always** considered "invariant" (see
/// <https://doc.rust-lang.org/nomicon/subtyping.html>). Since [`Cow`] uses the [`ToOwned`] trait
/// and its associated type of [`ToOwned::Owned`], this means [`Cow`] types are invariant (which
/// TL;DR means that in some cases Rust is not allowed to shorten lifetimes, causing lifetime
/// errors).
///
/// This type also allows working with any type, not just those that implement [`ToOwned`] - at the
/// cost of losing the ability to mutate the value.
///
/// `Moo` stands for maybe-owned-object.
///
/// [`Cow`]: alloc::borrow::Cow
/// [`ToOwned`]: alloc::borrow::ToOwned
/// [`ToOwned::Owned`]: alloc::borrow::ToOwned::Owned
#[derive(Clone)]
enum Moo<'a, T> {
    Owned(T),
    Borrowed(&'a T),
}

impl<T> Deref for Moo<'_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Owned(t) => t,
            Self::Borrowed(t) => t,
        }
    }
}

/// Saves `asset` to `path` using the provided `saver` and `settings`.
pub async fn save_using_saver<S: AssetSaver>(
    asset_server: AssetServer,
    saver: &S,
    path: &AssetPath<'_>,
    asset: SavedAsset<'_, '_, S::Asset>,
    settings: &S::Settings,
) -> Result<(), SaveAssetError> {
    let source = asset_server.get_source(path.source())?;
    let writer = source.writer()?;

    let mut file_writer = writer.write(path.path()).await?;

    let loader_settings = saver
        .save(&mut file_writer, asset, settings, path.clone())
        .await
        .map_err(|err| SaveAssetError::SaverError(Arc::new(err.into())))?;

    file_writer.flush().await.map_err(AssetWriterError::Io)?;

    let meta = AssetMeta::<S::OutputLoader, ()>::new(AssetAction::Load {
        loader: S::OutputLoader::type_path().into(),
        settings: loader_settings,
    });

    let meta = AssetMetaDyn::serialize(&meta);
    writer.write_meta_bytes(path.path(), &meta).await?;

    Ok(())
}

/// An error occurring when saving an asset.
#[derive(Error, Debug)]
pub enum SaveAssetError {
    #[error(transparent)]
    MissingSource(#[from] MissingAssetSourceError),
    #[error(transparent)]
    MissingWriter(#[from] MissingAssetWriterError),
    #[error(transparent)]
    WriterError(#[from] AssetWriterError),
    #[error("Failed to save asset due to error from saver: {0}")]
    SaverError(Arc<BevyError>),
}

#[cfg(test)]
pub(crate) mod tests {
    use alloc::{string::ToString, vec, vec::Vec};
    use bevy_reflect::TypePath;
    use bevy_tasks::block_on;
    use futures_lite::AsyncWriteExt;
    use ron::ser::PrettyConfig;

    use crate::{
        saver::{save_using_saver, AssetSaver, SavedAsset, SavedAssetBuilder},
        tests::{create_app, run_app_until, CoolText, CoolTextLoader, CoolTextRon, SubText},
        AssetApp, AssetPath, AssetServer, Assets,
    };

    fn new_subtext(text: &str) -> SubText {
        SubText {
            text: text.to_string(),
        }
    }

    #[derive(TypePath)]
    pub struct CoolTextSaver;

    impl AssetSaver for CoolTextSaver {
        type Asset = CoolText;
        type Settings = ();
        type OutputLoader = CoolTextLoader;
        type Error = std::io::Error;

        async fn save(
            &self,
            writer: &mut crate::io::Writer,
            asset: SavedAsset<'_, '_, Self::Asset>,
            _: &Self::Settings,
            _: AssetPath<'_>,
        ) -> Result<(), Self::Error> {
            // NOTE: We can't handle embedded dependencies in any way, since we need to write to
            // another file to do so.
            assert!(asset.embedded.is_empty());
            let ron = CoolTextRon {
                text: asset.text.clone(),
                sub_texts: asset
                    .iter_labels()
                    .map(|label| asset.get_labeled::<SubText>(label).unwrap().text.clone())
                    .collect(),
                dependencies: asset
                    .dependencies
                    .iter()
                    .map(|handle| handle.path().unwrap().path())
                    .map(|path| path.to_str().unwrap().to_string())
                    .collect(),
                embedded_dependencies: vec![],
            };
            let ron = ron::ser::to_string_pretty(&ron, PrettyConfig::new().new_line("\n")).unwrap();
            writer.write_all(ron.as_bytes()).await?;
            Ok(())
        }
    }

    #[test]
    fn builds_saved_asset_for_new_asset() {
        let mut app = create_app().0;

        app.init_asset::<CoolText>()
            .init_asset::<SubText>()
            .register_asset_loader(CoolTextLoader);

        // Update a few times before saving to show that assets can be entirely created from
        // scratch.
        app.update();
        app.update();
        app.update();

        let hiya_subasset = new_subtext("hiya");
        let goodbye_subasset = new_subtext("goodbye");
        let idk_subasset = new_subtext("idk");

        let asset_server = app.world().resource::<AssetServer>().clone();
        let mut saved_asset_builder =
            SavedAssetBuilder::new(asset_server.clone(), "some/target/path.cool.ron".into());
        let hiya_handle = saved_asset_builder
            .add_labeled_asset_with_new_handle("hiya", SavedAsset::from_asset(&hiya_subasset));
        let goodbye_handle = saved_asset_builder.add_labeled_asset_with_new_handle(
            "goodbye",
            SavedAsset::from_asset(&goodbye_subasset),
        );
        let idk_handle = saved_asset_builder
            .add_labeled_asset_with_new_handle("idk", SavedAsset::from_asset(&idk_subasset));

        let main_asset = CoolText {
            text: "wassup".into(),
            sub_texts: vec![hiya_handle, goodbye_handle, idk_handle],
            ..Default::default()
        };

        let saved_asset = saved_asset_builder.build(&main_asset);
        let mut asset_labels = saved_asset
            .label_to_asset_index
            .keys()
            .map(|label| label.as_ref().to_string())
            .collect::<Vec<_>>();
        asset_labels.sort();
        assert_eq!(asset_labels, &["goodbye", "hiya", "idk"]);

        {
            let asset_server = asset_server.clone();
            block_on(async move {
                save_using_saver(
                    asset_server,
                    &CoolTextSaver,
                    &"some/target/path.cool.ron".into(),
                    saved_asset,
                    &(),
                )
                .await
            })
            .unwrap();
        }

        let readback = asset_server.load("some/target/path.cool.ron");
        run_app_until(&mut app, |_| {
            asset_server.is_loaded(&readback).then_some(())
        });

        let cool_text = app
            .world()
            .resource::<Assets<CoolText>>()
            .get(&readback)
            .unwrap();

        let subtexts = app.world().resource::<Assets<SubText>>();
        let mut asset_labels = cool_text
            .sub_texts
            .iter()
            .map(|handle| subtexts.get(handle).unwrap().text.clone())
            .collect::<Vec<_>>();
        asset_labels.sort();
        assert_eq!(asset_labels, &["goodbye", "hiya", "idk"]);
    }

    #[test]
    fn builds_saved_asset_for_existing_asset() {
        let (mut app, _) = create_app();

        app.init_asset::<CoolText>()
            .init_asset::<SubText>()
            .register_asset_loader(CoolTextLoader);

        let mut subtexts = app.world_mut().resource_mut::<Assets<SubText>>();
        let hiya_handle = subtexts.add(new_subtext("hiya"));
        let goodbye_handle = subtexts.add(new_subtext("goodbye"));
        let idk_handle = subtexts.add(new_subtext("idk"));

        let mut cool_texts = app.world_mut().resource_mut::<Assets<CoolText>>();
        let cool_text_handle = cool_texts.add(CoolText {
            text: "wassup".into(),
            sub_texts: vec![
                hiya_handle.clone(),
                goodbye_handle.clone(),
                idk_handle.clone(),
            ],
            ..Default::default()
        });

        let subtexts = app.world().resource::<Assets<SubText>>();
        let cool_texts = app.world().resource::<Assets<CoolText>>();
        let asset_server = app.world().resource::<AssetServer>().clone();
        let mut saved_asset_builder =
            SavedAssetBuilder::new(asset_server.clone(), "some/target/path.cool.ron".into());
        saved_asset_builder.add_labeled_asset_with_existing_handle(
            "hiya",
            SavedAsset::from_asset(subtexts.get(&hiya_handle).unwrap()),
            hiya_handle,
        );
        saved_asset_builder.add_labeled_asset_with_existing_handle(
            "goodbye",
            SavedAsset::from_asset(subtexts.get(&goodbye_handle).unwrap()),
            goodbye_handle,
        );
        saved_asset_builder.add_labeled_asset_with_existing_handle(
            "idk",
            SavedAsset::from_asset(subtexts.get(&idk_handle).unwrap()),
            idk_handle,
        );

        let saved_asset = saved_asset_builder.build(cool_texts.get(&cool_text_handle).unwrap());
        let mut asset_labels = saved_asset
            .label_to_asset_index
            .keys()
            .map(|label| label.as_ref().to_string())
            .collect::<Vec<_>>();
        asset_labels.sort();
        assert_eq!(asset_labels, &["goodbye", "hiya", "idk"]);

        // While this example is supported, it is **not** recommended. This currently blocks the
        // entire world from updating. A slow write could cause visible stutters. However we do this
        // here to show it's possible to use assets directly out of the Assets resources.
        {
            let asset_server = asset_server.clone();
            block_on(async move {
                save_using_saver(
                    asset_server,
                    &CoolTextSaver,
                    &"some/target/path.cool.ron".into(),
                    saved_asset,
                    &(),
                )
                .await
            })
            .unwrap();
        }

        let readback = asset_server.load("some/target/path.cool.ron");
        run_app_until(&mut app, |_| {
            asset_server.is_loaded(&readback).then_some(())
        });

        let cool_text = app
            .world()
            .resource::<Assets<CoolText>>()
            .get(&readback)
            .unwrap();

        let subtexts = app.world().resource::<Assets<SubText>>();
        let mut asset_labels = cool_text
            .sub_texts
            .iter()
            .map(|handle| subtexts.get(handle).unwrap().text.clone())
            .collect::<Vec<_>>();
        asset_labels.sort();
        assert_eq!(asset_labels, &["goodbye", "hiya", "idk"]);
    }
}