diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/clap_builder/src/util/flat_set.rs | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | third_party/rust/clap_builder/src/util/flat_set.rs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/third_party/rust/clap_builder/src/util/flat_set.rs b/third_party/rust/clap_builder/src/util/flat_set.rs new file mode 100644 index 0000000000..3e0b23dae3 --- /dev/null +++ b/third_party/rust/clap_builder/src/util/flat_set.rs @@ -0,0 +1,107 @@ +#![allow(dead_code)] + +use std::borrow::Borrow; + +/// Flat (Vec) backed set +/// +/// This preserves insertion order +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct FlatSet<T> { + inner: Vec<T>, +} + +impl<T: PartialEq + Eq> FlatSet<T> { + pub(crate) fn new() -> Self { + Default::default() + } + + pub(crate) fn insert(&mut self, value: T) -> bool { + for existing in &self.inner { + if *existing == value { + return false; + } + } + self.inner.push(value); + true + } + + pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool + where + T: Borrow<Q>, + Q: Eq, + { + for existing in &self.inner { + if existing.borrow() == value { + return true; + } + } + false + } + + pub fn retain<F>(&mut self, f: F) + where + F: FnMut(&T) -> bool, + { + self.inner.retain(f); + } + + pub(crate) fn is_empty(&self) -> bool { + self.inner.is_empty() + } + + pub(crate) fn iter(&self) -> std::slice::Iter<'_, T> { + self.inner.iter() + } + + pub fn sort_by_key<K, F>(&mut self, f: F) + where + F: FnMut(&T) -> K, + K: Ord, + { + self.inner.sort_by_key(f); + } +} + +impl<T: PartialEq + Eq> Default for FlatSet<T> { + fn default() -> Self { + Self { + inner: Default::default(), + } + } +} + +impl<T: PartialEq + Eq> IntoIterator for FlatSet<T> { + type Item = T; + type IntoIter = std::vec::IntoIter<T>; + + fn into_iter(self) -> Self::IntoIter { + self.inner.into_iter() + } +} + +impl<'s, T: PartialEq + Eq> IntoIterator for &'s FlatSet<T> { + type Item = &'s T; + type IntoIter = std::slice::Iter<'s, T>; + + fn into_iter(self) -> Self::IntoIter { + self.inner.iter() + } +} + +impl<T: PartialEq + Eq> Extend<T> for FlatSet<T> { + fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { + for value in iter { + self.insert(value); + } + } +} + +impl<T: PartialEq + Eq> FromIterator<T> for FlatSet<T> { + fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { + let mut set = Self::new(); + for value in iter { + set.insert(value); + } + set + } +} |