Coverage Report

Created: 2026-01-09 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.10.4/src/map/vecs.rs
Line
Count
Source
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
5
use crate::ule::*;
6
use crate::varzerovec::owned::VarZeroVecOwned;
7
use crate::vecs::{FlexZeroSlice, FlexZeroVec, FlexZeroVecOwned, VarZeroVecFormat};
8
use crate::{VarZeroSlice, VarZeroVec};
9
use crate::{ZeroSlice, ZeroVec};
10
use alloc::boxed::Box;
11
use alloc::vec::Vec;
12
use core::cmp::Ordering;
13
use core::mem;
14
use core::ops::Range;
15
16
/// Trait abstracting over [`ZeroVec`] and [`VarZeroVec`], for use in [`ZeroMap`](super::ZeroMap). **You
17
/// should not be implementing or calling this trait directly.**
18
///
19
/// The T type is the type received by [`Self::zvl_binary_search()`], as well as the one used
20
/// for human-readable serialization.
21
///
22
/// Methods are prefixed with `zvl_*` to avoid clashes with methods on the types themselves
23
pub trait ZeroVecLike<T: ?Sized> {
24
    /// The type returned by `Self::get()`
25
    type GetType: ?Sized + 'static;
26
    /// A fully borrowed version of this
27
    type SliceVariant: ZeroVecLike<T, GetType = Self::GetType> + ?Sized;
28
29
    /// Create a new, empty borrowed variant
30
    fn zvl_new_borrowed() -> &'static Self::SliceVariant;
31
32
    /// Search for a key in a sorted vector, returns `Ok(index)` if found,
33
    /// returns `Err(insert_index)` if not found, where `insert_index` is the
34
    /// index where it should be inserted to maintain sort order.
35
    fn zvl_binary_search(&self, k: &T) -> Result<usize, usize>
36
    where
37
        T: Ord;
38
    /// Search for a key within a certain range in a sorted vector.
39
    /// Returns `None` if the range is out of bounds, and
40
    /// `Ok` or `Err` in the same way as `zvl_binary_search`.
41
    /// Indices are returned relative to the start of the range.
42
    fn zvl_binary_search_in_range(
43
        &self,
44
        k: &T,
45
        range: Range<usize>,
46
    ) -> Option<Result<usize, usize>>
47
    where
48
        T: Ord;
49
50
    /// Search for a key in a sorted vector by a predicate, returns `Ok(index)` if found,
51
    /// returns `Err(insert_index)` if not found, where `insert_index` is the
52
    /// index where it should be inserted to maintain sort order.
53
    fn zvl_binary_search_by(&self, predicate: impl FnMut(&T) -> Ordering) -> Result<usize, usize>;
54
    /// Search for a key within a certain range in a sorted vector by a predicate.
55
    /// Returns `None` if the range is out of bounds, and
56
    /// `Ok` or `Err` in the same way as `zvl_binary_search`.
57
    /// Indices are returned relative to the start of the range.
58
    fn zvl_binary_search_in_range_by(
59
        &self,
60
        predicate: impl FnMut(&T) -> Ordering,
61
        range: Range<usize>,
62
    ) -> Option<Result<usize, usize>>;
63
64
    /// Get element at `index`
65
    fn zvl_get(&self, index: usize) -> Option<&Self::GetType>;
66
    /// The length of this vector
67
    fn zvl_len(&self) -> usize;
68
    /// Check if this vector is in ascending order according to `T`s `Ord` impl
69
0
    fn zvl_is_ascending(&self) -> bool
70
0
    where
71
0
        T: Ord,
72
    {
73
0
        if let Some(first) = self.zvl_get(0) {
74
0
            let mut prev = first;
75
0
            for i in 1..self.zvl_len() {
76
                #[allow(clippy::unwrap_used)] // looping over the valid indices
77
0
                let curr = self.zvl_get(i).unwrap();
78
0
                if Self::get_cmp_get(prev, curr) != Ordering::Less {
79
0
                    return false;
80
0
                }
81
0
                prev = curr;
82
            }
83
0
        }
84
0
        true
85
0
    }
86
    /// Check if this vector is empty
87
0
    fn zvl_is_empty(&self) -> bool {
88
0
        self.zvl_len() == 0
89
0
    }
90
91
    /// Construct a borrowed variant by borrowing from `&self`.
92
    ///
93
    /// This function behaves like `&'b self -> Self::SliceVariant<'b>`,
94
    /// where `'b` is the lifetime of the reference to this object.
95
    ///
96
    /// Note: We rely on the compiler recognizing `'a` and `'b` as covariant and
97
    /// casting `&'b Self<'a>` to `&'b Self<'b>` when this gets called, which works
98
    /// out for `ZeroVec` and `VarZeroVec` containers just fine.
99
    fn zvl_as_borrowed(&self) -> &Self::SliceVariant;
100
101
    /// Compare this type with a `Self::GetType`. This must produce the same result as
102
    /// if `g` were converted to `Self`
103
    #[inline]
104
0
    fn t_cmp_get(t: &T, g: &Self::GetType) -> Ordering
105
0
    where
106
0
        T: Ord,
107
    {
108
0
        Self::zvl_get_as_t(g, |g| t.cmp(g))
109
0
    }
110
111
    /// Compare two values of `Self::GetType`. This must produce the same result as
112
    /// if both `a` and `b` were converted to `Self`
113
    #[inline]
114
0
    fn get_cmp_get(a: &Self::GetType, b: &Self::GetType) -> Ordering
115
0
    where
116
0
        T: Ord,
117
    {
118
0
        Self::zvl_get_as_t(a, |a| Self::zvl_get_as_t(b, |b| a.cmp(b)))
119
0
    }
120
121
    /// Obtain a reference to T, passed to a closure
122
    ///
123
    /// This uses a callback because it's not possible to return owned-or-borrowed
124
    /// types without GATs
125
    ///
126
    /// Impls should guarantee that the callback function is be called exactly once.
127
    fn zvl_get_as_t<R>(g: &Self::GetType, f: impl FnOnce(&T) -> R) -> R;
128
}
129
130
/// Trait abstracting over [`ZeroVec`] and [`VarZeroVec`], for use in [`ZeroMap`](super::ZeroMap). **You
131
/// should not be implementing or calling this trait directly.**
132
///
133
/// This trait augments [`ZeroVecLike`] with methods allowing for mutation of the underlying
134
/// vector for owned vector types.
135
///
136
/// Methods are prefixed with `zvl_*` to avoid clashes with methods on the types themselves
137
pub trait MutableZeroVecLike<'a, T: ?Sized>: ZeroVecLike<T> {
138
    /// The type returned by `Self::remove()` and `Self::replace()`
