hydrate-base 0.0.2

Game asset pipeline and authoring 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
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
use std::cell::RefCell;
use std::hash::Hasher;
use std::{
    collections::{HashMap, HashSet},
    fmt::Debug,
    hash::Hash,
    marker::PhantomData,
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc, Mutex, RwLock,
    },
};

use crate::ArtifactId;
use crossbeam_channel::Sender;
use serde::{de, ser, Deserialize, Serialize};
use uuid::Uuid;

/// Loading ID allocated by [`Loader`](crate::loader::Loader) to track loading of a particular artifact
/// or an indirect reference to an artifact.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct LoadHandle(pub u64);

impl LoadHandle {
    pub fn new(
        load_handle: u64,
        is_indirect: bool,
    ) -> Self {
        if is_indirect {
            Self(load_handle | (1 << 63))
        } else {
            Self(load_handle)
        }
    }

    /// Returns true if the handle needs to be resolved through the [`IndirectionTable`] before use.
    /// An "indirect" LoadHandle represents a load operation for an identifier that is late-bound,
    /// meaning the identifier may change which [`ArtifactId`] it resolves to.
    /// An example of an indirect LoadHandle would be one that loads by filesystem path.
    /// The specific artifact at a path may change as files change, move or are deleted, while a direct
    /// LoadHandle (one that addresses by ArtifactId) is guaranteed to refer to an ArtifactId for its
    /// whole lifetime.
    pub fn is_indirect(&self) -> bool {
        (self.0 & (1 << 63)) == 1 << 63
    }
}

/// A potentially unresolved reference to an artifact
#[derive(Debug, Hash, PartialEq, Eq, Clone, Ord, PartialOrd, Serialize, Deserialize)]
pub struct ArtifactRef(pub ArtifactId);

/// Provides information about mappings between `ArtifactId` and `LoadHandle`.
/// Intended to be used for `Handle` serde.
pub trait LoaderInfoProvider: Send + Sync {
    /// Returns the load handle for the artifact with the given UUID, if present.
    ///
    /// This will only return `Some(..)` if there has been a previous call to [`crate::loader::Loader::add_ref`].
    ///
    /// # Parameters
    ///
    /// * `id`: UUID of the artifact.
    fn resolved_load_handle(
        &self,
        artifact_ref: &ArtifactRef,
    ) -> Option<Arc<ResolvedLoadHandle>>;

    /// Returns the ArtifactId for the given LoadHandle, if present.
    ///
    /// # Parameters
    ///
    /// * `load_handle`: ID allocated by [`Loader`](crate::loader::Loader) to track loading of the artifact.
    fn artifact_id(
        &self,
        load: LoadHandle,
    ) -> Option<ArtifactId>;
}

#[derive(Debug)]
pub struct ResolvedLoadHandle {
    pub id: LoadHandle,
    pub direct_load_handle: AtomicU64,
}

impl PartialEq for ResolvedLoadHandle {
    fn eq(
        &self,
        other: &Self,
    ) -> bool {
        // Only need to check the indirect ID
        self.id == other.id
    }
}

impl Eq for ResolvedLoadHandle {}

impl Hash for ResolvedLoadHandle {
    fn hash<H: Hasher>(
        &self,
        state: &mut H,
    ) {
        // Only care about hashing the id
        self.id.hash(state)
    }
}

impl ResolvedLoadHandle {
    pub fn new(
        id: LoadHandle,
        resolved_load_handle: LoadHandle,
    ) -> Arc<Self> {
        Arc::new(ResolvedLoadHandle {
            id,
            direct_load_handle: AtomicU64::new(resolved_load_handle.0),
        })
    }

    pub fn new_null_handle() -> Arc<Self> {
        Arc::new(ResolvedLoadHandle {
            id: LoadHandle(0),
            direct_load_handle: AtomicU64::default(),
        })
    }

    pub fn direct_load_handle(&self) -> LoadHandle {
        LoadHandle(self.direct_load_handle.load(Ordering::Relaxed))
    }
}

