zerovec/map/
kv.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use super::vecs::{MutableZeroVecLike, ZeroVecLike};
use crate::ule::*;
use crate::vecs::{FlexZeroSlice, FlexZeroVec};
use crate::vecs::{VarZeroSlice, VarZeroVec};
use crate::zerovec::{ZeroSlice, ZeroVec};
use alloc::boxed::Box;

/// Trait marking types which are allowed to be keys or values in [`ZeroMap`](super::ZeroMap).
///
/// Users should not be calling methods of this trait directly, however if you are
/// implementing your own [`AsULE`] or [`VarULE`] type you may wish to implement
/// this trait.
// this lifetime should be a GAT on Container once that is possible
#[allow(clippy::upper_case_acronyms)] // KV is not an acronym
pub trait ZeroMapKV<'a> {
    /// The container that can be used with this type: [`ZeroVec`] or [`VarZeroVec`].
    type Container: MutableZeroVecLike<
            'a,
            Self,
            SliceVariant = Self::Slice,
            GetType = Self::GetType,
            OwnedType = Self::OwnedType,
        > + Sized;
    type Slice: ZeroVecLike<Self, GetType = Self::GetType> + ?Sized;
    /// The type produced by `Container::get()`
    ///
    /// This type will be predetermined by the choice of `Self::Container`:
    /// For sized types this must be `T::ULE`, and for unsized types this must be `T`
    type GetType: ?Sized + 'static;
    /// The type produced by `Container::replace()` and `Container::remove()`,
    /// also used during deserialization. If `Self` is human readable serialized,
    /// deserializing to `Self::OwnedType` should produce the same value once
    /// passed through `Self::owned_as_self()`
    ///
    /// This type will be predetermined by the choice of `Self::Container`:
    /// For sized types this must be `T` and for unsized types this must be `Box<T>`
    type OwnedType: 'static;
}

macro_rules! impl_sized_kv {
    ($ty:ident) => {
        impl<'a> ZeroMapKV<'a> for $ty {
            type Container = ZeroVec<'a, $ty>;
            type Slice = ZeroSlice<$ty>;
            type GetType = <$ty as AsULE>::ULE;
            type OwnedType = $ty;
        }
    };
}

impl_sized_kv!(u8);
impl_sized_kv!(u16);
impl_sized_kv!(u32);
impl_sized_kv!(u64);
impl_sized_kv!(u128);
impl_sized_kv!(i8);
impl_sized_kv!(i16);
impl_sized_kv!(i32);
impl_sized_kv!(i64);
impl_sized_kv!(i128);
impl_sized_kv!(char);
impl_sized_kv!(f32);
impl_sized_kv!(f64);

impl<'a> ZeroMapKV<'a> for usize {
    type Container = FlexZeroVec<'a>;
    type Slice = FlexZeroSlice;
    type GetType = [u8];
    type OwnedType = usize;
}

impl<'a, T> ZeroMapKV<'a> for Option<T>
where
    Option<T>: AsULE + 'static,
{
    type Container = ZeroVec<'a, Option<T>>;
    type Slice = ZeroSlice<Option<T>>;
    type GetType = <Option<T> as AsULE>::ULE;
    type OwnedType = Option<T>;
}

impl<'a, T> ZeroMapKV<'a> for OptionVarULE<T>
where
    T: VarULE + ?Sized,
{
    type Container = VarZeroVec<'a, OptionVarULE<T>>;
    type Slice = VarZeroSlice<OptionVarULE<T>>;
    type GetType = OptionVarULE<T>;
    type OwnedType = Box<OptionVarULE<T>>;
}

impl<'a> ZeroMapKV<'a> for str {
    type Container = VarZeroVec<'a, str>;
    type Slice = VarZeroSlice<str>;
    type GetType = str;
    type OwnedType = Box<str>;
}

impl<'a, T> ZeroMapKV<'a> for [T]
where
    T: ULE + AsULE<ULE = T>,
{
    type Container = VarZeroVec<'a, [T]>;
    type Slice = VarZeroSlice<[T]>;
    type GetType = [T];
    type OwnedType = Box<[T]>;
}

impl<'a, T, const N: usize> ZeroMapKV<'a> for [T; N]
where
    T: AsULE + 'static,
{
    type Container = ZeroVec<'a, [T; N]>;
    type Slice = ZeroSlice<[T; N]>;
    type GetType = [T::ULE; N];
    type OwnedType = [T; N];
}

impl<'a, T> ZeroMapKV<'a> for ZeroSlice<T>
where
    T: AsULE + 'static,
{
    type Container = VarZeroVec<'a, ZeroSlice<T>>;
    type Slice = VarZeroSlice<ZeroSlice<T>>;
    type GetType = ZeroSlice<T>;
    type OwnedType = Box<ZeroSlice<T>>;
}