Skip to content

Commit 24015a8

Browse files
bors[bot]CAD97
andauthored
Merge #44
44: Scope trait TextLen just for primitives r=matklad a=CAD97 As an alternative to #41 I thought I posted this already.... Co-authored-by: CAD97 <[email protected]>
2 parents 800b4b4 + 408e204 commit 24015a8

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "text-size"
3-
version = "0.99.0"
3+
version = "1.0.0-pre.1"
44
edition = "2018"
55

66
authors = [

src/traits.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,34 @@ use {
33
std::{borrow::Cow, convert::TryInto, rc::Rc, sync::Arc},
44
};
55

6-
/// Text-like structures that have a text size.
7-
pub trait TextLen: Copy {
8-
/// The size of this text-alike.
6+
use priv_in_pub::Sealed;
7+
mod priv_in_pub {
8+
pub trait Sealed {}
9+
}
10+
11+
/// Primitives with a textual length that can be passed to [`TextSize::of`].
12+
pub trait TextLen: Copy + Sealed {
13+
/// The textual length of this primitive.
914
fn text_len(self) -> TextSize;
1015
}
1116

17+
impl Sealed for &'_ str {}
1218
impl TextLen for &'_ str {
1319
#[inline]
1420
fn text_len(self) -> TextSize {
1521
self.len().try_into().unwrap()
1622
}
1723
}
1824

25+
impl Sealed for char {}
1926
impl TextLen for char {
2027
#[inline]
2128
fn text_len(self) -> TextSize {
2229
(self.len_utf8() as u32).into()
2330
}
2431
}
2532

33+
impl<D> Sealed for &'_ D where D: TextLen + Copy {}
2634
impl<D> TextLen for &'_ D
2735
where
2836
D: TextLen + Copy,
@@ -38,6 +46,7 @@ where
3846
// open a PR and we are likely to accept it. Or convince Rust to deref to &str.
3947
macro_rules! impl_textlen_for_string {
4048
($($ty:ty),+ $(,)?) => {$(
49+
impl Sealed for $ty {}
4150
impl TextLen for $ty {
4251
#[inline]
4352
fn text_len(self) -> TextSize {

tests/constructors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use {
66
#[derive(Copy, Clone)]
77
struct BadRope<'a>(&'a [&'a str]);
88

9-
impl TextLen for BadRope<'_> {
9+
impl BadRope<'_> {
1010
fn text_len(self) -> TextSize {
1111
self.0.iter().map(TextSize::of).sum()
1212
}
@@ -29,6 +29,7 @@ fn main() {
2929
&String::new().into_boxed_str(),
3030
&Arc::new(String::new()),
3131
&Cow::Borrowed(""),
32-
BadRope(&[""]),
3332
}
33+
34+
let _ = BadRope(&[""]).text_len();
3435
}

0 commit comments

Comments
 (0)