139
    type OwnedType;
140
141
    /// Insert an element at `index`
142
    fn zvl_insert(&mut self, index: usize, value: &T);
143
    /// Remove the element at `index` (panicking if nonexistant)
144
    fn zvl_remove(&mut self, index: usize) -> Self::OwnedType;
145
    /// Replace the element at `index` with another one, returning the old element
146
    fn zvl_replace(&mut self, index: usize, value: &T) -> Self::OwnedType;
147
    /// Push an element to the end of this vector
148
    fn zvl_push(&mut self, value: &T);
149
    /// Create a new, empty vector, with given capacity
150
    fn zvl_with_capacity(cap: usize) -> Self;
151
    /// Remove all elements from the vector
152
    fn zvl_clear(&mut self);
153
    /// Reserve space for `addl` additional elements
154
    fn zvl_reserve(&mut self, addl: usize);
155
    /// Applies the permutation such that `before.zvl_get(permutation[i]) == after.zvl_get(i)`.
156
    ///
157
    /// # Panics
158
    /// If `permutation` is not a valid permutation of length `zvl_len()`.
159
    fn zvl_permute(&mut self, permutation: &mut [usize]);
160
161
    /// Convert an owned value to a borrowed T
162
    fn owned_as_t(o: &Self::OwnedType) -> &T;
163
164
    /// Construct from the borrowed version of the type
165
    ///
166
    /// These are useful to ensure serialization parity between borrowed and owned versions
167
    fn zvl_from_borrowed(b: &'a Self::SliceVariant) -> Self;
168
    /// Extract the inner borrowed variant if possible. Returns `None` if the data is owned.
169
    ///
170
    /// This function behaves like `&'_ self -> Self::SliceVariant<'a>`,
171
    /// where `'a` is the lifetime of this object's borrowed data.
172
    ///
173
    /// This function is similar to matching the `Borrowed` variant of `ZeroVec`
174
    /// or `VarZeroVec`, returning the inner borrowed type.
175
    fn zvl_as_borrowed_inner(&self) -> Option<&'a Self::SliceVariant>;
176
}
177
178
impl<'a, T> ZeroVecLike<T> for ZeroVec<'a, T>
179
where
180
    T: 'a + AsULE + Copy,
181
{
182
    type GetType = T::ULE;
183
    type SliceVariant = ZeroSlice<T>;
184
185
0
    fn zvl_new_borrowed() -> &'static Self::SliceVariant {
186
0
        ZeroSlice::<T>::new_empty()
187
0
    }
188
0
    fn zvl_binary_search(&self, k: &T) -> Result<usize, usize>
189
0
    where
190
0
        T: Ord,
191
    {
192
0
        ZeroSlice::binary_search(self, k)
193
0
    }
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_calendar::types::MonthCode> as zerovec::map::vecs::ZeroVecLike<icu_calendar::types::MonthCode>>::zvl_binary_search
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_timezone::provider::MetazoneId> as zerovec::map::vecs::ZeroVecLike<icu_timezone::provider::MetazoneId>>::zvl_binary_search
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_timezone::provider::TimeZoneBcp47Id> as zerovec::map::vecs::ZeroVecLike<icu_timezone::provider::TimeZoneBcp47Id>>::zvl_binary_search
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<tinystr::unvalidated::UnvalidatedTinyAsciiStr<2>> as zerovec::map::vecs::ZeroVecLike<tinystr::unvalidated::UnvalidatedTinyAsciiStr<2>>>::zvl_binary_search
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>> as zerovec::map::vecs::ZeroVecLike<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>>>::zvl_binary_search
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>> as zerovec::map::vecs::ZeroVecLike<tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>>>::zvl_binary_search
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<tinystr::unvalidated::UnvalidatedTinyAsciiStr<7>> as zerovec::map::vecs::ZeroVecLike<tinystr::unvalidated::UnvalidatedTinyAsciiStr<7>>>::zvl_binary_search
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<tinystr::unvalidated::UnvalidatedTinyAsciiStr<8>> as zerovec::map::vecs::ZeroVecLike<tinystr::unvalidated::UnvalidatedTinyAsciiStr<8>>>::zvl_binary_search
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>, tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>)> as zerovec::map::vecs::ZeroVecLike<(tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>, tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>)>>::zvl_binary_search
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>, tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>)> as zerovec::map::vecs::ZeroVecLike<(tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>, tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>)>>::zvl_binary_search
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>, tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>)> as zerovec::map::vecs::ZeroVecLike<(tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>, tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>)>>::zvl_binary_search
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<_> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_binary_search
194
0
    fn zvl_binary_search_in_range(&self, k: &T, range: Range<usize>) -> Option<Result<usize, usize>>
195
0
    where
196
0
        T: Ord,
197
    {
198
0
        let zs: &ZeroSlice<T> = self;
199
0
        zs.zvl_binary_search_in_range(k, range)
200
0
    }
201
0
    fn zvl_binary_search_by(
202
0
        &self,
203
0
        mut predicate: impl FnMut(&T) -> Ordering,
204
0
    ) -> Result<usize, usize> {
205
0
        ZeroSlice::binary_search_by(self, |probe| predicate(&probe))
206
0
    }
