summaryrefslogtreecommitdiffstats
path: root/vendor/smol_str/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
commitdc0db358abe19481e475e10c32149b53370f1a1c (patch)
treeab8ce99c4b255ce46f99ef402c27916055b899ee /vendor/smol_str/src
parentReleasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff)
downloadrustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz
rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/smol_str/src')
-rw-r--r--vendor/smol_str/src/lib.rs118
1 files changed, 99 insertions, 19 deletions
diff --git a/vendor/smol_str/src/lib.rs b/vendor/smol_str/src/lib.rs
index 8c92e51e1..91dc6252c 100644
--- a/vendor/smol_str/src/lib.rs
+++ b/vendor/smol_str/src/lib.rs
@@ -2,6 +2,8 @@
extern crate alloc;
use alloc::{
+ borrow::Cow,
+ boxed::Box,
string::{String, ToString},
sync::Arc,
};
@@ -10,17 +12,18 @@ use core::{
cmp::{self, Ordering},
convert::Infallible,
fmt, hash, iter,
+ mem::transmute,
ops::Deref,
str::FromStr,
};
/// A `SmolStr` is a string type that has the following properties:
///
-/// * `size_of::<SmolStr>() == size_of::<String>()`
+/// * `size_of::<SmolStr>() == 24 (therefor == size_of::<String>() on 64 bit platforms)
/// * `Clone` is `O(1)`
/// * Strings are stack-allocated if they are:
-/// * Up to 22 bytes long
-/// * Longer than 22 bytes, but substrings of `WS` (see below). Such strings consist
+/// * Up to 23 bytes long
+/// * Longer than 23 bytes, but substrings of `WS` (see below). Such strings consist
/// solely of consecutive newlines, followed by consecutive spaces
/// * If a string does not satisfy the aforementioned conditions, it is heap-allocated
///
@@ -51,16 +54,16 @@ impl SmolStr {
buf[$idx] = byte
});
}
- s!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21);
+ s!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22);
SmolStr(Repr::Inline {
- len: len as u8,
+ len: unsafe { transmute(len as u8) },
buf,
})
}
/// Constructs inline variant of `SmolStr`.
///
- /// Panics if `text.len() > 22`.
+ /// Panics if `text.len() > 23`.
#[inline]
pub const fn new_inline(text: &str) -> SmolStr {
let mut buf = [0; INLINE_CAP];
@@ -70,7 +73,7 @@ impl SmolStr {
i += 1
}
SmolStr(Repr::Inline {
- len: text.len() as u8,
+ len: unsafe { transmute(text.len() as u8) },
buf,
})
}
@@ -132,7 +135,7 @@ impl SmolStr {
len += size;
}
SmolStr(Repr::Inline {
- len: len as u8,
+ len: unsafe { transmute(len as u8) },
buf,
})
}
@@ -266,7 +269,7 @@ where
len += size;
}
SmolStr(Repr::Inline {
- len: len as u8,
+ len: unsafe { transmute(len as u8) },
buf,
})
}
@@ -289,22 +292,64 @@ impl<'a> iter::FromIterator<&'a str> for SmolStr {
}
}
-impl<T> From<T> for SmolStr
-where
- T: AsRef<str>,
-{
- fn from(text: T) -> Self {
+impl AsRef<str> for SmolStr {
+ #[inline(always)]
+ fn as_ref(&self) -> &str {
+ self.as_str()
+ }
+}
+
+impl From<&str> for SmolStr {
+ #[inline]
+ fn from(s: &str) -> SmolStr {
+ SmolStr::new(s)
+ }
+}
+
+impl From<&mut str> for SmolStr {
+ #[inline]
+ fn from(s: &mut str) -> SmolStr {
+ SmolStr::new(s)
+ }
+}
+
+impl From<&String> for SmolStr {
+ #[inline]
+ fn from(s: &String) -> SmolStr {
+ SmolStr::new(s)
+ }
+}
+
+impl From<String> for SmolStr {
+ #[inline(always)]
+ fn from(text: String) -> Self {
Self::new(text)
}
}
+impl From<Box<str>> for SmolStr {
+ #[inline]
+ fn from(s: Box<str>) -> SmolStr {
+ SmolStr::new(s)
+ }
+}
+
+impl<'a> From<Cow<'a, str>> for SmolStr {
+ #[inline]
+ fn from(s: Cow<'a, str>) -> SmolStr {
+ SmolStr::new(s)
+ }
+}
+
impl From<SmolStr> for String {
+ #[inline(always)]
fn from(text: SmolStr) -> Self {
text.as_str().into()
}
}
impl Borrow<str> for SmolStr {
+ #[inline(always)]
fn borrow(&self) -> &str {
self.as_str()
}
@@ -327,17 +372,52 @@ impl<'a> arbitrary::Arbitrary<'a> for SmolStr {
}
}
-const INLINE_CAP: usize = 22;
+const INLINE_CAP: usize = 23;
const N_NEWLINES: usize = 32;
const N_SPACES: usize = 128;
const WS: &str =
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n ";
+#[derive(Clone, Copy, Debug)]
+#[repr(u8)]
+enum InlineSize {
+ _V0 = 0,
+ _V1,
+ _V2,
+ _V3,
+ _V4,
+ _V5,
+ _V6,
+ _V7,
+ _V8,
+ _V9,
+ _V10,
+ _V11,
+ _V12,
+ _V13,
+ _V14,
+ _V15,
+ _V16,
+ _V17,
+ _V18,
+ _V19,
+ _V20,
+ _V21,
+ _V22,
+ _V23,
+}
+
#[derive(Clone, Debug)]
enum Repr {
Heap(Arc<str>),
- Inline { len: u8, buf: [u8; INLINE_CAP] },
- Substring { newlines: usize, spaces: usize },
+ Inline {
+ len: InlineSize,
+ buf: [u8; INLINE_CAP],
+ },
+ Substring {
+ newlines: usize,
+ spaces: usize,
+ },
}
impl Repr {
@@ -353,7 +433,7 @@ impl Repr {
let mut buf = [0; INLINE_CAP];
buf[..len].copy_from_slice(text.as_bytes());
return Repr::Inline {
- len: len as u8,
+ len: unsafe { transmute(len as u8) },
buf,
};
}
@@ -390,7 +470,7 @@ impl Repr {
fn is_empty(&self) -> bool {
match self {
Repr::Heap(data) => data.is_empty(),
- Repr::Inline { len, .. } => *len == 0,
+ Repr::Inline { len, .. } => *len as u8 == 0,
// A substring isn't created for an empty string.
Repr::Substring { .. } => false,
}