Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'lib/text-size/src/traits.rs')
-rw-r--r--lib/text-size/src/traits.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/text-size/src/traits.rs b/lib/text-size/src/traits.rs
new file mode 100644
index 0000000000..52601534d2
--- /dev/null
+++ b/lib/text-size/src/traits.rs
@@ -0,0 +1,29 @@
+use {
+ crate::{TextRange, TextSize},
+ std::convert::TryInto,
+};
+
+/// Text-like structures that have a text size.
+pub trait TextSized {
+ /// The size of this text-alike.
+ fn text_size(&self) -> TextSize;
+}
+
+impl TextSized for str {
+ fn text_size(&self) -> TextSize {
+ let len = self.len();
+ TextSize::new(len)
+ }
+}
+
+impl TextSized for char {
+ fn text_size(&self) -> TextSize {
+ self.len_utf8().try_into().unwrap()
+ }
+}
+
+impl TextSized for TextRange {
+ fn text_size(&self) -> TextSize {
+ self.len()
+ }
+}