/// Operations on an artifact reference.
#[derive(Debug)]
pub enum RefOp {
    Decrease(LoadHandle),
    Increase(LoadHandle),
}

/// Keeps track of whether a handle ref is a strong, weak or "internal" ref
#[derive(Debug)]
pub enum HandleRefType {
    /// Strong references decrement the count on drop
    Strong(Sender<RefOp>),
    /// Weak references do nothing on drop.
    Weak(Sender<RefOp>),
    /// Internal references do nothing on drop, but turn into Strong references on clone.
    /// Should only be used for references stored in loaded artifacts to avoid self-referencing
    Internal(Sender<RefOp>),
    /// Implementation detail, used when changing state in this enum
    None,
}

struct HandleRef {
    resolved_load_handle: Arc<ResolvedLoadHandle>,
    ref_type: HandleRefType,
}
impl PartialEq for HandleRef {
    fn eq(
        &self,
        other: &Self,
    ) -> bool {
        self.resolved_load_handle
            .id
            .eq(&other.resolved_load_handle.id)
    }
}
impl Hash for HandleRef {
    fn hash<H: std::hash::Hasher>(
        &self,
        state: &mut H,
    ) {
        self.resolved_load_handle.id.hash(state)
    }
}
impl Eq for HandleRef {}
impl Debug for HandleRef {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        self.resolved_load_handle.id.fmt(f)
    }
}

impl Drop for HandleRef {
    fn drop(&mut self) {
        use HandleRefType::*;
        self.ref_type = match std::mem::replace(&mut self.ref_type, None) {
            Strong(sender) => {
                let _ = sender.send(RefOp::Decrease(self.resolved_load_handle.id));
                Weak(sender)
            }
            r => r,
        };
    }
}

impl Clone for HandleRef {
    fn clone(&self) -> Self {
        use HandleRefType::*;
        Self {
            resolved_load_handle: self.resolved_load_handle.clone(),
            ref_type: match &self.ref_type {
                Internal(sender) | Strong(sender) => {
                    let _ = sender.send(RefOp::Increase(self.resolved_load_handle.id));
                    Strong(sender.clone())
                }
                Weak(sender) => Weak(sender.clone()),
                None => panic!("unexpected ref type in clone()"),
            },
        }
    }
}

impl ArtifactHandle for HandleRef {
    fn resolved_load_handle(&self) -> &Arc<ResolvedLoadHandle> {
        &self.resolved_load_handle
    }
}

/// Handle to an artifact.
#[derive(Eq)]
pub struct Handle<T: ?Sized> {
    handle_ref: HandleRef,
    marker: PhantomData<T>,
}

impl<T: ?Sized> PartialEq for Handle<T> {
    fn eq(
        &self,
        other: &Self,
    ) -> bool {
        self.handle_ref == other.handle_ref
    }
}

impl<T: ?Sized> Clone for Handle<T> {
    fn clone(&self) -> Self {
        Self {
            handle_ref: self.handle_ref.clone(),
            marker: PhantomData,
        }
    }
}

impl<T: ?Sized> Hash for Handle<T> {
    fn hash<H: std::hash::Hasher>(
        &self,
        state: &mut H,
    ) {
        self.handle_ref.hash(state);
    }
}

impl<T: ?Sized> Debug for Handle<T> {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        f.debug_struct("Handle")
            .field("handle_ref", &self.handle_ref)
            .finish()
    }
}

impl<T: ?Sized> From<GenericHandle> for Handle<T> {
    fn from(handle: GenericHandle) -> Self {
        Self {
            handle_ref: handle.handle_ref,
            marker: PhantomData,
        }
    }
}

impl<T> Handle<T> {
    /// Creates a new handle with `HandleRefType::Strong`
    pub fn new(
        chan: Sender<RefOp>,
        resolved_load_handle: Arc<ResolvedLoadHandle>,
    ) -> Self {
        Self {
            handle_ref: HandleRef {
                resolved_load_handle,
                ref_type: HandleRefType::Strong(chan),
            },
            marker: PhantomData,
        }
    }

