Unnamed repository; edit this file 'description' to name the repository.
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
use {
    crate::{TextRange, TextSize},
    std::convert::TryInto,
};

/// Text-like structures that have a text size.
pub trait TextSized: Copy {
    /// The size of this text-alike.
    fn text_size(self) -> TextSize;
}

impl TextSized for &'_ str {
    fn text_size(self) -> TextSize {
        let len = self.len();
        if let Ok(size) = len.try_into() {
            size
        } else if cfg!(debug_assertions) {
            panic!("overflow when converting to TextSize");
        } else {
            TextSize(len as u32)
        }
    }
}

impl TextSized for char {
    fn text_size(self) -> TextSize {
        TextSize(self.len_utf8() as u32)
    }
}

impl TextSized for TextRange {
    fn text_size(self) -> TextSize {
        self.len()
    }
}