207
0
    fn zvl_binary_search_in_range_by(
208
0
        &self,
209
0
        predicate: impl FnMut(&T) -> Ordering,
210
0
        range: Range<usize>,
211
0
    ) -> Option<Result<usize, usize>> {
212
0
        let zs: &ZeroSlice<T> = self;
213
0
        zs.zvl_binary_search_in_range_by(predicate, range)
214
0
    }
215
0
    fn zvl_get(&self, index: usize) -> Option<&T::ULE> {
216
0
        self.get_ule_ref(index)
217
0
    }
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<u16> as zerovec::map::vecs::ZeroVecLike<u16>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<tinystr::ascii::TinyAsciiStr<7>> as zerovec::map::vecs::ZeroVecLike<tinystr::ascii::TinyAsciiStr<7>>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::region::Region> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::region::Region>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::script::Script> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::script::Script>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::variant::Variant> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::variant::Variant>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::language::Language> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::language::Language>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(icu_locid::subtags::script::Script, icu_locid::subtags::region::Region)> as zerovec::map::vecs::ZeroVecLike<(icu_locid::subtags::script::Script, icu_locid::subtags::region::Region)>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(icu_locid::subtags::language::Language, core::option::Option<icu_locid::subtags::script::Script>, core::option::Option<icu_locid::subtags::region::Region>)> as zerovec::map::vecs::ZeroVecLike<(icu_locid::subtags::language::Language, core::option::Option<icu_locid::subtags::script::Script>, core::option::Option<icu_locid::subtags::region::Region>)>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(icu_locid::subtags::language::Language, icu_locid::subtags::region::Region)> as zerovec::map::vecs::ZeroVecLike<(icu_locid::subtags::language::Language, icu_locid::subtags::region::Region)>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(icu_locid::subtags::language::Language, icu_locid::subtags::script::Script)> as zerovec::map::vecs::ZeroVecLike<(icu_locid::subtags::language::Language, icu_locid::subtags::script::Script)>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<_> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_get
218
0
    fn zvl_len(&self) -> usize {
219
0
        ZeroSlice::len(self)
220
0
    }
221
0
    fn zvl_as_borrowed(&self) -> &ZeroSlice<T> {
222
0
        self
223
0
    }
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<u16> as zerovec::map::vecs::ZeroVecLike<u16>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_timezone::types::ZoneVariant> as zerovec::map::vecs::ZeroVecLike<icu_timezone::types::ZoneVariant>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_timezone::provider::MetazoneId> as zerovec::map::vecs::ZeroVecLike<icu_timezone::provider::MetazoneId>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<core::option::Option<icu_timezone::provider::MetazoneId>> as zerovec::map::vecs::ZeroVecLike<core::option::Option<icu_timezone::provider::MetazoneId>>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_timezone::provider::TimeZoneBcp47Id> as zerovec::map::vecs::ZeroVecLike<icu_timezone::provider::TimeZoneBcp47Id>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<i32> as zerovec::map::vecs::ZeroVecLike<i32>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>> as zerovec::map::vecs::ZeroVecLike<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>> as zerovec::map::vecs::ZeroVecLike<tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::region::Region> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::region::Region>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::script::Script> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::script::Script>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<_> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_as_borrowed
224
    #[inline]