    /// Creates a new handle with `HandleRefType::Internal`
    pub(crate) fn new_internal(
        chan: Sender<RefOp>,
        resolved_load_handle: Arc<ResolvedLoadHandle>,
    ) -> Self {
        Self {
            handle_ref: HandleRef {
                resolved_load_handle,
                ref_type: HandleRefType::Internal(chan),
            },
            marker: PhantomData,
        }
    }

    pub fn artifact<'a>(
        &self,
        storage: &'a impl TypedArtifactStorage<T>,
    ) -> Option<&'a T> {
        ArtifactHandle::artifact(self, storage)
    }
}

impl<T> ArtifactHandle for Handle<T> {
    fn resolved_load_handle(&self) -> &Arc<ResolvedLoadHandle> {
        &self.handle_ref.resolved_load_handle
    }
}

/// Handle to an artifact whose type is unknown during loading.
///
/// This is returned by `Loader::load_artifact_generic` for artifacts loaded by UUID.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GenericHandle {
    handle_ref: HandleRef,
}

impl GenericHandle {
    /// Creates a new handle with `HandleRefType::Strong`
    pub fn new(
        chan: Sender<RefOp>,
        resolved_load_handle: Arc<ResolvedLoadHandle>,
    ) -> Self {
        Self {
            handle_ref: HandleRef {
                resolved_load_handle,
                ref_type: HandleRefType::Strong(chan),
            },
        }
    }

    /// Creates a new handle with `HandleRefType::Internal`
    pub(crate) fn new_internal(
        chan: Sender<RefOp>,
        resolved_load_handle: Arc<ResolvedLoadHandle>,
    ) -> Self {
        Self {
            handle_ref: HandleRef {
                resolved_load_handle,
                ref_type: HandleRefType::Internal(chan),
            },
        }
    }
}

impl ArtifactHandle for GenericHandle {
    fn resolved_load_handle(&self) -> &Arc<ResolvedLoadHandle> {
        &self.handle_ref.resolved_load_handle
    }
}

impl<T: ?Sized> From<Handle<T>> for GenericHandle {
    fn from(handle: Handle<T>) -> Self {
        Self {
            handle_ref: handle.handle_ref,
        }
    }
}

/// Handle to an artifact that does not prevent the artifact from being unloaded.
///
/// Weak handles are primarily used when you want to use something that is already loaded.
///
/// For example, a strong handle to an artifact may be guaranteed to exist elsewhere in the program,
/// and so you can simply get and use a weak handle to that artifact in other parts of your code. This
/// removes reference counting overhead, but also ensures that the system which uses the weak handle
/// is not in control of when to unload the artifact.
#[derive(Clone, Eq, Hash, PartialEq, Debug)]
pub struct WeakHandle {
    resolved_load_handle: Arc<ResolvedLoadHandle>,
}

impl WeakHandle {
    pub fn new(handle: Arc<ResolvedLoadHandle>) -> Self {
        WeakHandle {
            resolved_load_handle: handle,
        }
    }
}

impl ArtifactHandle for WeakHandle {
    fn resolved_load_handle(&self) -> &Arc<ResolvedLoadHandle> {
        &self.resolved_load_handle
    }
}

