summaryrefslogtreecommitdiffstats
path: root/vendor/kstring/src/lib.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
commitef24de24a82fe681581cc130f342363c47c0969a (patch)
tree0d494f7e1a38b95c92426f58fe6eaa877303a86c /vendor/kstring/src/lib.rs
parentReleasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz
rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/kstring/src/lib.rs')
-rw-r--r--vendor/kstring/src/lib.rs78
1 files changed, 78 insertions, 0 deletions
diff --git a/vendor/kstring/src/lib.rs b/vendor/kstring/src/lib.rs
new file mode 100644
index 000000000..dc1ffd608
--- /dev/null
+++ b/vendor/kstring/src/lib.rs
@@ -0,0 +1,78 @@
+//! Key String: Optimized for map keys.
+//!
+//! # Examples
+//!
+//! String creation
+//! ```rust
+//! // Explicit
+//! let literal = kstring::KString::from_static("literal");
+//! // Implicit
+//! let literal = kstring::KString::from("literal");
+//!
+//! // Explicit
+//! let inline = kstring::KString::try_inline("stack").unwrap();
+//! let inline = kstring::KString::from_ref("stack");
+//!
+//! let formatted: kstring::KStringCow = format!("Hello {} and {}", literal, inline).into();
+//! ```
+//!
+//! # Background
+//!
+//! Considerations:
+//! - Large maps
+//! - Most keys live and drop without being used in any other way
+//! - Most keys are relatively small (single to double digit bytes)
+//! - Keys are immutable
+//! - Allow zero-cost abstractions between structs and maps (e.g. no allocating
+//! when dealing with struct field names)
+//!
+//! Ramifications:
+//! - Inline small strings rather than going to the heap.
+//! - Preserve `&'static str` across strings ([`KString`]),
+//! references ([`KStringRef`]), and lifetime abstractions ([`KStringCow`]) to avoid
+//! allocating for struct field names.
+//! - Use `Box<str>` rather than `String` to use less memory.
+//!
+//! # Feature Flags
+//!
+#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
+#![cfg_attr(feature = "safe", forbid(unsafe_code))]
+
+#[cfg(not(feature = "std"))]
+compile_error!("`std` feature is required; reserved for future `no_std` support");
+
+mod stack;
+mod string;
+mod string_cow;
+mod string_ref;
+
+pub mod backend;
+
+pub use stack::StackString;
+pub use string::*;
+pub use string_cow::*;
+pub use string_ref::*;
+
+#[cfg(test)]
+mod test {
+ #[test]
+ fn test_size() {
+ println!(
+ "String: {}",
+ std::mem::size_of::<crate::string::StdString>()
+ );
+ println!(
+ "Box<str>: {}",
+ std::mem::size_of::<crate::backend::DefaultStr>()
+ );
+ println!(
+ "Box<Box<str>>: {}",
+ std::mem::size_of::<Box<crate::backend::DefaultStr>>()
+ );
+ println!("str: {}", std::mem::size_of::<&'static str>());
+ println!(
+ "Cow: {}",
+ std::mem::size_of::<std::borrow::Cow<'static, str>>()
+ );
+ }
+}