diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-07 05:48:48 +0000 |
commit | ef24de24a82fe681581cc130f342363c47c0969a (patch) | |
tree | 0d494f7e1a38b95c92426f58fe6eaa877303a86c /vendor/icu_locid/src/helpers.rs | |
parent | Releasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip |
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/icu_locid/src/helpers.rs')
-rw-r--r-- | vendor/icu_locid/src/helpers.rs | 127 |
1 files changed, 86 insertions, 41 deletions
diff --git a/vendor/icu_locid/src/helpers.rs b/vendor/icu_locid/src/helpers.rs index 42b2b7286..d12435fbf 100644 --- a/vendor/icu_locid/src/helpers.rs +++ b/vendor/icu_locid/src/helpers.rs @@ -7,6 +7,7 @@ use core::iter::FromIterator; use alloc::boxed::Box; use alloc::vec; use alloc::vec::Vec; +use core::ops::{Deref, DerefMut}; use litemap::store::*; /// Internal: A vector that supports no-allocation, constant values if length 0 or 1. @@ -43,24 +44,6 @@ impl<T> ShortSlice<T> { } #[inline] - pub fn as_slice(&self) -> &[T] { - match self { - ShortSlice::ZeroOne(None) => &[], - ShortSlice::ZeroOne(Some(v)) => core::slice::from_ref(v), - ShortSlice::Multi(v) => v, - } - } - - #[inline] - pub fn as_mut_slice(&mut self) -> &mut [T] { - match self { - ShortSlice::ZeroOne(None) => &mut [], - ShortSlice::ZeroOne(Some(v)) => core::slice::from_mut(v), - ShortSlice::Multi(v) => v, - } - } - - #[inline] pub const fn single(&self) -> Option<&T> { match self { ShortSlice::ZeroOne(Some(v)) => Some(v), @@ -134,6 +117,43 @@ impl<T> ShortSlice<T> { pub fn clear(&mut self) { let _ = core::mem::replace(self, ShortSlice::ZeroOne(None)); } + + pub fn retain<F>(&mut self, mut f: F) + where + F: FnMut(&T) -> bool, + { + *self = match core::mem::take(self) { + Self::ZeroOne(Some(one)) if f(&one) => Self::ZeroOne(Some(one)), + Self::ZeroOne(_) => Self::ZeroOne(None), + Self::Multi(slice) => { + let mut vec = slice.into_vec(); + vec.retain(f); + Self::from(vec) + } + }; + } +} + +impl<T> Deref for ShortSlice<T> { + type Target = [T]; + + fn deref(&self) -> &Self::Target { + match self { + ShortSlice::ZeroOne(None) => &[], + ShortSlice::ZeroOne(Some(v)) => core::slice::from_ref(v), + ShortSlice::Multi(v) => v, + } + } +} + +impl<T> DerefMut for ShortSlice<T> { + fn deref_mut(&mut self) -> &mut Self::Target { + match self { + ShortSlice::ZeroOne(None) => &mut [], + ShortSlice::ZeroOne(Some(v)) => core::slice::from_mut(v), + ShortSlice::Multi(v) => v, + } + } } impl<T> From<Vec<T>> for ShortSlice<T> { @@ -155,7 +175,18 @@ impl<T> Default for ShortSlice<T> { impl<T> FromIterator<T> for ShortSlice<T> { fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { - iter.into_iter().collect::<Vec<_>>().into() + let mut iter = iter.into_iter(); + match (iter.next(), iter.next()) { + (Some(first), Some(second)) => { + // Size hint behaviour same as `Vec::extend` + 2 + let mut vec = Vec::with_capacity(iter.size_hint().0.saturating_add(3)); + vec.push(first); + vec.push(second); + vec.extend(iter); + Self::Multi(vec.into_boxed_slice()) + } + (first, _) => Self::ZeroOne(first), + } } } @@ -176,7 +207,7 @@ impl<K, V> Store<K, V> for ShortSlice<(K, V)> { #[inline] fn lm_get(&self, index: usize) -> Option<(&K, &V)> { - self.as_slice().get(index).map(|elt| (&elt.0, &elt.1)) + self.get(index).map(|elt| (&elt.0, &elt.1)) } #[inline] @@ -193,7 +224,7 @@ impl<K, V> Store<K, V> for ShortSlice<(K, V)> { where F: FnMut(&K) -> core::cmp::Ordering, { - self.as_slice().binary_search_by(|(k, _)| cmp(k)) + self.binary_search_by(|(k, _)| cmp(k)) } } @@ -212,9 +243,7 @@ impl<K, V> StoreMut<K, V> for ShortSlice<(K, V)> { fn lm_reserve(&mut self, _additional: usize) {} fn lm_get_mut(&mut self, index: usize) -> Option<(&K, &mut V)> { - self.as_mut_slice() - .get_mut(index) - .map(|elt| (&elt.0, &mut elt.1)) + self.get_mut(index).map(|elt| (&elt.0, &mut elt.1)) } fn lm_push(&mut self, key: K, value: V) { @@ -232,6 +261,13 @@ impl<K, V> StoreMut<K, V> for ShortSlice<(K, V)> { fn lm_clear(&mut self) { self.clear(); } + + fn lm_retain<F>(&mut self, mut predicate: F) + where + F: FnMut(&K, &V) -> bool, + { + self.retain(|(k, v)| predicate(k, v)) + } } impl<'a, K: 'a, V: 'a> StoreIterable<'a, K, V> for ShortSlice<(K, V)> { @@ -239,14 +275,14 @@ impl<'a, K: 'a, V: 'a> StoreIterable<'a, K, V> for ShortSlice<(K, V)> { core::iter::Map<core::slice::Iter<'a, (K, V)>, for<'r> fn(&'r (K, V)) -> (&'r K, &'r V)>; fn lm_iter(&'a self) -> Self::KeyValueIter { - self.as_slice().iter().map(|elt| (&elt.0, &elt.1)) + self.iter().map(|elt| (&elt.0, &elt.1)) } } impl<K, V> StoreFromIterator<K, V> for ShortSlice<(K, V)> {} #[test] -fn test_shortvec_impl() { +fn test_short_slice_impl() { litemap::testing::check_store::<ShortSlice<(u32, u64)>>(); } @@ -254,8 +290,9 @@ macro_rules! impl_tinystr_subtag { ( $(#[$doc:meta])* $name:ident, - $($full_name:ident)::+, + $($path:ident)::+, $macro_name:ident, + $legacy_macro_name:ident, $len_start:literal..=$len_end:literal, $tinystr_ident:ident, $validate:expr, @@ -278,7 +315,7 @@ macro_rules! impl_tinystr_subtag { /// # Examples /// /// ``` - #[doc = concat!("use icu_locid::", stringify!($($full_name)::+), ";")] + #[doc = concat!("use icu_locid::", stringify!($($path::)+), stringify!($name), ";")] /// #[doc = concat!("assert!(", stringify!($name), "::try_from_bytes(b", stringify!($good_example), ").is_ok());")] #[doc = concat!("assert!(", stringify!($name), "::try_from_bytes(b", stringify!($bad_example), ").is_err());")] @@ -349,6 +386,11 @@ macro_rules! impl_tinystr_subtag { self.0.as_str() } + #[doc(hidden)] + pub const fn into_tinystr(&self) -> tinystr::TinyAsciiStr<$len_end> { + self.0 + } + /// Compare with BCP-47 bytes. /// /// The return value is equivalent to what would happen if you first converted @@ -389,7 +431,7 @@ macro_rules! impl_tinystr_subtag { impl From<$name> for tinystr::TinyAsciiStr<$len_end> { fn from(input: $name) -> Self { - input.0 + input.into_tinystr() } } @@ -417,37 +459,40 @@ macro_rules! impl_tinystr_subtag { /// Parsing errors don't have to be handled at runtime: /// ``` /// assert_eq!( - #[doc = concat!(" icu_locid::", stringify!($macro_name), "!(", stringify!($good_example) ,"),")] - #[doc = concat!(" ", stringify!($good_example), ".parse::<icu_locid::", stringify!($($full_name)::+),">().unwrap()")] + #[doc = concat!(" icu_locid::", $(stringify!($path), "::",)+ stringify!($macro_name), "!(", stringify!($good_example) ,"),")] + #[doc = concat!(" ", stringify!($good_example), ".parse::<icu_locid::", $(stringify!($path), "::",)+ stringify!($name), ">().unwrap()")] /// ); /// ``` /// /// Invalid input is a compile failure: /// ```compile_fail,E0080 - #[doc = concat!("icu_locid::", stringify!($macro_name), "!(", stringify!($bad_example) ,");")] + #[doc = concat!("icu_locid::", $(stringify!($path), "::",)+ stringify!($macro_name), "!(", stringify!($bad_example) ,");")] /// ``` /// - #[doc = concat!("[`", stringify!($name), "`]: crate::", stringify!($($full_name)::+))] + #[doc = concat!("[`", stringify!($name), "`]: crate::", $(stringify!($path), "::",)+ stringify!($name))] #[macro_export] - macro_rules! $macro_name { + #[doc(hidden)] + macro_rules! $legacy_macro_name { ($string:literal) => {{ - use $crate::$($full_name)::+; + use $crate::$($path ::)+ $name; const R: $name = match $name::try_from_bytes($string.as_bytes()) { Ok(r) => r, #[allow(clippy::panic)] // const context - _ => panic!(concat!("Invalid ", stringify!($name), ": ", $string)), + _ => panic!(concat!("Invalid ", $(stringify!($path), "::",)+ stringify!($name), ": ", $string)), }; R }}; } + #[doc(inline)] + pub use $legacy_macro_name as $macro_name; #[cfg(feature = "databake")] impl databake::Bake for $name { fn bake(&self, env: &databake::CrateEnv) -> databake::TokenStream { env.insert("icu_locid"); let string = self.as_str(); - databake::quote! {::icu_locid::$macro_name!(#string) } + databake::quote! { icu_locid::$($path::)+ $macro_name!(#string) } } } @@ -610,20 +655,20 @@ macro_rules! impl_writeable_for_each_subtag_str_no_test { macro_rules! impl_writeable_for_subtag_list { ($type:tt, $sample1:literal, $sample2:literal) => { - impl_writeable_for_each_subtag_str_no_test!($type, selff, selff.0.len() == 1 => alloc::borrow::Cow::Borrowed(selff.0.as_slice().get(0).unwrap().as_str())); + impl_writeable_for_each_subtag_str_no_test!($type, selff, selff.0.len() == 1 => alloc::borrow::Cow::Borrowed(selff.0.get(0).unwrap().as_str())); #[test] fn test_writeable() { writeable::assert_writeable_eq!(&$type::default(), ""); writeable::assert_writeable_eq!( - &$type::from_vec_unchecked(alloc::vec![$sample1.parse().unwrap()]), + &$type::from_short_slice_unchecked(alloc::vec![$sample1.parse().unwrap()].into()), $sample1, ); writeable::assert_writeable_eq!( - &$type::from_vec_unchecked(vec![ + &$type::from_short_slice_unchecked(vec![ $sample1.parse().unwrap(), $sample2.parse().unwrap() - ]), + ].into()), core::concat!($sample1, "-", $sample2), ); } |