From 37a7afddfaffd44cb9bc013c9506599267e08983 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 3 Nov 2022 11:44:53 +0100 Subject: Split crates --- src/util/eco.rs | 13 +++++++++---- src/util/mod.rs | 12 +++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'src/util') diff --git a/src/util/eco.rs b/src/util/eco.rs index 645880c4..800760e2 100644 --- a/src/util/eco.rs +++ b/src/util/eco.rs @@ -8,7 +8,9 @@ use std::sync::Arc; use super::ArcExt; /// Create a new [`EcoString`] from a format string. -macro_rules! format_eco { +#[macro_export] +#[doc(hidden)] +macro_rules! __format_eco { ($($tts:tt)*) => {{ use std::fmt::Write; let mut s = $crate::util::EcoString::new(); @@ -17,6 +19,9 @@ macro_rules! format_eco { }}; } +#[doc(inline)] +pub use crate::__format_eco as format_eco; + /// An economical string with inline storage and clone-on-write semantics. #[derive(Clone)] pub struct EcoString(Repr); @@ -55,7 +60,7 @@ impl EcoString { } /// Create an instance from an existing string-like type. - pub fn from_str(s: S) -> Self + pub fn from_str_like(s: S) -> Self where S: AsRef + Into, { @@ -324,13 +329,13 @@ impl From for EcoString { impl From<&str> for EcoString { fn from(s: &str) -> Self { - Self::from_str(s) + Self::from_str_like(s) } } impl From for EcoString { fn from(s: String) -> Self { - Self::from_str(s) + Self::from_str_like(s) } } diff --git a/src/util/mod.rs b/src/util/mod.rs index bc7aa250..df3c446e 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -3,7 +3,7 @@ pub mod fat; pub use buffer::Buffer; -pub use eco::EcoString; +pub use eco::{format_eco, EcoString}; #[macro_use] mod eco; @@ -11,9 +11,12 @@ mod buffer; use std::any::TypeId; use std::fmt::{self, Debug, Formatter}; +use std::hash::Hash; use std::path::{Component, Path, PathBuf}; use std::sync::Arc; +use siphasher::sip128::{Hasher128, SipHasher}; + /// Turn a closure into a struct implementing [`Debug`]. pub fn debug(f: F) -> impl Debug where @@ -33,6 +36,13 @@ where Wrapper(f) } +/// Calculate a 128-bit siphash of a value. +pub fn hash128(value: &T) -> u128 { + let mut state = SipHasher::new(); + value.hash(&mut state); + state.finish128().as_u128() +} + /// Extra methods for [`str`]. pub trait StrExt { /// The number of code units this string would use if it was encoded in -- cgit v1.2.3