std::thread_local!(static LOADER: std::cell::RefCell<Option<&'static dyn LoaderInfoProvider>> = RefCell::new(None));
std::thread_local!(static REFOP_SENDER: std::cell::RefCell<Option<Sender<RefOp>>> = RefCell::new(None));

/// Used to make some limited Loader interactions available to `serde` Serialize/Deserialize
/// implementations by using thread-local storage. Required to support Serialize/Deserialize of Handle.
pub struct SerdeContext;
impl SerdeContext {
    pub fn with_active<R>(f: impl FnOnce(&dyn LoaderInfoProvider, &Sender<RefOp>) -> R) -> R {
        //LOADER.with(|l| REFOP_SENDER.with(|r| f(*l, r)))
        LOADER.with(|loader| {
            //*loader.borrow_mut() = Some(loader_info_provider);
            REFOP_SENDER.with(|refop_sender| {
                (f)(
                    *loader.borrow().as_ref().unwrap(),
                    refop_sender.borrow().as_ref().unwrap(),
                )
                //*refop_sender.borrow_mut() = Some(sender);
                //let output = (f)(l, r);
                //*refop_sender.borrow_mut() = None;
                //output
            })
            //*loader.borrow_mut() = None;
            //output
        })
    }

    pub fn with<T, F>(
        loader: &dyn LoaderInfoProvider,
        sender: Sender<RefOp>,
        f: F,
    ) -> T
    where
        F: FnOnce() -> T,
    {
        // The loader lifetime needs to be transmuted to 'static to be able to be stored in thread_local.
        // This is safe since SerdeContext's lifetime cannot be shorter than the opened scope, and the loader
        // must live at least as long.
        let loader_info_provider = unsafe {
            std::mem::transmute::<&dyn LoaderInfoProvider, &'static dyn LoaderInfoProvider>(loader)
        };

        LOADER.with(|loader| {
            *loader.borrow_mut() = Some(loader_info_provider);
            let output = REFOP_SENDER.with(|refop_sender| {
                *refop_sender.borrow_mut() = Some(sender);
                let output = (f)();
                *refop_sender.borrow_mut() = None;
                output
            });
            *loader.borrow_mut() = None;
            output
        })

        // *LOADER.borrow_mut() = Some(loader);
        // *REFOP_SENDER.borrow_mut() = Some(sender);
        //
        //
        // (*f)();
        //
        // *LOADER.borrow_mut() = None;
        // *REFOP_SENDER.borrow_mut() = None;

        // LOADER.(|x| {
        //
        // })
        //
        // LOADER.scope(loader, REFOP_SENDER.scope(sender, f)).await
    }
}

/// This context can be used to maintain ArtifactId references through a serialize/deserialize cycle
/// even if the LoadHandles produced are invalid. This is useful when a loader is not
/// present, such as when processing in the Distill Daemon.
pub struct DummySerdeContext {
    maps: RwLock<DummySerdeContextMaps>,
    current: Mutex<DummySerdeContextCurrent>,
    ref_sender: Sender<RefOp>,
    handle_gen: AtomicU64,
}

struct DummySerdeContextMaps {
    uuid_to_load: HashMap<ArtifactRef, LoadHandle>,
    load_to_uuid: HashMap<LoadHandle, ArtifactRef>,
}

struct DummySerdeContextCurrent {
    current_serde_dependencies: HashSet<ArtifactRef>,
    current_serde_artifact: Option<ArtifactId>,
}

impl DummySerdeContext {
    pub fn new() -> Self {
        let (tx, _) = crossbeam_channel::unbounded();
        Self {
            maps: RwLock::new(DummySerdeContextMaps {
                uuid_to_load: HashMap::default(),
                load_to_uuid: HashMap::default(),
            }),
            current: Mutex::new(DummySerdeContextCurrent {
                current_serde_dependencies: HashSet::new(),
                current_serde_artifact: None,
            }),
            ref_sender: tx,
            handle_gen: AtomicU64::new(1),
        }
    }
}

impl LoaderInfoProvider for DummySerdeContext {
    fn resolved_load_handle(
        &self,
        artifact_ref: &ArtifactRef,
    ) -> Option<Arc<ResolvedLoadHandle>> {
        let mut maps = self.maps.write().unwrap();
        let maps = &mut *maps;
        let uuid_to_load = &mut maps.uuid_to_load;
        let load_to_uuid = &mut maps.load_to_uuid;

        let entry = uuid_to_load.entry(artifact_ref.clone());
        let handle = entry.or_insert_with(|| {
            let new_id = self.handle_gen.fetch_add(1, Ordering::Relaxed);
            let handle = LoadHandle(new_id);
            load_to_uuid.insert(handle, artifact_ref.clone());
            handle
        });

        Some(Arc::new(ResolvedLoadHandle {
            id: *handle,
            direct_load_handle: AtomicU64::new(handle.0),
        }))
    }

    fn artifact_id(
        &self,
        load: LoadHandle,
    ) -> Option<ArtifactId> {
        let maps = self.maps.read().unwrap();
        let maybe_artifact = maps.load_to_uuid.get(&load).cloned();
        if let Some(artifact_ref) = maybe_artifact.as_ref() {
            let mut current = self.current.lock().unwrap();
            if let Some(ref current_serde_id) = current.current_serde_artifact {
                if ArtifactRef(*current_serde_id) != *artifact_ref
                    && *artifact_ref != ArtifactRef(ArtifactId::null())
                {
                    current
                        .current_serde_dependencies
                        .insert(artifact_ref.clone());
                }
            }
        }
        if let Some(ArtifactRef(uuid)) = maybe_artifact {
            Some(uuid)
        } else {
            None
        }
    }
}
pub struct DummySerdeContextHandle {
    dummy: Arc<DummySerdeContext>,
}

impl Default for DummySerdeContextHandle {
    fn default() -> Self {
        DummySerdeContextHandle {
            dummy: Arc::new(DummySerdeContext::new()),
        }
    }
}

impl DummySerdeContextHandle {
    pub fn scope<'a, T, F: FnOnce() -> T>(
        &self,
        f: F,
    ) -> T {
        let sender = self.dummy.ref_sender.clone();
        let loader = &*self.dummy;
        SerdeContext::with(loader, sender, f)
    }

    pub fn resolve_ref(
        &mut self,
        artifact_ref: &ArtifactRef,
        artifact: ArtifactId,
    ) {
        let new_ref = ArtifactRef(artifact);
        let mut maps = self.dummy.maps.write().unwrap();
        if let Some(handle) = maps.uuid_to_load.get(artifact_ref) {
            let handle = *handle;
            maps.load_to_uuid.insert(handle, new_ref.clone());
            maps.uuid_to_load.insert(new_ref, handle);
        }
    }

    /// Begin gathering dependencies for an artifact
    pub fn begin_serialize_artifact(
        &mut self,
        artifact: ArtifactId,
    ) {
        let mut current = self.dummy.current.lock().unwrap();
        if current.current_serde_artifact.is_some() {
            panic!("begin_serialize_artifact when current_serde_artifact is already set");
        }
        current.current_serde_artifact = Some(artifact);
    }

    /// Finish gathering dependencies for an artifact
    pub fn end_serialize_artifact(
        &mut self,
        _artifact: ArtifactId,
    ) -> HashSet<ArtifactRef> {
        let mut current = self.dummy.current.lock().unwrap();
        if current.current_serde_artifact.is_none() {
            panic!("end_serialize_artifact when current_serde_artifact is not set");
        }
        current.current_serde_artifact = None;
        std::mem::take(&mut current.current_serde_dependencies)
    }
}

