Skip to main content

encase/types/
wrapper.rs

1use alloc::borrow::ToOwned;
2
3/// Used to implement `ShaderType` for the given wrapper type
4///
5/// # Args
6///
7/// - `$type` the type (representing a wrapper) for which `ShaderType` will be implemented for
8///
9/// - `$generics` \[optional\] generics that will be passed into the `impl< >`
10///
11/// - `$using` \[optional\] can be any combination of `Ref{ X } Mut{ X } From{ X }`
12///   (where `X` denotes a possible function call)
13#[macro_export]
14macro_rules! impl_wrapper {
15    ($type:ty; using $($using:tt)*) => {
16        $crate::impl_wrapper_inner!(__inner, ($type, T: ?Sized); $($using)*);
17    };
18    ($type:ty; ($($generics:tt)*); using $($using:tt)*) => {
19        $crate::impl_wrapper_inner!(__inner, ($type, $($generics)*); $($using)*);
20    };
21}
22
23#[doc(hidden)]
24#[macro_export]
25macro_rules! impl_wrapper_inner {
26    (__inner, ($($other:tt)*); Ref{ $($get_ref:tt)* } $($using:tt)*) => {
27        $crate::impl_wrapper_inner!(__ref, ($($other)*); { $($get_ref)* });
28        $crate::impl_wrapper_inner!(__inner, ($($other)*); $($using)*);
29    };
30    (__inner, ($($other:tt)*); Mut{ $($get_mut:tt)* } $($using:tt)*) => {
31        $crate::impl_wrapper_inner!(__mut, ($($other)*); { $($get_mut)* });
32        $crate::impl_wrapper_inner!(__inner, ($($other)*); $($using)*);
33    };
34    (__inner, ($($other:tt)*); From{ $($from:tt)* } $($using:tt)*) => {
35        $crate::impl_wrapper_inner!(__from, ($($other)*); { $($from)* });
36        $crate::impl_wrapper_inner!(__inner, ($($other)*); $($using)*);
37    };
38    (__inner, ($type:ty, $($generics:tt)*); ) => {};
39
40    (__ref, ($type:ty, $($generics:tt)*); { $($get_ref:tt)* }) => {
41        impl<$($generics)*> $crate::private::ShaderType for $type
42        where
43            T: $crate::private::ShaderType
44        {
45            type ExtraMetadata = T::ExtraMetadata;
46            const METADATA: $crate::private::Metadata<Self::ExtraMetadata> = T::METADATA.no_pod();
47
48            const UNIFORM_COMPAT_ASSERT: fn() = T::UNIFORM_COMPAT_ASSERT;
49
50            #[inline]
51            fn size(&self) -> ::core::num::NonZeroU64 {
52                <T as $crate::private::ShaderType>::size(&self$($get_ref)*)
53            }
54        }
55        impl<$($generics)*> $crate::private::ShaderSize for $type
56        where
57            T: $crate::private::ShaderSize
58        {
59            const SHADER_SIZE: ::core::num::NonZeroU64 = T::SHADER_SIZE;
60        }
61
62        impl<$($generics)*> $crate::private::RuntimeSizedArray for $type
63        where
64            T: $crate::private::RuntimeSizedArray
65        {
66            #[inline]
67            fn len(&self) -> usize {
68                <T as $crate::private::RuntimeSizedArray>::len(&self$($get_ref)*)
69            }
70        }
71
72        impl<$($generics)*> $crate::private::CalculateSizeFor for $type
73        where
74            T: $crate::private::CalculateSizeFor
75        {
76            #[inline]
77            fn calculate_size_for(nr_of_el: u64) -> ::core::num::NonZeroU64 {
78                <T as $crate::private::CalculateSizeFor>::calculate_size_for(nr_of_el)
79            }
80        }
81
82        impl<$($generics)*> $crate::private::WriteInto for $type
83        where
84            T: $crate::private::WriteInto
85        {
86            #[inline]
87            fn write_into<B: $crate::private::BufferMut>(&self, writer: &mut $crate::private::Writer<B>) {
88                <T as $crate::private::WriteInto>::write_into(&self$($get_ref)*, writer)
89            }
90        }
91    };
92    (__mut, ($type:ty, $($generics:tt)*); { $($get_mut:tt)* }) => {
93        impl<$($generics)*> $crate::private::ReadFrom for $type
94        where
95            T: $crate::private::ReadFrom
96        {
97            #[inline]
98            fn read_from<B: $crate::private::BufferRef>(&mut self, reader: &mut $crate::private::Reader<B>) {
99                <T as $crate::private::ReadFrom>::read_from(self$($get_mut)*, reader)
100            }
101        }
102    };
103    (__from, ($type:ty, $($generics:tt)*); { $($from:tt)* }) => {
104        impl<$($generics)*> $crate::private::CreateFrom for $type
105        where
106            T: $crate::private::CreateFrom
107        {
108            #[inline]
109            fn create_from<B: $crate::private::BufferRef>(reader: &mut $crate::private::Reader<B>) -> Self {
110                <$type>::$($from)*(<T as $crate::private::CreateFrom>::create_from(reader))
111            }
112        }
113    };
114}
115
116impl_wrapper!(&T; using Ref{});
117impl_wrapper!(&mut T; using Ref{} Mut{});
118impl_wrapper!(alloc::boxed::Box<T>; using Ref{} Mut{} From{ new });
119impl_wrapper!(alloc::borrow::Cow<'_, T>; (T: ?Sized + ToOwned<Owned = T>); using Ref{} From{ Owned });
120impl_wrapper!(alloc::rc::Rc<T>; using Ref{} From{ new });
121impl_wrapper!(alloc::sync::Arc<T>; using Ref{} From{ new });
122impl_wrapper!(core::cell::Cell<T>; (T: Copy); using Ref{ .get() } Mut{ .get_mut() } From{ new });