1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::{fmt::Display, ops::Deref};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use smol_str::SmolStr;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct ShortString(SmolStr);
impl ShortString {
pub fn new<T: Into<String> + AsRef<str>>(text: T) -> Self {
ShortString(SmolStr::from(text))
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl Display for ShortString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl Deref for ShortString {
type Target = str;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl<T: Into<String> + AsRef<str>> From<T> for ShortString {
fn from(value: T) -> Self {
ShortString(SmolStr::from(value))
}
}