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.rs | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/lib/text-size/src/traits.rs b/lib/text-size/src/traits.rs index 65f6445a1e..3944030c62 100644 --- a/lib/text-size/src/traits.rs +++ b/lib/text-size/src/traits.rs @@ -1,6 +1,6 @@ use { crate::TextSize, - std::{convert::TryInto, ops::Deref}, + std::{borrow::Cow, convert::TryInto, rc::Rc, sync::Arc}, }; /// Text-like structures that have a text size. @@ -16,19 +16,44 @@ impl LenTextSize for &'_ str { } } -impl<D> LenTextSize for &'_ D -where - D: Deref<Target = str>, -{ +impl LenTextSize for char { #[inline] fn len_text_size(self) -> TextSize { - self.deref().len_text_size() + (self.len_utf8() as u32).into() } } -impl LenTextSize for char { - #[inline] +impl<D> LenTextSize for &'_ D +where + D: LenTextSize + Copy, +{ fn len_text_size(self) -> TextSize { - (self.len_utf8() as u32).into() + D::len_text_size(*self) } } + +// Because we could not find a smart blanket impl to do this automatically and +// cleanly (rust-analyzer/text-size#36), just provide a bunch of manual impls. +// If a type fits in this macro and you need it to impl LenTextSize, just open +// a PR and we are likely to accept it. Or use `TextSize::of::<&str>` for now. +macro_rules! impl_lentextsize_for_string { + ($($ty:ty),+ $(,)?) => {$( + impl LenTextSize for $ty { + #[inline] + fn len_text_size(self) -> TextSize { + <&str>::len_text_size(self) + } + } + )+}; +} + +impl_lentextsize_for_string! { + &Box<str>, + &'_ String, + &Cow<'_, str>, + &Cow<'_, String>, + &Arc<str>, + &Arc<String>, + &Rc<str>, + &Rc<String>, +} |