/// Register this context with ArtifactDaemon to add serde support for Handle.
// pub struct HandleSerdeContextProvider;
// impl crate::importer_context::ImporterContext for HandleSerdeContextProvider {
//     fn handle(&self) -> Box<dyn crate::importer_context::ImporterContextHandle> {
//         let dummy = Arc::new(DummySerdeContext::new());
//         Box::new(DummySerdeContextHandle { dummy })
//     }
// }

fn serialize_handle<S>(
    load: LoadHandle,
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: ser::Serializer,
{
    SerdeContext::with_active(|loader, _| {
        use ser::SerializeSeq;
        let uuid_bytes: uuid::Bytes = *loader
            .artifact_id(load)
            .unwrap_or_default()
            .as_uuid()
            .as_bytes();
        let mut seq = serializer.serialize_seq(Some(uuid_bytes.len()))?;
        for element in &uuid_bytes {
            seq.serialize_element(element)?;
        }
        seq.end()
    })
}
impl<T> Serialize for Handle<T> {
    fn serialize<S>(
        &self,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: ser::Serializer,
    {
        serialize_handle(self.handle_ref.resolved_load_handle.id, serializer)
    }
}
impl Serialize for GenericHandle {
    fn serialize<S>(
        &self,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: ser::Serializer,
    {
        serialize_handle(self.handle_ref.resolved_load_handle.id, serializer)
    }
}

fn get_handle_ref(artifact_ref: ArtifactRef) -> (Arc<ResolvedLoadHandle>, Sender<RefOp>) {
    SerdeContext::with_active(|loader, sender| {
        let handle = if artifact_ref == ArtifactRef(ArtifactId::default()) {
            ResolvedLoadHandle::new_null_handle()
        } else {
            loader
                .resolved_load_handle(&artifact_ref)
                .unwrap_or_else(|| panic!("Handle for ArtifactId {:?} was not present when deserializing a Handle. This indicates missing dependency metadata, and can be caused by dependency cycles.", artifact_ref))
        };
        (handle, sender.clone())
    })
}

impl<'de, T> Deserialize<'de> for Handle<T> {
    fn deserialize<D>(deserializer: D) -> Result<Handle<T>, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        let artifact_ref = if deserializer.is_human_readable() {
            deserializer.deserialize_any(ArtifactRefVisitor)?
        } else {
            deserializer.deserialize_seq(ArtifactRefVisitor)?
        };
        let (handle, sender) = get_handle_ref(artifact_ref);
        Ok(Handle::new_internal(sender, handle))
    }
}

impl<'de> Deserialize<'de> for GenericHandle {
    fn deserialize<D>(deserializer: D) -> Result<GenericHandle, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        let artifact_ref = if deserializer.is_human_readable() {
            deserializer.deserialize_any(ArtifactRefVisitor)?
        } else {
            deserializer.deserialize_seq(ArtifactRefVisitor)?
        };
        let (handle, sender) = get_handle_ref(artifact_ref);
        Ok(GenericHandle::new_internal(sender, handle))
    }
}

struct ArtifactRefVisitor;

impl<'de> de::Visitor<'de> for ArtifactRefVisitor {
    type Value = ArtifactRef;

    fn expecting(
        &self,
        formatter: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        formatter.write_str("an array of 16 u8")
    }

    fn visit_newtype_struct<D>(
        self,
        deserializer: D,
    ) -> Result<Self::Value, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        deserializer.deserialize_seq(self)
    }

    fn visit_seq<A>(
        self,
        mut seq: A,
    ) -> Result<Self::Value, A::Error>
    where
        A: de::SeqAccess<'de>,
    {
        use de::Error;
        let mut uuid: [u8; 16] = Default::default();
        for (i, uuid_byte) in uuid.iter_mut().enumerate() {
            if let Some(byte) = seq.next_element::<u8>()? {
                *uuid_byte = byte;
            } else {
                return Err(A::Error::custom(format!(
                    "expected byte at element {} when deserializing handle",
                    i
                )));
            }
        }
        if seq.next_element::<u8>()?.is_some() {
            return Err(A::Error::custom(
                "too many elements when deserializing handle",
            ));
        }
        Ok(ArtifactRef(ArtifactId::from_uuid(Uuid::from_bytes(uuid))))
    }

    fn visit_str<E>(
        self,
        v: &str,
    ) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        if let Ok(uuid) = Uuid::parse_str(v) {
            Ok(ArtifactRef(ArtifactId::from_uuid(uuid)))
        } else {
            Err(E::custom(format!("failed to parse Handle string")))
        }
    }

    fn visit_bytes<E>(
        self,
        v: &[u8],
    ) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        if v.len() != 16 {
            Err(E::custom(format!(
                "byte array len == {}, expected {}",
                v.len(),
                16
            )))
        } else {
            let mut a = <[u8; 16]>::default();
            a.copy_from_slice(v);
            Ok(ArtifactRef(ArtifactId::from_uuid(Uuid::from_bytes(a))))
        }
    }
}

