1use std::marker::PhantomData;
2
3use anyhow::Result;
4
5use crate::{ReadRef, TraitRef, VcValueTrait, VcValueType, backend::TypedCellContent};
6
7pub trait VcCast: private::Sealed {
13 type Output;
14
15 fn cast(content: TypedCellContent) -> Result<Self::Output>;
16}
17
18pub struct VcValueTypeCast<T> {
20 _phantom: PhantomData<T>,
21}
22
23impl<T> VcCast for VcValueTypeCast<T>
24where
25 T: VcValueType,
26{
27 type Output = ReadRef<T>;
28
29 fn cast(content: TypedCellContent) -> Result<Self::Output> {
30 content.cast()
31 }
32}
33
34pub struct VcValueTraitCast<T>
36where
37 T: ?Sized,
38{
39 _phantom: PhantomData<T>,
40}
41
42impl<T> VcCast for VcValueTraitCast<T>
43where
44 T: VcValueTrait + ?Sized,
45{
46 type Output = TraitRef<T>;
47
48 fn cast(content: TypedCellContent) -> Result<Self::Output> {
49 content.cast_trait::<T>()
52 }
53}
54
55mod private {
58 use super::{VcValueTraitCast, VcValueTypeCast};
59 use crate::{VcValueTrait, VcValueType};
60
61 pub trait Sealed {}
62 impl<T> Sealed for VcValueTypeCast<T> where T: VcValueType {}
63 impl<T> Sealed for VcValueTraitCast<T> where T: VcValueTrait + ?Sized {}
64}