diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-08-31 12:59:53 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-08-31 12:59:53 +0200 |
| commit | 3481d8cc81a2b3a14118869c7f0ffe204ff3efc8 (patch) | |
| tree | 907efa2e092366a24e25243854b1a4e088cc04a9 /src/eval/str.rs | |
| parent | ee84bf74083f5b9cc88a2a0a968dc905b1eef22c (diff) | |
More utility functions
- join("a", "b", "c", sep: ", ")
- int("12")
- float("31.4e-1")
- str(10)
- sorted((3, 2, 1))
Diffstat (limited to 'src/eval/str.rs')
| -rw-r--r-- | src/eval/str.rs | 23 |
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()) |
