Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,21 @@ impl FromStr for EcoString {
}
}

/// A trait for converting a value to an [`EcoString`].
///
/// This trait is automatically implemented for any type which implements the
/// [`Display`] trait.
pub trait ToEcoString {
/// Converts the given value to an [`EcoString`].
fn to_eco_string(&self) -> EcoString;
}

impl<T: Display + ?Sized> ToEcoString for T {
fn to_eco_string(&self) -> EcoString {
eco_format!("{self}")
}
}

#[cold]
const fn exceeded_inline_capacity() -> ! {
panic!("exceeded inline capacity");
Expand Down
7 changes: 7 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::fmt::Write;
use std::mem;
use std::sync::atomic::{AtomicUsize, Ordering::*};

use ecow::string::ToEcoString;
use ecow::{eco_vec, EcoString, EcoVec};

const ALPH: &str = "abcdefghijklmnopqrstuvwxyz";
Expand Down Expand Up @@ -625,3 +626,9 @@ fn test_str_complex() {
assert_eq!(hash_map.get("foo").unwrap(), &["bar".into(), "foo".into(), "foo".into()]);
assert_eq!(hash_map.get("bar").unwrap(), &["bar".into()]);
}

#[test]
fn test_str_to_eco_string() {
let num = 123456789;
assert_eq!(num.to_eco_string(), "123456789");
}
Loading