225
0
    fn zvl_get_as_t<R>(g: &Self::GetType, f: impl FnOnce(&T) -> R) -> R {
226
0
        f(&T::from_unaligned(*g))
227
0
    }
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<u16> as zerovec::map::vecs::ZeroVecLike<u16>>::zvl_get_as_t::<core::option::Option<u16>, <zerovec::map::map::ZeroMap<icu_properties::provider::names::NormalizedPropertyNameStr, u16>>::get_copied_at::{closure#0}>
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::region::Region> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::region::Region>>::zvl_get_as_t::<core::option::Option<icu_locid::subtags::region::Region>, <zerovec::map::map::ZeroMap<(tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>, tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>), icu_locid::subtags::region::Region>>::get_copied_at::{closure#0}>
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::script::Script> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::script::Script>>::zvl_get_as_t::<core::option::Option<icu_locid::subtags::script::Script>, <zerovec::map2d::cursor::ZeroMap2dCursor<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>, tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>, icu_locid::subtags::script::Script>>::get1_copied_at::{closure#0}>
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::script::Script> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::script::Script>>::zvl_get_as_t::<core::option::Option<icu_locid::subtags::script::Script>, <zerovec::map::map::ZeroMap<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>, icu_locid::subtags::script::Script>>::get_copied_at::{closure#0}>
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::script::Script> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::script::Script>>::zvl_get_as_t::<core::option::Option<icu_locid::subtags::script::Script>, <zerovec::map::map::ZeroMap<(tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>, tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>), icu_locid::subtags::script::Script>>::get_copied_at::{closure#0}>
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::language::Language> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::language::Language>>::zvl_get_as_t::<core::option::Option<icu_locid::subtags::language::Language>, <zerovec::map::map::ZeroMap<(tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>, tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>), icu_locid::subtags::language::Language>>::get_copied_at::{closure#0}>
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(icu_locid::subtags::script::Script, icu_locid::subtags::region::Region)> as zerovec::map::vecs::ZeroVecLike<(icu_locid::subtags::script::Script, icu_locid::subtags::region::Region)>>::zvl_get_as_t::<core::option::Option<(icu_locid::subtags::script::Script, icu_locid::subtags::region::Region)>, <zerovec::map::map::ZeroMap<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>, (icu_locid::subtags::script::Script, icu_locid::subtags::region::Region)>>::get_copied_at::{closure#0}>
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(icu_locid::subtags::language::Language, core::option::Option<icu_locid::subtags::script::Script>, core::option::Option<icu_locid::subtags::region::Region>)> as zerovec::map::vecs::ZeroVecLike<(icu_locid::subtags::language::Language, core::option::Option<icu_locid::subtags::script::Script>, core::option::Option<icu_locid::subtags::region::Region>)>>::zvl_get_as_t::<core::option::Option<(icu_locid::subtags::language::Language, core::option::Option<icu_locid::subtags::script::Script>, core::option::Option<icu_locid::subtags::region::Region>)>, <zerovec::map::map::ZeroMap<zerovec::ule::unvalidated::UnvalidatedStr, (icu_locid::subtags::language::Language, core::option::Option<icu_locid::subtags::script::Script>, core::option::Option<icu_locid::subtags::region::Region>)>>::get_copied_at::{closure#0}>
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(icu_locid::subtags::language::Language, icu_locid::subtags::region::Region)> as zerovec::map::vecs::ZeroVecLike<(icu_locid::subtags::language::Language, icu_locid::subtags::region::Region)>>::zvl_get_as_t::<core::option::Option<(icu_locid::subtags::language::Language, icu_locid::subtags::region::Region)>, <zerovec::map::map::ZeroMap<tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>, (icu_locid::subtags::language::Language, icu_locid::subtags::region::Region)>>::get_copied_at::{closure#0}>
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(icu_locid::subtags::language::Language, icu_locid::subtags::script::Script)> as zerovec::map::vecs::ZeroVecLike<(icu_locid::subtags::language::Language, icu_locid::subtags::script::Script)>>::zvl_get_as_t::<core::option::Option<(icu_locid::subtags::language::Language, icu_locid::subtags::script::Script)>, <zerovec::map::map::ZeroMap<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>, (icu_locid::subtags::language::Language, icu_locid::subtags::script::Script)>>::get_copied_at::{closure#0}>
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<_> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_get_as_t::<_, _>
228
}
229
230
impl<T> ZeroVecLike<T> for ZeroSlice<T>
231
where
232
    T: AsULE + Copy,
233
{
234
    type GetType = T::ULE;
235
    type SliceVariant = ZeroSlice<T>;
236
237
0
    fn zvl_new_borrowed() -> &'static Self::SliceVariant {
238
0
        ZeroSlice::<T>::new_empty()
239
0
    }
240
0
    fn zvl_binary_search(&self, k: &T) -> Result<usize, usize>
241
0
    where
242
0
        T: Ord,
243
    {
244
0
        ZeroSlice::binary_search(self, k)
245
0
    }
246
0
    fn zvl_binary_search_in_range(&self, k: &T, range: Range<usize>) -> Option<Result<usize, usize>>
247
0
    where
248
0
        T: Ord,
249
    {
250
0
        let subslice = self.get_subslice(range)?;
251
0
        Some(ZeroSlice::binary_search(subslice, k))
252
0
    }
Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_timezone::types::ZoneVariant> as zerovec::map::vecs::ZeroVecLike<icu_timezone::types::ZoneVariant>>::zvl_binary_search_in_range
Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>> as zerovec::map::vecs::ZeroVecLike<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>>>::zvl_binary_search_in_range
Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>> as zerovec::map::vecs::ZeroVecLike<tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>>>::zvl_binary_search_in_range
Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_binary_search_in_range
253
0
    fn zvl_binary_search_by(
254
0
        &self,
255
0
        mut predicate: impl FnMut(&T) -> Ordering,
256
0
    ) -> Result<usize, usize> {
257
0
        ZeroSlice::binary_search_by(self, |probe| predicate(&probe))
258
0
    }
259
0
    fn zvl_binary_search_in_range_by(
260
0
        &self,
261
0
        mut predicate: impl FnMut(&T) -> Ordering,
262
0
        range: Range<usize>,
263
0
    ) -> Option<Result<usize, usize>> {
264
0
        let subslice = self.get_subslice(range)?;
265
0
        Some(ZeroSlice::binary_search_by(subslice, |probe| {
266
0
            predicate(&probe)
267
0
        }))
268
0
    }
269
0
    fn zvl_get(&self, index: usize) -> Option<&T::ULE> {
270
0
        self.get_ule_ref(index)
271
0
    }
Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<core::option::Option<icu_timezone::provider::MetazoneId>> as zerovec::map::vecs::ZeroVecLike<core::option::Option<icu_timezone::provider::MetazoneId>>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<i32> as zerovec::map::vecs::ZeroVecLike<i32>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_locid::subtags::region::Region> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::region::Region>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_locid::subtags::script::Script> as zerovec::map::vecs::ZeroVecLike<icu_locid::subtags::script::Script>>::zvl_get
Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_get
272
0
    fn zvl_len(&self) -> usize {
273
0
        ZeroSlice::len(self)
274
0
    }
Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u16> as zerovec::map::vecs::ZeroVecLike<u16>>::zvl_len
Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_len
275
0
    fn zvl_as_borrowed(&self) -> &ZeroSlice<T> {
276
0
        self
277
0
    }
278
279
    #[inline]
280
0
    fn zvl_get_as_t<R>(g: &Self::GetType, f: impl FnOnce(&T) -> R) -> R {
281
0
        f(&T::from_unaligned(*g))
282
0
    }
283
}
284
285
impl<'a, T> MutableZeroVecLike<'a, T> for ZeroVec<'a, T>
286
where
287
    T: AsULE + Copy + 'static,
288
{
289
    type OwnedType = T;
290
0
    fn zvl_insert(&mut self, index: usize, value: &T) {
291
0
        self.with_mut(|v| v.insert(index, value.to_unaligned()))
292
0
    }
293
0
    fn zvl_remove(&mut self, index: usize) -> T {
294
0
        T::from_unaligned(self.with_mut(|v| v.remove(index)))
295
0
    }
296
0
    fn zvl_replace(&mut self, index: usize, value: &T) -> T {
297
        #[allow(clippy::indexing_slicing)]
298
0
        let unaligned = self.with_mut(|vec| {
299
0
            debug_assert!(index < vec.len());
300
0
            mem::replace(&mut vec[index], value.to_unaligned())
301
0
        });
302
0
        T::from_unaligned(unaligned)
303
0
    }
304
0
    fn zvl_push(&mut self, value: &T) {
305
0
        self.with_mut(|v| v.push(value.to_unaligned()))
306
0
    }
307
0
    fn zvl_with_capacity(cap: usize) -> Self {
308
0
        if cap == 0 {
309
0
            ZeroVec::new()
310
        } else {
311
0
            ZeroVec::new_owned(Vec::with_capacity(cap))
312
        }
313
0
    }
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_calendar::types::MonthCode> as zerovec::map::vecs::MutableZeroVecLike<icu_calendar::types::MonthCode>>::zvl_with_capacity
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>> as zerovec::map::vecs::MutableZeroVecLike<tinystr::unvalidated::UnvalidatedTinyAsciiStr<3>>>::zvl_with_capacity
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>> as zerovec::map::vecs::MutableZeroVecLike<tinystr::unvalidated::UnvalidatedTinyAsciiStr<4>>>::zvl_with_capacity
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::region::Region> as zerovec::map::vecs::MutableZeroVecLike<icu_locid::subtags::region::Region>>::zvl_with_capacity
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<icu_locid::subtags::script::Script> as zerovec::map::vecs::MutableZeroVecLike<icu_locid::subtags::script::Script>>::zvl_with_capacity
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<(icu_locid::subtags::language::Language, core::option::Option<icu_locid::subtags::script::Script>, core::option::Option<icu_locid::subtags::region::Region>)> as zerovec::map::vecs::MutableZeroVecLike<(icu_locid::subtags::language::Language, core::option::Option<icu_locid::subtags::script::Script>, core::option::Option<icu_locid::subtags::region::Region>)>>::zvl_with_capacity
Unexecuted instantiation: <zerovec::zerovec::ZeroVec<_> as zerovec::map::vecs::MutableZeroVecLike<_>>::zvl_with_capacity
314
0
    fn zvl_clear(&mut self) {
315
0
        self.with_mut(|v| v.clear())
316
0
    }
317
0
    fn zvl_reserve(&mut self, addl: usize) {
318
0
        self.with_mut(|v| v.reserve(addl))
319
0
    }
320
321
0
    fn owned_as_t(o: &Self::OwnedType) -> &T {
322
0
        o
323
0
    }
324
325
0
    fn zvl_from_borrowed(b: &'a ZeroSlice<T>) -> Self {
326
0
        b.as_zerovec()
327
0
    }
328
0
    fn zvl_as_borrowed_inner(&self) -> Option<&'a ZeroSlice<T>> {
329
0
        self.as_maybe_borrowed()
330
0
    }
331
332
    #[allow(clippy::indexing_slicing)] // documented panic
333
0
    fn zvl_permute(&mut self, permutation: &mut [usize]) {
334
0
        assert_eq!(permutation.len(), self.zvl_len());
335
336
0
        let vec = self.to_mut_slice();
337
338
0
        for cycle_start in 0..permutation.len() {
339
0
            let mut curr = cycle_start;
340
0
            let mut next = permutation[curr];
341
342
0
            while next != cycle_start {
343
0
                vec.swap(curr, next);
344
0
                // Make curr a self-cycle so we don't use it as a cycle_start later
345
0
                permutation[curr] = curr;
346
0
                curr = next;
347
0
                next = permutation[next];
348
0
            }
349
0
            permutation[curr] = curr;
350
        }
351
0
    }
352
}
353
354
impl<'a, T, F> ZeroVecLike<T> for VarZeroVec<'a, T, F>
355
where
356
    T: VarULE,
357
    T: ?Sized,
358
    F: VarZeroVecFormat,
359
{
360
    type GetType = T;
361
    type SliceVariant = VarZeroSlice<T, F>;
362
363
0
    fn zvl_new_borrowed() -> &'static Self::SliceVariant {
364
0
        VarZeroSlice::<T, F>::new_empty()
365
0
    }
366
0
    fn zvl_binary_search(&self, k: &T) -> Result<usize, usize>
367
0
    where
368
0
        T: Ord,
369
    {
370
0
        self.binary_search(k)
371
0
    }
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<zerovec::ule::unvalidated::UnvalidatedStr> as zerovec::map::vecs::ZeroVecLike<zerovec::ule::unvalidated::UnvalidatedStr>>::zvl_binary_search
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<zerovec::ule::unvalidated::UnvalidatedStr> as zerovec::map::vecs::ZeroVecLike<zerovec::ule::unvalidated::UnvalidatedStr>>::zvl_binary_search
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<icu_properties::provider::names::NormalizedPropertyNameStr> as zerovec::map::vecs::ZeroVecLike<icu_properties::provider::names::NormalizedPropertyNameStr>>::zvl_binary_search
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<_, _> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_binary_search
372
0
    fn zvl_binary_search_in_range(&self, k: &T, range: Range<usize>) -> Option<Result<usize, usize>>
373
0
    where
374
0
        T: Ord,
375
    {
376
0
        self.binary_search_in_range(k, range)
377
0
    }
378
0
    fn zvl_binary_search_by(&self, predicate: impl FnMut(&T) -> Ordering) -> Result<usize, usize> {
379
0
        self.binary_search_by(predicate)
380
0
    }
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<icu_properties::provider::names::NormalizedPropertyNameStr> as zerovec::map::vecs::ZeroVecLike<icu_properties::provider::names::NormalizedPropertyNameStr>>::zvl_binary_search_by::<icu_properties::props::get_loose_u16::{closure#0}>
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<zerovec::ule::unvalidated::UnvalidatedStr> as zerovec::map::vecs::ZeroVecLike<zerovec::ule::unvalidated::UnvalidatedStr>>::zvl_binary_search_by::<<icu_locid_transform::fallback::LocaleFallbackIteratorInner>::get_explicit_parent::{closure#0}::{closure#0}>
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<zerovec::ule::unvalidated::UnvalidatedStr> as zerovec::map::vecs::ZeroVecLike<zerovec::ule::unvalidated::UnvalidatedStr>>::zvl_binary_search_by::<<icu_locid_transform::fallback::LocaleFallbackIteratorInner>::get_explicit_parent::{closure#1}::{closure#0}>
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<_, _> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_binary_search_by::<_>
381
0
    fn zvl_binary_search_in_range_by(
382
0
        &self,
383
0
        predicate: impl FnMut(&T) -> Ordering,
384
0
        range: Range<usize>,
385
0
    ) -> Option<Result<usize, usize>> {
386
0
        self.binary_search_in_range_by(predicate, range)
387
0
    }
388
0
    fn zvl_get(&self, index: usize) -> Option<&T> {
389
0
        self.get(index)
390
0
    }
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<zerovec::zerovec::slice::ZeroSlice<icu_locid::subtags::region::Region>> as zerovec::map::vecs::ZeroVecLike<zerovec::zerovec::slice::ZeroSlice<icu_locid::subtags::region::Region>>>::zvl_get
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<str> as zerovec::map::vecs::ZeroVecLike<str>>::zvl_get
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<_, _> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_get
391
0
    fn zvl_len(&self) -> usize {
392
0
        self.len()
393
0
    }
394
395
0
    fn zvl_as_borrowed(&self) -> &VarZeroSlice<T, F> {
396
0
        self.as_slice()
397
0
    }
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<zerovec::ule::unvalidated::UnvalidatedStr> as zerovec::map::vecs::ZeroVecLike<zerovec::ule::unvalidated::UnvalidatedStr>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<str> as zerovec::map::vecs::ZeroVecLike<str>>::zvl_as_borrowed
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<_, _> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_as_borrowed
398
399
    #[inline]
400
0
    fn zvl_get_as_t<R>(g: &Self::GetType, f: impl FnOnce(&T) -> R) -> R {
401
0
        f(g)
402
0
    }
403
}
404
405
impl<T, F> ZeroVecLike<T> for VarZeroSlice<T, F>
406
where
407
    T: VarULE,
408
    T: ?Sized,
409
    F: VarZeroVecFormat,
410
{
411
    type GetType = T;
412
    type SliceVariant = VarZeroSlice<T, F>;
413
414
0
    fn zvl_new_borrowed() -> &'static Self::SliceVariant {
415
0
        VarZeroSlice::<T, F>::new_empty()
416
0
    }
417
0
    fn zvl_binary_search(&self, k: &T) -> Result<usize, usize>
418
0
    where
419
0
        T: Ord,
420
    {
421
0
        self.binary_search(k)
422
0
    }
Unexecuted instantiation: <zerovec::varzerovec::slice::VarZeroSlice<zerovec::ule::unvalidated::UnvalidatedStr> as zerovec::map::vecs::ZeroVecLike<zerovec::ule::unvalidated::UnvalidatedStr>>::zvl_binary_search
Unexecuted instantiation: <zerovec::varzerovec::slice::VarZeroSlice<_, _> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_binary_search
423
0
    fn zvl_binary_search_in_range(&self, k: &T, range: Range<usize>) -> Option<Result<usize, usize>>
424
0
    where
425
0
        T: Ord,
426
    {
427
0
        self.binary_search_in_range(k, range)
428
0
    }
429
0
    fn zvl_binary_search_by(&self, predicate: impl FnMut(&T) -> Ordering) -> Result<usize, usize> {
430
0
        self.binary_search_by(predicate)
431
0
    }
Unexecuted instantiation: <zerovec::varzerovec::slice::VarZeroSlice<zerovec::ule::unvalidated::UnvalidatedStr> as zerovec::map::vecs::ZeroVecLike<zerovec::ule::unvalidated::UnvalidatedStr>>::zvl_binary_search_by::<<icu_segmenter::complex::lstm::LstmSegmenter>::segment_utf16::{closure#0}::{closure#0}>
Unexecuted instantiation: <zerovec::varzerovec::slice::VarZeroSlice<_, _> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_binary_search_by::<_>
432
0
    fn zvl_binary_search_in_range_by(
433
0
        &self,
434
0
        predicate: impl FnMut(&T) -> Ordering,
435
0
        range: Range<usize>,
436
0
    ) -> Option<Result<usize, usize>> {
437
0
        self.binary_search_in_range_by(predicate, range)
438
0
    }
439
0
    fn zvl_get(&self, index: usize) -> Option<&T> {
440
0
        self.get(index)
441
0
    }
Unexecuted instantiation: <zerovec::varzerovec::slice::VarZeroSlice<str> as zerovec::map::vecs::ZeroVecLike<str>>::zvl_get
Unexecuted instantiation: <zerovec::varzerovec::slice::VarZeroSlice<_, _> as zerovec::map::vecs::ZeroVecLike<_>>::zvl_get
442
0
    fn zvl_len(&self) -> usize {
443
0
        self.len()
444
0
    }
445
446
0
    fn zvl_as_borrowed(&self) -> &VarZeroSlice<T, F> {
447
0
        self
448
0
    }
449
450
    #[inline]
451
0
    fn zvl_get_as_t<R>(g: &Self::GetType, f: impl FnOnce(&T) -> R) -> R {
452
0
        f(g)
453
0
    }
454
}
455
456
impl<'a, T, F> MutableZeroVecLike<'a, T> for VarZeroVec<'a, T, F>
457
where
458
    T: VarULE,
459
    T: ?Sized,
460
    F: VarZeroVecFormat,
461
{
462
    type OwnedType = Box<T>;
463
0
    fn zvl_insert(&mut self, index: usize, value: &T) {
464
0
        self.make_mut().insert(index, value)
465
0
    }
466
0
    fn zvl_remove(&mut self, index: usize) -> Box<T> {
467
0
        let vec = self.make_mut();
468
0
        debug_assert!(index < vec.len());
469
        #[allow(clippy::unwrap_used)]
470
0
        let old = vec.get(index).unwrap().to_boxed();
471
0
        vec.remove(index);
472
0
        old
473
0
    }
474
0
    fn zvl_replace(&mut self, index: usize, value: &T) -> Box<T> {
475
0
        let vec = self.make_mut();
476
0
        debug_assert!(index < vec.len());
477
        #[allow(clippy::unwrap_used)]
478
0
        let old = vec.get(index).unwrap().to_boxed();
479
0
        vec.replace(index, value);
480
0
        old
481
0
    }
482
0
    fn zvl_push(&mut self, value: &T) {
483
0
        let len = self.len();
484
0
        self.make_mut().insert(len, value)
485
0
    }
486
0
    fn zvl_with_capacity(cap: usize) -> Self {
487
0
        if cap == 0 {
488
0
            VarZeroVec::new()
489
        } else {
490
0
            VarZeroVec::Owned(VarZeroVecOwned::with_capacity(cap))
491
        }
492
0
    }
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<str> as zerovec::map::vecs::MutableZeroVecLike<str>>::zvl_with_capacity
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<zerovec::ule::unvalidated::UnvalidatedStr> as zerovec::map::vecs::MutableZeroVecLike<zerovec::ule::unvalidated::UnvalidatedStr>>::zvl_with_capacity
Unexecuted instantiation: <zerovec::varzerovec::vec::VarZeroVec<_, _> as zerovec::map::vecs::MutableZeroVecLike<_>>::zvl_with_capacity
493
0
    fn zvl_clear(&mut self) {
494
0
        self.make_mut().clear()
495
0
    }
496
0
    fn zvl_reserve(&mut self, addl: usize) {
497
0
        self.make_mut().reserve(addl)
498
0
    }
499
500
0
    fn owned_as_t(o: &Self::OwnedType) -> &T {
501
0
        o
502
0
    }
503
504
0
    fn zvl_from_borrowed(b: &'a VarZeroSlice<T, F>) -> Self {
505
0
        b.as_varzerovec()
506
0
    }
507
0
    fn zvl_as_borrowed_inner(&self) -> Option<&'a VarZeroSlice<T, F>> {
508
0
        if let VarZeroVec::Borrowed(b) = *self {
509
0
            Some(b)
510
        } else {
511
0
            None
512
        }
513
0
    }
514
515
    #[allow(clippy::unwrap_used)] // documented panic
516
0
    fn zvl_permute(&mut self, permutation: &mut [usize]) {
517
0
        assert_eq!(permutation.len(), self.zvl_len());
518
519
0
        let mut result = VarZeroVecOwned::new();
520
0
        for &i in permutation.iter() {
521
0
            result.push(self.get(i).unwrap());
522
0
        }
523
0
        *self = VarZeroVec::Owned(result);
524
0
    }
525
}
526
527
impl<'a> ZeroVecLike<usize> for FlexZeroVec<'a> {
528
    type GetType = [u8];
529
    type SliceVariant = FlexZeroSlice;
530
531
0
    fn zvl_new_borrowed() -> &'static Self::SliceVariant {
532
0
        FlexZeroSlice::new_empty()
533
0
    }
534
0
    fn zvl_binary_search(&self, k: &usize) -> Result<usize, usize> {
535
0
        FlexZeroSlice::binary_search(self, *k)
536
0
    }
537
0
    fn zvl_binary_search_in_range(
538
0
        &self,
539
0
        k: &usize,
540
0
        range: Range<usize>,
541
0
    ) -> Option<Result<usize, usize>> {
542
0
        FlexZeroSlice::binary_search_in_range(self, *k, range)
543
0
    }
544
0
    fn zvl_binary_search_by(
545
0
        &self,
546
0
        mut predicate: impl FnMut(&usize) -> Ordering,
547
0
    ) -> Result<usize, usize> {
548
0
        FlexZeroSlice::binary_search_by(self, |probe| predicate(&probe))
549
0
    }
550
0
    fn zvl_binary_search_in_range_by(
551
0
        &self,
552
0
        mut predicate: impl FnMut(&usize) -> Ordering,
553
0
        range: Range<usize>,
554
0
    ) -> Option<Result<usize, usize>> {
555
0
        FlexZeroSlice::binary_search_in_range_by(self, |probe| predicate(&probe), range)
556
0
    }
557
0
    fn zvl_get(&self, index: usize) -> Option<&[u8]> {
558
0
        self.get_chunk(index)
559
0
    }
560
0
    fn zvl_len(&self) -> usize {
561
0
        FlexZeroSlice::len(self)
562
0
    }
563
564
0
    fn zvl_as_borrowed(&self) -> &FlexZeroSlice {
565
0
        self
566
0
    }
567
568
    #[inline]
569
0
    fn zvl_get_as_t<R>(g: &[u8], f: impl FnOnce(&usize) -> R) -> R {
570
0
        f(&crate::chunk_to_usize(g, g.len()))
571
0
    }
572
}
573
574
impl ZeroVecLike<usize> for FlexZeroSlice {
575
    type GetType = [u8];
576
    type SliceVariant = FlexZeroSlice;
577
578
0
    fn zvl_new_borrowed() -> &'static Self::SliceVariant {
579
0
        FlexZeroSlice::new_empty()
580
0
    }
581
0
    fn zvl_binary_search(&self, k: &usize) -> Result<usize, usize> {
582
0
        FlexZeroSlice::binary_search(self, *k)
583
0
    }
584
0
    fn zvl_binary_search_in_range(
585
0
        &self,
586
0
        k: &usize,
587
0
        range: Range<usize>,
588
0
    ) -> Option<Result<usize, usize>> {
589
0
        FlexZeroSlice::binary_search_in_range(self, *k, range)
590
0
    }
591
0
    fn zvl_binary_search_by(
592
0
        &self,
593
0
        mut predicate: impl FnMut(&usize) -> Ordering,
594
0
    ) -> Result<usize, usize> {
595
0
        FlexZeroSlice::binary_search_by(self, |probe| predicate(&probe))
596
0
    }
597
0
    fn zvl_binary_search_in_range_by(
598
0
        &self,
599
0
        mut predicate: impl FnMut(&usize) -> Ordering,
600
0
        range: Range<usize>,
601
0
    ) -> Option<Result<usize, usize>> {
602
0
        FlexZeroSlice::binary_search_in_range_by(self, |probe| predicate(&probe), range)
603
0
    }
604
0
    fn zvl_get(&self, index: usize) -> Option<&[u8]> {
605
0
        self.get_chunk(index)
606
0
    }
607
0
    fn zvl_len(&self) -> usize {
608
0
        FlexZeroSlice::len(self)
609
0
    }
610
611
0
    fn zvl_as_borrowed(&self) -> &FlexZeroSlice {
612
0
        self
613
0
    }
614
615
    #[inline]
616
0
    fn zvl_get_as_t<R>(g: &Self::GetType, f: impl FnOnce(&usize) -> R) -> R {
617
0
        f(&crate::chunk_to_usize(g, g.len()))
618
0
    }
619
}
620
621
impl<'a> MutableZeroVecLike<'a, usize> for FlexZeroVec<'a> {
622
    type OwnedType = usize;
623
0
    fn zvl_insert(&mut self, index: usize, value: &usize) {
624
0
        self.to_mut().insert(index, *value)
625
0
    }
626
0
    fn zvl_remove(&mut self, index: usize) -> usize {
627
0
        self.to_mut().remove(index)
628
0
    }
629
0
    fn zvl_replace(&mut self, index: usize, value: &usize) -> usize {
630
        // TODO(#2028): Make this a single operation instead of two operations.
631
0
        let mutable = self.to_mut();
632
0
        let old_value = mutable.remove(index);
633
0
        mutable.insert(index, *value);
634
0
        old_value
635
0
    }
636
0
    fn zvl_push(&mut self, value: &usize) {
637
0
        self.to_mut().push(*value)
638
0
    }
639
0
    fn zvl_with_capacity(_cap: usize) -> Self {
640
        // There is no `FlexZeroVec::with_capacity()` because it is variable-width
641
0
        FlexZeroVec::Owned(FlexZeroVecOwned::new_empty())
642
0
    }
643
0
    fn zvl_clear(&mut self) {
644
0
        self.to_mut().clear()
645
0
    }
646
0
    fn zvl_reserve(&mut self, _addl: usize) {
647
        // There is no `FlexZeroVec::reserve()` because it is variable-width
648
0
    }
649
650
0
    fn owned_as_t(o: &Self::OwnedType) -> &usize {
651
0
        o
652
0
    }
653
654
0
    fn zvl_from_borrowed(b: &'a FlexZeroSlice) -> Self {
655
0
        b.as_flexzerovec()
656
0
    }
657
0
    fn zvl_as_borrowed_inner(&self) -> Option<&'a FlexZeroSlice> {
658
0
        if let FlexZeroVec::Borrowed(b) = *self {
659
0
            Some(b)
660
        } else {
661
0
            None
662
        }
663
0
    }
664
665
    #[allow(clippy::unwrap_used)] // documented panic
666
0
    fn zvl_permute(&mut self, permutation: &mut [usize]) {
667
0
        assert_eq!(permutation.len(), self.zvl_len());
668
0
        *self = permutation.iter().map(|&i| self.get(i).unwrap()).collect();
669
0
    }
670
}
671
672
#[cfg(test)]
673
mod test {
674
    use super::*;
675
676
    #[test]
677
    fn test_zerovec_binary_search_in_range() {
678
        let zv: ZeroVec<u16> = ZeroVec::from_slice_or_alloc(&[11, 22, 33, 44, 55, 66, 77]);
679
680
        // Full range search
681
        assert_eq!(zv.zvl_binary_search_in_range(&11, 0..7), Some(Ok(0)));
682
        assert_eq!(zv.zvl_binary_search_in_range(&12, 0..7), Some(Err(1)));
683
        assert_eq!(zv.zvl_binary_search_in_range(&44, 0..7), Some(Ok(3)));
684
        assert_eq!(zv.zvl_binary_search_in_range(&45, 0..7), Some(Err(4)));
685
        assert_eq!(zv.zvl_binary_search_in_range(&77, 0..7), Some(Ok(6)));
686
        assert_eq!(zv.zvl_binary_search_in_range(&78, 0..7), Some(Err(7)));
687
688
        // Out-of-range search
689
        assert_eq!(zv.zvl_binary_search_in_range(&44, 0..2), Some(Err(2)));
690
        assert_eq!(zv.zvl_binary_search_in_range(&44, 5..7), Some(Err(0)));
691
692
        // Offset search
693
        assert_eq!(zv.zvl_binary_search_in_range(&44, 2..5), Some(Ok(1)));
694
        assert_eq!(zv.zvl_binary_search_in_range(&45, 2..5), Some(Err(2)));
695
696
        // Out-of-bounds
697
        assert_eq!(zv.zvl_binary_search_in_range(&44, 0..100), None);
698
        assert_eq!(zv.zvl_binary_search_in_range(&44, 100..200), None);
699
    }
700
701
    #[test]
702
    fn test_permute() {
703
        let mut zv: ZeroVec<u16> = ZeroVec::from_slice_or_alloc(&[11, 22, 33, 44, 55, 66, 77]);
704
        let mut permutation = vec![3, 2, 1, 0, 6, 5, 4];
705
        zv.zvl_permute(&mut permutation);
706
        assert_eq!(&zv, &[44, 33, 22, 11, 77, 66, 55]);
707
708
        let mut vzv: VarZeroVec<str> = VarZeroVec::Owned(
709
            VarZeroVecOwned::try_from_elements(&["11", "22", "33", "44", "55", "66", "77"])
710
                .unwrap(),
711
        );
712
        let mut permutation = vec![3, 2, 1, 0, 6, 5, 4];
713
        vzv.zvl_permute(&mut permutation);
714
        assert_eq!(&vzv, &["44", "33", "22", "11", "77", "66", "55"]);
715
716
        let mut fzv: FlexZeroVec = [11, 22, 33, 44, 55, 66, 77].into_iter().collect();
717
        let mut permutation = vec![3, 2, 1, 0, 6, 5, 4];
718
        fzv.zvl_permute(&mut permutation);
719
        assert_eq!(
720
            fzv.iter().collect::<Vec<_>>(),
721
            [44, 33, 22, 11, 77, 66, 55].into_iter().collect::<Vec<_>>()
722
        );
723
    }
724
}