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
55
56
57
58
59
use {
crate::TextSize,
std::{borrow::Cow, convert::TryInto, rc::Rc, sync::Arc},
};
pub trait TextLen: Copy {
fn text_len(self) -> TextSize;
}
impl TextLen for &'_ str {
#[inline]
fn text_len(self) -> TextSize {
self.len().try_into().unwrap()
}
}
impl TextLen for char {
#[inline]
fn text_len(self) -> TextSize {
(self.len_utf8() as u32).into()
}
}
impl<D> TextLen for &'_ D
where
D: TextLen + Copy,
{
fn text_len(self) -> TextSize {
D::text_len(*self)
}
}
macro_rules! impl_textlen_for_string {
($($ty:ty),+ $(,)?) => {$(
impl TextLen for $ty {
#[inline]
fn text_len(self) -> TextSize {
<&str>::text_len(self)
}
}
)+};
}
impl_textlen_for_string! {
&Box<str>,
&String,
&Cow<'_, str>,
&Cow<'_, String>,
&Arc<str>,
&Arc<String>,
&Rc<str>,
&Rc<String>,
}