From 1b6a04ca5504955c571d1c97504fb45ea0befee4 Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Mon, 8 Jan 2024 01:21:28 +0400 Subject: Initial vendor packages Signed-off-by: Valentin Popov --- vendor/clap_builder/src/util/flat_set.rs | 107 +++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 vendor/clap_builder/src/util/flat_set.rs (limited to 'vendor/clap_builder/src/util/flat_set.rs') diff --git a/vendor/clap_builder/src/util/flat_set.rs b/vendor/clap_builder/src/util/flat_set.rs new file mode 100644 index 0000000..3e0b23d --- /dev/null +++ b/vendor/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 { + inner: Vec, +} + +impl FlatSet { + 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(&self, value: &Q) -> bool + where + T: Borrow, + Q: Eq, + { + for existing in &self.inner { + if existing.borrow() == value { + return true; + } + } + false + } + + pub fn retain(&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(&mut self, f: F) + where + F: FnMut(&T) -> K, + K: Ord, + { + self.inner.sort_by_key(f); + } +} + +impl Default for FlatSet { + fn default() -> Self { + Self { + inner: Default::default(), + } + } +} + +impl IntoIterator for FlatSet { + type Item = T; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.inner.into_iter() + } +} + +impl<'s, T: PartialEq + Eq> IntoIterator for &'s FlatSet { + type Item = &'s T; + type IntoIter = std::slice::Iter<'s, T>; + + fn into_iter(self) -> Self::IntoIter { + self.inner.iter() + } +} + +impl Extend for FlatSet { + fn extend>(&mut self, iter: I) { + for value in iter { + self.insert(value); + } + } +} + +impl FromIterator for FlatSet { + fn from_iter>(iter: I) -> Self { + let mut set = Self::new(); + for value in iter { + set.insert(value); + } + set + } +} -- cgit v1.2.3