diff options
Diffstat (limited to 'vendor/kstring/src/backend.rs')
-rw-r--r-- | vendor/kstring/src/backend.rs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/vendor/kstring/src/backend.rs b/vendor/kstring/src/backend.rs new file mode 100644 index 000000000..3827082f1 --- /dev/null +++ b/vendor/kstring/src/backend.rs @@ -0,0 +1,97 @@ +#[cfg(feature = "arc")] +pub(crate) type DefaultStr = crate::backend::ArcStr; +#[cfg(not(feature = "arc"))] +pub(crate) type DefaultStr = crate::backend::BoxedStr; + +/// Fast allocations, O(n) clones +pub type BoxedStr = Box<str>; +static_assertions::assert_eq_size!(DefaultStr, BoxedStr); + +/// Cross-thread, O(1) clones +pub type ArcStr = std::sync::Arc<str>; +static_assertions::assert_eq_size!(DefaultStr, ArcStr); + +/// O(1) clones +pub type RcStr = std::rc::Rc<str>; +static_assertions::assert_eq_size!(DefaultStr, RcStr); + +/// Abstract over different type of heap-allocated strings +pub trait HeapStr: std::fmt::Debug + Clone + private::Sealed { + fn from_str(other: &str) -> Self; + fn from_string(other: String) -> Self; + fn from_boxed_str(other: BoxedStr) -> Self; + fn as_str(&self) -> &str; +} + +impl HeapStr for BoxedStr { + #[inline] + fn from_str(other: &str) -> Self { + other.into() + } + + #[inline] + fn from_string(other: String) -> Self { + other.into_boxed_str() + } + + #[inline] + fn from_boxed_str(other: BoxedStr) -> Self { + other + } + + #[inline] + fn as_str(&self) -> &str { + self + } +} + +impl HeapStr for ArcStr { + #[inline] + fn from_str(other: &str) -> Self { + other.into() + } + + #[inline] + fn from_string(other: String) -> Self { + other.into_boxed_str().into() + } + + #[inline] + fn from_boxed_str(other: BoxedStr) -> Self { + other.into() + } + + #[inline] + fn as_str(&self) -> &str { + self + } +} + +impl HeapStr for RcStr { + #[inline] + fn from_str(other: &str) -> Self { + other.into() + } + + #[inline] + fn from_string(other: String) -> Self { + other.into_boxed_str().into() + } + + #[inline] + fn from_boxed_str(other: BoxedStr) -> Self { + other.into() + } + + #[inline] + fn as_str(&self) -> &str { + self + } +} + +pub(crate) mod private { + pub trait Sealed {} + impl Sealed for super::BoxedStr {} + impl Sealed for super::ArcStr {} + impl Sealed for super::RcStr {} +} |