|
@@ -32,23 +32,23 @@
|
|
|
//! # use stringstore::{StoredString,NamespaceTag};
|
|
|
//! # struct Tag;
|
|
|
//! # impl NamespaceTag for Tag { const PREFIX: &'static str = "tag"; }
|
|
|
-//! assert_eq!(StoredString::<Tag>::new("string"), StoredString::<Tag>::new("string")); // we can
|
|
|
-//! compare StoredStrings
|
|
|
+//! // we can compare StoredStrings directly
|
|
|
+//! assert_eq!(StoredString::<Tag>::new("string"), StoredString::<Tag>::new("string"));
|
|
|
//! ```
|
|
|
//!
|
|
|
//! But this is not:
|
|
|
//! ```compile_fail
|
|
|
-//! use stringstore::StoredString;
|
|
|
+//! # use stringstore::StoredString;
|
|
|
//! let ss1 = StoredString::<Tag1>::new("string");
|
|
|
//! let ss2 = StoredString::<Tag2>::new("string");
|
|
|
//! assert_eq!(ss1.as_str().as_ptr(), ss2.as_str().as_ptr()); // this holds
|
|
|
//! assert_eq!(ss1, ss2); // this is a compilation error
|
|
|
//! ```
|
|
|
+//!
|
|
|
+//! The use of type aliases is highly encouraged.
|
|
|
|
|
|
-use once_cell::sync::Lazy;
|
|
|
-
|
|
|
-static STR_STORE: Lazy<std::sync::Mutex<std::collections::HashSet<&'static str>>> =
|
|
|
- Lazy::new(|| Default::default());
|
|
|
+static STR_STORE: std::sync::LazyLock<std::sync::Mutex<std::collections::HashSet<&'static str>>> =
|
|
|
+ std::sync::LazyLock::new(Default::default);
|
|
|
|
|
|
pub trait NamespaceTag: 'static {
|
|
|
/// Prefix to use when displaying strings with this tag in debug contexts.
|
|
@@ -69,6 +69,18 @@ impl<Tag: 'static> std::ops::Deref for StoredString<Tag> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+impl<Tag: 'static> AsRef<str> for StoredString<Tag> {
|
|
|
+ fn as_ref(&self) -> &str {
|
|
|
+ self.stored
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<Tag: 'static> std::fmt::Display for StoredString<Tag> {
|
|
|
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
+ <str as std::fmt::Display>::fmt(self.stored, f)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
impl<Tag: NamespaceTag> std::fmt::Debug for StoredString<Tag> {
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
f.write_fmt(format_args!("{}:{}", Tag::PREFIX, self.stored))
|
|
@@ -84,7 +96,7 @@ impl<Tag: 'static> std::hash::Hash for StoredString<Tag> {
|
|
|
|
|
|
impl<Tag: 'static> PartialEq for StoredString<Tag> {
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
- self.stored == other.stored
|
|
|
+ self.stored.as_ptr() == other.stored.as_ptr()
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -100,7 +112,7 @@ impl<Tag: 'static> PartialOrd for StoredString<Tag> {
|
|
|
|
|
|
impl<Tag: 'static> Ord for StoredString<Tag> {
|
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
|
- Ord::cmp(self.stored, other.stored)
|
|
|
+ self.stored.as_ptr().cmp(&other.stored.as_ptr())
|
|
|
}
|
|
|
}
|
|
|
|