From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_data_structures/src/small_str.rs | 68 +++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 compiler/rustc_data_structures/src/small_str.rs (limited to 'compiler/rustc_data_structures/src/small_str.rs') diff --git a/compiler/rustc_data_structures/src/small_str.rs b/compiler/rustc_data_structures/src/small_str.rs new file mode 100644 index 000000000..800acb1b0 --- /dev/null +++ b/compiler/rustc_data_structures/src/small_str.rs @@ -0,0 +1,68 @@ +use smallvec::SmallVec; + +#[cfg(test)] +mod tests; + +/// Like SmallVec but for strings. +#[derive(Default)] +pub struct SmallStr(SmallVec<[u8; N]>); + +impl SmallStr { + #[inline] + pub fn new() -> Self { + SmallStr(SmallVec::default()) + } + + #[inline] + pub fn push_str(&mut self, s: &str) { + self.0.extend_from_slice(s.as_bytes()); + } + + #[inline] + pub fn empty(&self) -> bool { + self.0.is_empty() + } + + #[inline] + pub fn spilled(&self) -> bool { + self.0.spilled() + } + + #[inline] + pub fn as_str(&self) -> &str { + unsafe { std::str::from_utf8_unchecked(self.0.as_slice()) } + } +} + +impl std::ops::Deref for SmallStr { + type Target = str; + + #[inline] + fn deref(&self) -> &str { + self.as_str() + } +} + +impl> FromIterator for SmallStr { + #[inline] + fn from_iter(iter: T) -> Self + where + T: IntoIterator, + { + let mut s = SmallStr::default(); + s.extend(iter); + s + } +} + +impl> Extend for SmallStr { + #[inline] + fn extend(&mut self, iter: T) + where + T: IntoIterator, + { + for a in iter.into_iter() { + self.push_str(a.as_ref()); + } + } +} -- cgit v1.2.3