icu_locale_core/extensions/transform/
fields.rs1use core::borrow::Borrow;
6use litemap::LiteMap;
7
8use super::Key;
9use super::Value;
10
11#[derive(#[automatically_derived]
impl ::core::clone::Clone for Fields {
#[inline]
fn clone(&self) -> Fields { Fields(::core::clone::Clone::clone(&self.0)) }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for Fields {
#[inline]
fn eq(&self, other: &Fields) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Fields {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_receiver_is_total_eq(&self) -> () {
let _: ::core::cmp::AssertParamIsEq<Inner>;
}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for Fields {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Fields",
&&self.0)
}
}Debug, #[automatically_derived]
impl ::core::default::Default for Fields {
#[inline]
fn default() -> Fields { Fields(::core::default::Default::default()) }
}Default, #[automatically_derived]
impl ::core::hash::Hash for Fields {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
::core::hash::Hash::hash(&self.0, state)
}
}Hash, #[automatically_derived]
impl ::core::cmp::PartialOrd for Fields {
#[inline]
fn partial_cmp(&self, other: &Fields)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for Fields {
#[inline]
fn cmp(&self, other: &Fields) -> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}Ord)]
34pub struct Fields(Inner);
35
36#[cfg(feature = "alloc")]
37type Inner = LiteMap<Key, Value>;
38#[cfg(not(feature = "alloc"))]
39type Inner = LiteMap<Key, Value, &'static [(Key, Value)]>;
40
41impl Fields {
42 #[inline]
52 pub const fn new() -> Self {
53 Self(LiteMap::new())
54 }
55
56 pub fn is_empty(&self) -> bool {
71 self.0.is_empty()
72 }
73
74 pub fn clear(&mut self) -> Self {
93 core::mem::take(self)
94 }
95
96 pub fn contains_key<Q>(&self, key: &Q) -> bool
112 where
113 Key: Borrow<Q>,
114 Q: Ord,
115 {
116 self.0.contains_key(key)
117 }
118
119 pub fn get<Q>(&self, key: &Q) -> Option<&Value>
135 where
136 Key: Borrow<Q>,
137 Q: Ord,
138 {
139 self.0.get(key)
140 }
141
142 #[cfg(feature = "alloc")]
164 pub fn set(&mut self, key: Key, value: Value) -> Option<Value> {
165 self.0.insert(key, value)
166 }
167
168 #[cfg(feature = "alloc")]
193 pub fn retain_by_key<F>(&mut self, mut predicate: F)
194 where
195 F: FnMut(&Key) -> bool,
196 {
197 self.0.retain(|k, _| predicate(k))
198 }
199
200 pub(crate) fn for_each_subtag_str<E, F>(&self, f: &mut F) -> Result<(), E>
201 where
202 F: FnMut(&str) -> Result<(), E>,
203 {
204 for (k, v) in self.0.iter() {
205 f(k.as_str())?;
206 v.for_each_subtag_str(f)?;
207 }
208 Ok(())
209 }
210
211 #[cfg(test)]
213 pub(crate) fn from_tuple_vec(v: Vec<(Key, Value)>) -> Self {
214 v.into_iter().collect()
215 }
216}
217
218#[cfg(feature = "alloc")]
220impl From<LiteMap<Key, Value>> for Fields {
221 fn from(map: LiteMap<Key, Value>) -> Self {
222 Self(map)
223 }
224}
225
226#[cfg(feature = "alloc")]
228impl core::iter::FromIterator<(Key, Value)> for Fields {
229 fn from_iter<I: IntoIterator<Item = (Key, Value)>>(iter: I) -> Self {
230 LiteMap::from_iter(iter).into()
231 }
232}
233
234impl writeable::Writeable for Fields {
fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W)
-> core::fmt::Result {
let mut initial = true;
self.for_each_subtag_str(&mut |subtag|
{
if initial {
initial = false;
} else { sink.write_char('-')?; }
sink.write_str(subtag)
})
}
#[inline]
fn writeable_length_hint(&self) -> writeable::LengthHint {
let mut result = writeable::LengthHint::exact(0);
let mut initial = true;
self.for_each_subtag_str::<core::convert::Infallible,
_>(&mut |subtag|
{
if initial { initial = false; } else { result += 1; }
result += subtag.len();
Ok(())
}).expect("infallible");
result
}
}
impl core::fmt::Display for Fields {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
::writeable::Writeable::write_to(&self, f)
}
}impl_writeable_for_key_value!(Fields, "h0", "hybrid", "m0", "m0-true");