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 ).
45use super::vecs::{MutableZeroVecLike, ZeroVecLike};
6use crate::ule::*;
7use crate::vecs::{FlexZeroSlice, FlexZeroVec};
8use crate::vecs::{VarZeroSlice, VarZeroVec};
9use crate::zerovec::{ZeroSlice, ZeroVec};
10use alloc::boxed::Box;
1112/// 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
18#[allow(clippy::upper_case_acronyms)] // KV is not an acronym
19pub trait ZeroMapKV<'a> {
20/// The container that can be used with this type: [`ZeroVec`] or [`VarZeroVec`].
21type Container: MutableZeroVecLike<
22'a,
23Self,
24 SliceVariant = Self::Slice,
25 GetType = Self::GetType,
26 OwnedType = Self::OwnedType,
27 > + Sized;
28type Slice: ZeroVecLike<Self, GetType = Self::GetType> + ?Sized;
29/// The type produced by `Container::get()`
30 ///
31 /// This type will be predetermined by the choice of `Self::Container`:
32 /// For sized types this must be `T::ULE`, and for unsized types this must be `T`
33type GetType: ?Sized + 'static;
34/// The type produced by `Container::replace()` and `Container::remove()`,
35 /// also used during deserialization. If `Self` is human readable serialized,
36 /// deserializing to `Self::OwnedType` should produce the same value once
37 /// passed through `Self::owned_as_self()`
38 ///
39 /// This type will be predetermined by the choice of `Self::Container`:
40 /// For sized types this must be `T` and for unsized types this must be `Box<T>`
41type OwnedType: 'static;
42}
4344macro_rules! impl_sized_kv {
45 ($ty:ident) => {
46impl<'a> ZeroMapKV<'a> for $ty {
47type Container = ZeroVec<'a, $ty>;
48type Slice = ZeroSlice<$ty>;
49type GetType = <$ty as AsULE>::ULE;
50type OwnedType = $ty;
51 }
52 };
53}
5455impl_sized_kv!(u8);
56impl_sized_kv!(u16);
57impl_sized_kv!(u32);
58impl_sized_kv!(u64);
59impl_sized_kv!(u128);
60impl_sized_kv!(i8);
61impl_sized_kv!(i16);
62impl_sized_kv!(i32);
63impl_sized_kv!(i64);
64impl_sized_kv!(i128);
65impl_sized_kv!(char);
66impl_sized_kv!(f32);
67impl_sized_kv!(f64);
6869impl<'a> ZeroMapKV<'a> for usize {
70type Container = FlexZeroVec<'a>;
71type Slice = FlexZeroSlice;
72type GetType = [u8];
73type OwnedType = usize;
74}
7576impl<'a, T> ZeroMapKV<'a> for Option<T>
77where
78Option<T>: AsULE + 'static,
79{
80type Container = ZeroVec<'a, Option<T>>;
81type Slice = ZeroSlice<Option<T>>;
82type GetType = <Option<T> as AsULE>::ULE;
83type OwnedType = Option<T>;
84}
8586impl<'a, T> ZeroMapKV<'a> for OptionVarULE<T>
87where
88T: VarULE + ?Sized,
89{
90type Container = VarZeroVec<'a, OptionVarULE<T>>;
91type Slice = VarZeroSlice<OptionVarULE<T>>;
92type GetType = OptionVarULE<T>;
93type OwnedType = Box<OptionVarULE<T>>;
94}
9596impl<'a> ZeroMapKV<'a> for str {
97type Container = VarZeroVec<'a, str>;
98type Slice = VarZeroSlice<str>;
99type GetType = str;
100type OwnedType = Box<str>;
101}
102103impl<'a, T> ZeroMapKV<'a> for [T]
104where
105T: ULE + AsULE<ULE = T>,
106{
107type Container = VarZeroVec<'a, [T]>;
108type Slice = VarZeroSlice<[T]>;
109type GetType = [T];
110type OwnedType = Box<[T]>;
111}
112113impl<'a, T, const N: usize> ZeroMapKV<'a> for [T; N]
114where
115T: AsULE + 'static,
116{
117type Container = ZeroVec<'a, [T; N]>;
118type Slice = ZeroSlice<[T; N]>;
119type GetType = [T::ULE; N];
120type OwnedType = [T; N];
121}
122123impl<'a, T> ZeroMapKV<'a> for ZeroSlice<T>
124where
125T: AsULE + 'static,
126{
127type Container = VarZeroVec<'a, ZeroSlice<T>>;
128type Slice = VarZeroSlice<ZeroSlice<T>>;
129type GetType = ZeroSlice<T>;
130type OwnedType = Box<ZeroSlice<T>>;
131}