text_size/
traits.rs

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