1 | use {crate::TextSize, std::convert::TryInto};
|
2 |
|
3 | use priv_in_pub::Sealed;
|
4 | mod priv_in_pub {
|
5 | pub trait Sealed {}
|
6 | }
|
7 |
|
8 | /// Primitives with a textual length that can be passed to [`TextSize::of`].
|
9 | pub trait TextLen: Copy + Sealed {
|
10 | /// The textual length of this primitive.
|
11 | fn text_len(self) -> TextSize;
|
12 | }
|
13 |
|
14 | impl Sealed for &'_ str {}
|
15 | impl TextLen for &'_ str {
|
16 | #[inline ]
|
17 | fn text_len(self) -> TextSize {
|
18 | self.len().try_into().unwrap()
|
19 | }
|
20 | }
|
21 |
|
22 | impl Sealed for &'_ String {}
|
23 | impl TextLen for &'_ String {
|
24 | #[inline ]
|
25 | fn text_len(self) -> TextSize {
|
26 | self.as_str().text_len()
|
27 | }
|
28 | }
|
29 |
|
30 | impl Sealed for char {}
|
31 | impl TextLen for char {
|
32 | #[inline ]
|
33 | fn text_len(self) -> TextSize {
|
34 | (self.len_utf8() as u32).into()
|
35 | }
|
36 | }
|
37 | |