Unnamed repository; edit this file 'description' to name the repository.
Scope trait TextLen just for primitives
| -rw-r--r-- | lib/text-size/Cargo.toml | 2 | ||||
| -rw-r--r-- | lib/text-size/src/traits.rs | 15 | ||||
| -rw-r--r-- | lib/text-size/tests/constructors.rs | 5 |
3 files changed, 16 insertions, 6 deletions
diff --git a/lib/text-size/Cargo.toml b/lib/text-size/Cargo.toml index 9b36860ea9..9c75a9af04 100644 --- a/lib/text-size/Cargo.toml +++ b/lib/text-size/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "text-size" -version = "0.99.0" +version = "1.0.0-pre.1" edition = "2018" authors = [ diff --git a/lib/text-size/src/traits.rs b/lib/text-size/src/traits.rs index a19396c133..c0adacd92b 100644 --- a/lib/text-size/src/traits.rs +++ b/lib/text-size/src/traits.rs @@ -3,12 +3,18 @@ use { std::{borrow::Cow, convert::TryInto, rc::Rc, sync::Arc}, }; -/// Text-like structures that have a text size. -pub trait TextLen: Copy { - /// The size of this text-alike. +use priv_in_pub::Sealed; +mod priv_in_pub { + pub trait Sealed {} +} + +/// Primitives with a textual length that can be passed to [`TextSize::of`]. +pub trait TextLen: Copy + Sealed { + /// The textual length of this primitive. fn text_len(self) -> TextSize; } +impl Sealed for &'_ str {} impl TextLen for &'_ str { #[inline] fn text_len(self) -> TextSize { @@ -16,6 +22,7 @@ impl TextLen for &'_ str { } } +impl Sealed for char {} impl TextLen for char { #[inline] fn text_len(self) -> TextSize { @@ -23,6 +30,7 @@ impl TextLen for char { } } +impl<D> Sealed for &'_ D where D: TextLen + Copy {} impl<D> TextLen for &'_ D where D: TextLen + Copy, @@ -38,6 +46,7 @@ where // open a PR and we are likely to accept it. Or convince Rust to deref to &str. macro_rules! impl_textlen_for_string { ($($ty:ty),+ $(,)?) => {$( + impl Sealed for $ty {} impl TextLen for $ty { #[inline] fn text_len(self) -> TextSize { diff --git a/lib/text-size/tests/constructors.rs b/lib/text-size/tests/constructors.rs index 829a28e477..9c9d0801ca 100644 --- a/lib/text-size/tests/constructors.rs +++ b/lib/text-size/tests/constructors.rs @@ -6,7 +6,7 @@ use { #[derive(Copy, Clone)] struct BadRope<'a>(&'a [&'a str]); -impl TextLen for BadRope<'_> { +impl BadRope<'_> { fn text_len(self) -> TextSize { self.0.iter().map(TextSize::of).sum() } @@ -29,6 +29,7 @@ fn main() { &String::new().into_boxed_str(), &Arc::new(String::new()), &Cow::Borrowed(""), - BadRope(&[""]), } + + let _ = BadRope(&[""]).text_len(); } |