/// Implementors of [`crate::storage::ArtifactStorage`] can implement this trait to enable convenience
/// functions on the common [`ArtifactHandle`] trait, which is implemented by all handle types.
pub trait TypedArtifactStorage<A> {
    /// Returns the artifact for the given handle, or `None` if has not completed loading.
    ///
    /// # Parameters
    ///
    /// * `handle`: Handle of the artifact.
    ///
    /// # Type Parameters
    ///
    /// * `T`: Artifact handle type.
    fn get<T: ArtifactHandle>(
        &self,
        handle: &T,
    ) -> Option<&A>;
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LoadState {
    // Not loaded, and we haven't started trying to load it. Ref count > 0 implies we want to start
    // loading.
    Unloaded,
    // Metadata request is in flight
    WaitingForMetadata,
    // We've incremented ref counts for dependencies, but they aren't loaded yet
    WaitingForDependencies,
    // Dependencies are loaded, and we have requested the data required to load this artifact
    WaitingForData,
    // Data has been passed off to end-user's loader
    Loading,
    // The engine finished loading the artifact and it is available to the game.
    Loaded,
}

// This allows a handle in hydrate_base to get information from the loader which may be in hydrate_loader
// or some other loader implementation
pub trait LoadStateProvider {
    fn load_state(
        &self,
        load_handle: &Arc<ResolvedLoadHandle>,
    ) -> LoadState;
    fn artifact_id(
        &self,
        load_handle: &Arc<ResolvedLoadHandle>,
    ) -> ArtifactId;
}

/// The contract of an artifact handle.
///
/// There are two types of artifact handles:
///
/// * **Typed -- `Handle<T>`:** When the artifact's type is known when loading.
/// * **Generic -- `GenericHandle`:** When only the artifact's UUID is known when loading.
pub trait ArtifactHandle {
    /// Returns the load status of the artifact.
    ///
    /// # Parameters
    ///
    /// * `loader`: Loader that is loading the artifact.
    ///
    /// # Type Parameters
    ///
    /// * `L`: Artifact loader type.
    fn load_state<T: LoadStateProvider>(
        &self,
        loader: &T,
    ) -> LoadState {
        loader.load_state(self.resolved_load_handle())
    }

