From 68cc1a026b3e849b5636de9c20b6e240478b4bb6 Mon Sep 17 00:00:00 2001 From: Kornel Date: Fri, 2 Feb 2024 18:57:58 +0000 Subject: [PATCH] impl AsRef, PartialEq --- src/lib.rs | 218 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 215 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9fb84a6..fb55ac7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -139,9 +139,14 @@ //! use it on 32-bit, please make sure to run Miri and open and issue if you //! find any problems. use parking_lot::Mutex; +use std::borrow::Cow; +use std::ffi::OsStr; use std::fmt; use std::mem::size_of; +use std::path::Path; +use std::rc::Rc; use std::str::FromStr; +use std::sync::Arc; mod stringcache; pub use stringcache::*; @@ -332,23 +337,141 @@ impl Ustr { unsafe impl Send for Ustr {} unsafe impl Sync for Ustr {} +impl PartialEq for Ustr { + fn eq(&self, other: &str) -> bool { + self.as_str() == other + } +} + +impl PartialEq for str { + fn eq(&self, u: &Ustr) -> bool { + self == u.as_str() + } +} + impl PartialEq<&str> for Ustr { fn eq(&self, other: &&str) -> bool { self.as_str() == *other } } +impl PartialEq for &str { + fn eq(&self, u: &Ustr) -> bool { + *self == u.as_str() + } +} + + +impl PartialEq<&&str> for Ustr { + fn eq(&self, other: &&&str) -> bool { + self.as_str() == **other + } +} + +impl PartialEq for &&str { + fn eq(&self, u: &Ustr) -> bool { + **self == u.as_str() + } +} + impl PartialEq for Ustr { fn eq(&self, other: &String) -> bool { self.as_str() == other } } +impl PartialEq for String { + fn eq(&self, u: &Ustr) -> bool { + self == u.as_str() + } +} + +impl PartialEq<&String> for Ustr { + fn eq(&self, other: &&String) -> bool { + self.as_str() == *other + } +} + +impl PartialEq for &String { + fn eq(&self, u: &Ustr) -> bool { + *self == u.as_str() + } +} + +impl PartialEq> for Ustr { + fn eq(&self, other: &Box) -> bool { + self.as_str() == &**other + } +} + +impl PartialEq for Box { + fn eq(&self, u: &Ustr) -> bool { + &**self == u.as_str() + } +} + +impl PartialEq for &Box { + fn eq(&self, u: &Ustr) -> bool { + &***self == u.as_str() + } +} + +impl PartialEq> for Ustr { + fn eq(&self, other: &Cow<'_, str>) -> bool { + self.as_str() == &*other + } +} + +impl PartialEq for Cow<'_, str> { + fn eq(&self, u: &Ustr) -> bool { + &*self == u.as_str() + } +} + +impl PartialEq<&Cow<'_, str>> for Ustr { + fn eq(&self, other: &&Cow<'_, str>) -> bool { + self.as_str() == &**other + } +} + +impl PartialEq for &Cow<'_, str> { + fn eq(&self, u: &Ustr) -> bool { + &**self == u.as_str() + } +} + +impl PartialEq for Path { + fn eq(&self, u: &Ustr) -> bool { + self == Path::new(u) + } +} + +impl PartialEq for &Path { + fn eq(&self, u: &Ustr) -> bool { + *self == Path::new(u) + } +} + +impl PartialEq for OsStr { + fn eq(&self, u: &Ustr) -> bool { + self == OsStr::new(u) + } +} + +impl PartialEq for &OsStr { + fn eq(&self, u: &Ustr) -> bool { + *self == OsStr::new(u) + } +} + impl Eq for Ustr {} -impl AsRef for Ustr { - fn as_ref(&self) -> &str { - self.as_str() +impl AsRef for Ustr +where + str: AsRef, +{ + fn as_ref(&self) -> &T { + self.as_str().as_ref() } } @@ -373,12 +496,72 @@ impl From for &'static str { } } +impl From for String { + fn from(u: Ustr) -> Self { + String::from(u.as_str()) + } +} + +impl From for Box { + fn from(u: Ustr) -> Self { + Box::from(u.as_str()) + } +} + +impl From for Rc { + fn from(u: Ustr) -> Self { + Rc::from(u.as_str()) + } +} + +impl From for Arc { + fn from(u: Ustr) -> Self { + Arc::from(u.as_str()) + } +} + +impl From for Cow<'static, str> { + fn from(u: Ustr) -> Self { + Cow::Borrowed(u.as_str()) + } +} + impl From for Ustr { fn from(s: String) -> Ustr { Ustr::from(&s) } } +impl From<&String> for Ustr { + fn from(s: &String) -> Ustr { + Ustr::from(&**s) + } +} + +impl From> for Ustr { + fn from(s: Box) -> Ustr { + Ustr::from(&*s) + } +} + +impl From> for Ustr { + fn from(s: Rc) -> Ustr { + Ustr::from(&*s) + } +} + +impl From> for Ustr { + fn from(s: Arc) -> Ustr { + Ustr::from(&*s) + } +} + +impl From> for Ustr { + fn from(s: Cow<'_, str>) -> Ustr { + Ustr::from(&*s) + } +} + impl Default for Ustr { fn default() -> Self { Ustr::from("") @@ -581,6 +764,8 @@ pub struct Bins(pub(crate) [Mutex; NUM_BINS]); #[cfg(test)] mod tests { use lazy_static::lazy_static; + use std::ffi::OsStr; + use std::path::Path; use std::sync::Mutex; lazy_static! { @@ -865,6 +1050,33 @@ mod tests { let s2 = existing_ustr("hello world!"); assert_eq!(Some(s1), s2); } + + #[test] + fn as_refs() { + let _t = TEST_LOCK.lock(); + + let u = super::ustr("test"); + + let s: String = u.to_owned(); + assert_eq!(u, s); + assert_eq!(s, u); + + let p: &Path = u.as_ref(); + assert_eq!(p, u); + + let _: &[u8] = u.as_ref(); + + let o: &OsStr = u.as_ref(); + assert_eq!(p, o); + assert_eq!(o, p); + + let cow = std::borrow::Cow::from(u); + assert_eq!(cow, u); + assert_eq!(u, cow); + + let boxed: Box = u.into(); + assert_eq!(boxed, u); + } } lazy_static::lazy_static! {