icu_provider/
varule_traits.rs1use zerovec::ule::VarULE;
6
7#[cfg(feature = "alloc")]
8use zerovec::{maps::ZeroMapKV, ZeroMap2d};
9
10pub trait MaybeAsVarULE {
22    type EncodedStruct: ?Sized + VarULE;
25}
26
27#[cfg(feature = "export")]
32pub trait MaybeEncodeAsVarULE: MaybeAsVarULE {
33    fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct>;
36}
37
38#[macro_export] macro_rules! data_struct {
41    (<$generic:ident: $bound:tt> $ty:path $(, $(#[$attr:meta])*)?) => {
42        impl<$generic: $bound> $crate::ule::MaybeAsVarULE for $ty {
43            type EncodedStruct = [()];
44        }
45        $($(#[$attr])*)?
46        impl<$generic: $bound> $crate::ule::MaybeEncodeAsVarULE for $ty {
47            fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
48                None
49            }
50        }
51    };
52    ($ty:path $(, $(#[$attr:meta])*)?) => {
53        impl $crate::ule::MaybeAsVarULE for $ty {
54            type EncodedStruct = [()];
55        }
56        $($(#[$attr])*)?
57        impl $crate::ule::MaybeEncodeAsVarULE for $ty {
58            fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
59                None
60            }
61        }
62    };
63    (
64        $ty:ty,
65        varule: $varule:ty,
66        $(#[$attr:meta])*
67        encode_as_varule: $encode_as_varule:expr
68    ) => {
69        impl<'data> $crate::ule::MaybeAsVarULE for $ty {
70            type EncodedStruct = $varule;
71        }
72        $(#[$attr])*
73        impl<'data> $crate::ule::MaybeEncodeAsVarULE for $ty {
74            fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
75                fn bind_lifetimes<F>(f: F) -> F where F: for<'data> Fn(&'data $ty) -> &'data $varule { f }
77                Some(bind_lifetimes($encode_as_varule)(self))
78            }
79        }
80    };
81}
82
83#[cfg(feature = "alloc")]
86impl<'a, K0, K1, V> MaybeAsVarULE for ZeroMap2d<'a, K0, K1, V>
87where
88    K0: ZeroMapKV<'a>,
89    K1: ZeroMapKV<'a>,
90    V: ZeroMapKV<'a>,
91    K0: ?Sized,
92    K1: ?Sized,
93    V: ?Sized,
94{
95    type EncodedStruct = [()];
96}
97
98#[cfg(feature = "alloc")]
99#[cfg(feature = "export")]
100impl<'a, K0, K1, V> MaybeEncodeAsVarULE for ZeroMap2d<'a, K0, K1, V>
101where
102    K0: ZeroMapKV<'a>,
103    K1: ZeroMapKV<'a>,
104    V: ZeroMapKV<'a>,
105    K0: ?Sized,
106    K1: ?Sized,
107    V: ?Sized,
108{
109    fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
110        None
111    }
112}
113
114impl<T, const N: usize> MaybeAsVarULE for [T; N] {
115    type EncodedStruct = [()];
116}
117
118#[cfg(feature = "export")]
119impl<T, const N: usize> MaybeEncodeAsVarULE for [T; N] {
120    fn maybe_encode_as_varule(&self) -> Option<&Self::EncodedStruct> {
121        None
122    }
123}