Debian Codegraph @

third_party/rust/icu_locid/src/extensions/unicode/attributes.rs

raw
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 super::Attribute;
6
7 use crate::shortvec::ShortBoxSlice;
8 use alloc::vec::Vec;
9 use core::ops::Deref;
10
11 /// A set of [`Attribute`] elements as defined in [`Unicode Extension Attributes`].
12 ///
13 /// [`Unicode Extension Attributes`]: https://unicode.org/reports/tr35/tr35.html#u_Extension
14 ///
15 /// # Examples
16 ///
17 /// ```
18 /// use icu::locid::extensions::unicode::{Attribute, Attributes};
19 ///
20 /// let attribute1: Attribute =
21 /// "foobar".parse().expect("Failed to parse a variant subtag.");
22 ///
23 /// let attribute2: Attribute = "testing"
24 /// .parse()
25 /// .expect("Failed to parse a variant subtag.");
26 /// let mut v = vec![attribute1, attribute2];
27 /// v.sort();
28 /// v.dedup();
29 ///
30 /// let attributes: Attributes = Attributes::from_vec_unchecked(v);
31 /// assert_eq!(attributes.to_string(), "foobar-testing");
32 /// ```
33 #[derive(Default, Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord)]
34 pub struct Attributes(ShortBoxSlice<Attribute>);
35
36 impl Attributes {
37 /// Returns a new empty set of attributes. Same as [`default()`](Default::default()), but is `const`.
38 ///
39 /// # Examples
40 ///
41 /// ```
42 /// use icu::locid::extensions::unicode::Attributes;
43 ///
44 /// assert_eq!(Attributes::new(), Attributes::default());
45 /// ```
46 #[inline]
47 pub const fn new() -> Self {
48 Self(ShortBoxSlice::new())
49 }
50
51 /// A constructor which takes a pre-sorted list of [`Attribute`] elements.
52 ///
53 ///
54 /// # Examples
55 ///
56 /// ```
57 /// use icu::locid::extensions::unicode::{Attribute, Attributes};
58 ///
59 /// let attribute1: Attribute = "foobar".parse().expect("Parsing failed.");
60 /// let attribute2: Attribute = "testing".parse().expect("Parsing failed.");
61 /// let mut v = vec![attribute1, attribute2];
62 /// v.sort();
63 /// v.dedup();
64 ///
65 /// let attributes = Attributes::from_vec_unchecked(v);
66 /// ```
67 ///
68 /// Notice: For performance- and memory-constrained environments, it is recommended
69 /// for the caller to use [`binary_search`](slice::binary_search) instead of [`sort`](slice::sort)
70 /// and [`dedup`](Vec::dedup()).
71 pub fn from_vec_unchecked(input: Vec<Attribute>) -> Self {
72 Self(input.into())
73 }
74
75 pub(crate) fn from_short_slice_unchecked(input: ShortBoxSlice<Attribute>) -> Self {
76 Self(input)
77 }
78
79 /// Empties the [`Attributes`] list.
80 ///
81 /// Returns the old list.
82 ///
83 /// # Examples
84 ///
85 /// ```
86 /// use icu::locid::extensions::unicode::{attribute, Attributes};
87 /// use writeable::assert_writeable_eq;
88 ///
89 /// let mut attributes = Attributes::from_vec_unchecked(vec![
90 /// attribute!("foobar"),
91 /// attribute!("testing"),
92 /// ]);
93 ///
94 /// assert_writeable_eq!(attributes, "foobar-testing");
95 ///
96 /// attributes.clear();
97 ///
98 /// assert_writeable_eq!(attributes, "");
99 /// ```
100 pub fn clear(&mut self) -> Self {
101 core::mem::take(self)
102 }
103
104 pub(crate) fn for_each_subtag_str<E, F>(&self, f: &mut F) -> Result<(), E>
105 where
106 F: FnMut(&str) -> Result<(), E>,
107 {
108 self.deref().iter().map(|t| t.as_str()).try_for_each(f)
109 }
110 }
111
112 impl_writeable_for_subtag_list!(Attributes, "foobar", "testing");
113
114 impl Deref for Attributes {
115 type Target = [Attribute];
116
117 fn deref(&self) -> &[Attribute] {
118 self.0.deref()
119 }
120 }