zerovec/map/
kv.rs

1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5use super::vecs::{MutableZeroVecLike, ZeroVecLike};
6use crate::ule::vartuple::VarTupleULE;
7use crate::ule::*;
8use crate::vecs::{VarZeroSlice, VarZeroVec};
9use crate::zerovec::{ZeroSlice, ZeroVec};
10use alloc::boxed::Box;
11
12/// Trait marking types which are allowed to be keys or values in [`ZeroMap`](super::ZeroMap).
13///
14/// Users should not be calling methods of this trait directly, however if you are
15/// implementing your own [`AsULE`] or [`VarULE`] type you may wish to implement
16/// this trait.
17// this lifetime should be a GAT on Container once that is possible
18pub trait ZeroMapKV<'a> {
19    /// The container that can be used with this type: [`ZeroVec`] or [`VarZeroVec`].
20    type Container: MutableZeroVecLike<
21            'a,
22            Self,
23            SliceVariant = Self::Slice,
24            GetType = Self::GetType,
25            OwnedType = Self::OwnedType,
26        > + Sized;
27    type Slice: ZeroVecLike<Self, GetType = Self::GetType> + ?Sized;
28    /// The type produced by `Container::get()`
29    ///
30    /// This type will be predetermined by the choice of `Self::Container`:
31    /// For sized types this must be `T::ULE`, and for unsized types this must be `T`
32    type GetType: ?Sized + 'static;
33    /// The type produced by `Container::replace()` and `Container::remove()`,
34    /// also used during deserialization. If `Self` is human readable serialized,
35    /// deserializing to `Self::OwnedType` should produce the same value once
36    /// passed through `Self::owned_as_self()`
37    ///
38    /// This type will be predetermined by the choice of `Self::Container`:
39    /// For sized types this must be `T` and for unsized types this must be `Box<T>`
40    type OwnedType: 'static;
41}
42
43macro_rules! impl_sized_kv {
44    ($ty:path) => {
45        impl<'a> ZeroMapKV<'a> for $ty {
46            type Container = ZeroVec<'a, $ty>;
47            type Slice = ZeroSlice<$ty>;
48            type GetType = <$ty as AsULE>::ULE;
49            type OwnedType = $ty;
50        }
51    };
52}
53
54impl_sized_kv!(u8);
55impl_sized_kv!(u16);
56impl_sized_kv!(u32);
57impl_sized_kv!(u64);
58impl_sized_kv!(u128);
59impl_sized_kv!(i8);
60impl_sized_kv!(i16);
61impl_sized_kv!(i32);
62impl_sized_kv!(i64);
63impl_sized_kv!(i128);
64impl_sized_kv!(char);
65impl_sized_kv!(f32);
66impl_sized_kv!(f64);
67
68impl_sized_kv!(core::num::NonZeroU8);
69impl_sized_kv!(core::num::NonZeroI8);
70
71impl<'a, T> ZeroMapKV<'a> for Option<T>
72where
73    Option<T>: AsULE + 'static,
74{
75    type Container = ZeroVec<'a, Option<T>>;
76    type Slice = ZeroSlice<Option<T>>;
77    type GetType = <Option<T> as AsULE>::ULE;
78    type OwnedType = Option<T>;
79}
80
81impl<'a, T> ZeroMapKV<'a> for OptionVarULE<T>
82where
83    T: VarULE + ?Sized,
84{
85    type Container = VarZeroVec<'a, OptionVarULE<T>>;
86    type Slice = VarZeroSlice<OptionVarULE<T>>;
87    type GetType = OptionVarULE<T>;
88    type OwnedType = Box<OptionVarULE<T>>;
89}
90
91impl<'a, A, B> ZeroMapKV<'a> for VarTupleULE<A, B>
92where
93    A: AsULE + 'static,
94    B: VarULE + ?Sized,
95{
96    type Container = VarZeroVec<'a, VarTupleULE<A, B>>;
97    type Slice = VarZeroSlice<VarTupleULE<A, B>>;
98    type GetType = VarTupleULE<A, B>;
99    type OwnedType = Box<VarTupleULE<A, B>>;
100}
101
102impl<'a> ZeroMapKV<'a> for str {
103    type Container = VarZeroVec<'a, str>;
104    type Slice = VarZeroSlice<str>;
105    type GetType = str;
106    type OwnedType = Box<str>;
107}
108
109impl<'a, T> ZeroMapKV<'a> for [T]
110where
111    T: ULE + AsULE<ULE = T>,
112{
113    type Container = VarZeroVec<'a, [T]>;
114    type Slice = VarZeroSlice<[T]>;
115    type GetType = [T];
116    type OwnedType = Box<[T]>;
117}
118
119impl<'a, T, const N: usize> ZeroMapKV<'a> for [T; N]
120where
121    T: AsULE + 'static,
122{
123    type Container = ZeroVec<'a, [T; N]>;
124    type Slice = ZeroSlice<[T; N]>;
125    type GetType = [T::ULE; N];
126    type OwnedType = [T; N];
127}
128
129impl<'a, T> ZeroMapKV<'a> for ZeroSlice<T>
130where
131    T: AsULE + 'static,
132{
133    type Container = VarZeroVec<'a, ZeroSlice<T>>;
134    type Slice = VarZeroSlice<ZeroSlice<T>>;
135    type GetType = ZeroSlice<T>;
136    type OwnedType = Box<ZeroSlice<T>>;
137}