summaryrefslogtreecommitdiff
path: root/src/eval/str.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/eval/str.rs')
-rw-r--r--src/eval/str.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/eval/str.rs b/src/eval/str.rs
index a358cd9f..099a4363 100644
--- a/src/eval/str.rs
+++ b/src/eval/str.rs
@@ -1,3 +1,4 @@
+use std::borrow::Borrow;
use std::convert::TryFrom;
use std::fmt::{self, Debug, Display, Formatter, Write};
use std::ops::{Add, AddAssign, Deref};
@@ -5,6 +6,16 @@ use std::ops::{Add, AddAssign, Deref};
use crate::diag::StrResult;
use crate::util::EcoString;
+/// Create a new [`Str`] from a format string.
+macro_rules! format_str {
+ ($($tts:tt)*) => {{
+ use std::fmt::Write;
+ let mut s = $crate::util::EcoString::new();
+ write!(s, $($tts)*).unwrap();
+ $crate::eval::Str::from(s)
+ }};
+}
+
/// A string value with inline storage and clone-on-write semantics.
#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Str(EcoString);
@@ -92,6 +103,18 @@ impl AddAssign for Str {
}
}
+impl AsRef<str> for Str {
+ fn as_ref(&self) -> &str {
+ self
+ }
+}
+
+impl Borrow<str> for Str {
+ fn borrow(&self) -> &str {
+ self
+ }
+}
+
impl From<char> for Str {
fn from(c: char) -> Self {
Self(c.into())