    fn artifact_id<T: LoadStateProvider>(
        &self,
        loader: &T,
    ) -> ArtifactId {
        loader.artifact_id(self.resolved_load_handle())
    }

    /// Returns an immutable reference to the artifact if it is committed.
    ///
    /// # Parameters
    ///
    /// * `storage`: Artifact storage.
    fn artifact<'a, T, S: TypedArtifactStorage<T>>(
        &self,
        storage: &'a S,
    ) -> Option<&'a T>
    where
        Self: Sized,
    {
        storage.get(self)
    }

    /// Downgrades this handle into a `WeakHandle`.
    ///
    /// Be aware that if there are no longer any strong handles to the artifact, then the underlying
    /// artifact may be freed at any time.
    fn downgrade(&self) -> WeakHandle {
        WeakHandle::new(self.resolved_load_handle().clone())
    }

    fn resolved_load_handle(&self) -> &Arc<ResolvedLoadHandle>;

    /// Returns the `LoadHandle` of this artifact handle.
    fn load_handle(&self) -> LoadHandle {
        self.resolved_load_handle().id
    }

    fn direct_load_handle(&self) -> LoadHandle {
        self.resolved_load_handle().direct_load_handle()
    }
}

pub fn make_handle_within_serde_context<T>(uuid: ArtifactId) -> Handle<T> {
    SerdeContext::with_active(|loader_info_provider, ref_op_sender| {
        let load_handle = loader_info_provider
            .resolved_load_handle(&ArtifactRef(uuid))
            .unwrap();
        Handle::<T>::new(ref_op_sender.clone(), load_handle)
    })
}