Skip to content

Commit

Permalink
Add missing From impls that String has (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerhawkes authored Oct 28, 2023
1 parent f1e0649 commit ff00c74
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
93 changes: 93 additions & 0 deletions compact_str/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,99 @@ impl<'a> From<&'a CompactString> for Cow<'a, str> {
}
}

#[rustversion::since(1.60)]
#[cfg(target_has_atomic = "ptr")]
impl From<CompactString> for alloc::sync::Arc<str> {
fn from(value: CompactString) -> Self {
Self::from(value.as_str())
}
}

impl From<CompactString> for alloc::rc::Rc<str> {
fn from(value: CompactString) -> Self {
Self::from(value.as_str())
}
}

#[cfg(feature = "std")]
impl From<CompactString> for Box<dyn std::error::Error + Send + Sync> {
fn from(value: CompactString) -> Self {
struct StringError(CompactString);

impl std::error::Error for StringError {
#[allow(deprecated)]
fn description(&self) -> &str {
&self.0
}
}

impl fmt::Display for StringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}

// Purposefully skip printing "StringError(..)"
impl fmt::Debug for StringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}

Box::new(StringError(value))
}
}

#[cfg(feature = "std")]
impl From<CompactString> for Box<dyn std::error::Error> {
fn from(value: CompactString) -> Self {
let err1: Box<dyn std::error::Error + Send + Sync> = From::from(value);
let err2: Box<dyn std::error::Error> = err1;
err2
}
}

impl From<CompactString> for Box<str> {
fn from(value: CompactString) -> Self {
if value.is_heap_allocated() {
value.into_string().into_boxed_str()
} else {
Box::from(value.as_str())
}
}
}

#[cfg(feature = "std")]
impl From<CompactString> for std::ffi::OsString {
fn from(value: CompactString) -> Self {
Self::from(value.into_string())
}
}

#[cfg(feature = "std")]
impl From<CompactString> for std::path::PathBuf {
fn from(value: CompactString) -> Self {
Self::from(std::ffi::OsString::from(value))
}
}

#[cfg(feature = "std")]
impl AsRef<std::path::Path> for CompactString {
fn as_ref(&self) -> &std::path::Path {
std::path::Path::new(self.as_str())
}
}

impl From<CompactString> for alloc::vec::Vec<u8> {
fn from(value: CompactString) -> Self {
if value.is_heap_allocated() {
value.into_string().into_bytes()
} else {
value.as_bytes().to_vec()
}
}
}

impl FromStr for CompactString {
type Err = core::convert::Infallible;
fn from_str(s: &str) -> Result<CompactString, Self::Err> {
Expand Down
93 changes: 93 additions & 0 deletions compact_str/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,99 @@ fn test_into_cow() {
assert_eq!(og, cow);
}

#[test]
fn test_into_arc() {
let short = "short";
let long = "i am a long string that will be allocated on the heap";
let arc = alloc::sync::Arc::<str>::from(CompactString::new(short));
assert_eq!(short, &*arc);
let arc = alloc::sync::Arc::<str>::from(CompactString::new(long));
assert_eq!(long, &*arc);
}

#[test]
fn test_into_rc() {
let short = "short";
let long = "i am a long string that will be allocated on the heap";
let rc = alloc::rc::Rc::<str>::from(CompactString::new(short));
assert_eq!(short, &*rc);
let rc = alloc::rc::Rc::<str>::from(CompactString::new(long));
assert_eq!(long, &*rc);
}

#[test]
fn test_into_error() {
let short = "short";
let long = "i am a long string that will be allocated on the heap";
let short_error_ss =
Box::<dyn std::error::Error + Send + Sync>::from(CompactString::new(short));
assert_eq!(short, format!("{short_error_ss}"));
assert_eq!(format!("{short:?}"), format!("{short_error_ss:?}"));
let long_error_ss = Box::<dyn std::error::Error + Send + Sync>::from(CompactString::new(long));
assert_eq!(long, format!("{long_error_ss}"));
assert_eq!(format!("{long:?}"), format!("{long_error_ss:?}"));

let short_error = Box::<dyn std::error::Error>::from(CompactString::new(short));
assert_eq!(short, format!("{short_error}"));
assert_eq!(format!("{short:?}"), format!("{short_error:?}"));
let long_error = Box::<dyn std::error::Error>::from(CompactString::new(long));
assert_eq!(long, format!("{long_error}"));
assert_eq!(format!("{long:?}"), format!("{long_error:?}"));
}

#[test]
fn test_into_box_str() {
let short = "short";
let long = "i am a long string that will be allocated on the heap";
let s = Box::<str>::from(CompactString::new(short));
assert_eq!(short, &*s);
let l = Box::<str>::from(CompactString::new(long));
assert_eq!(long, &*l);
}

#[test]
fn test_into_os_string() {
let short = "short";
let long = "i am a long string that will be allocated on the heap";
let s = std::ffi::OsString::from(CompactString::new(short));
assert_eq!(s.as_os_str().to_str().unwrap(), short);
let l = std::ffi::OsString::from(CompactString::new(long));
assert_eq!(l.as_os_str().to_str().unwrap(), long);
}

#[test]
fn test_into_path_buf() {
let short = "short";
let long = "i am a long string that will be allocated on the heap";
let s = std::path::PathBuf::from(CompactString::new(short));
assert_eq!(s.as_os_str().to_str().unwrap(), short);
let l = std::path::PathBuf::from(CompactString::new(long));
assert_eq!(l.as_os_str().to_str().unwrap(), long);
}

#[test]
fn test_as_ref_path() {
let short = "short";
let long = "i am a long string that will be allocated on the heap";
let s = CompactString::new(short);
assert_eq!(
AsRef::<std::path::Path>::as_ref(&s).to_str().unwrap(),
short
);
let l = CompactString::new(long);
assert_eq!(AsRef::<std::path::Path>::as_ref(&l).to_str().unwrap(), long);
}

#[test]
fn test_into_vec_u8() {
let short = "short";
let long = "i am a long string that will be allocated on the heap";
let s = Vec::<u8>::from(CompactString::new(short));
assert_eq!(&s, short.as_bytes());
let l = Vec::<u8>::from(CompactString::new(long));
assert_eq!(&l, long.as_bytes());
}

#[test]
fn test_from_string_buffer_inlines_on_push() {
let mut compact = CompactString::from_string_buffer("hello".to_string());
Expand Down

0 comments on commit ff00c74

Please sign in to comment.