Skip to content

Commit be75985

Browse files
bors[bot]CAD97
andauthored
Merge #40
40: Rename LenTextSize => TextLen r=matklad a=CAD97 This way, it's a reasonable name to use. Closes #38 I still think `TextSize::of` should be the primarily recommended construction interface (thus keeping the `TextLen` macro impls for standard types), but calling `.text_len()` is a friendlier alternative for when deref coercion is desired to kick in. Co-authored-by: CAD97 <[email protected]>
2 parents d015768 + 9c3a298 commit be75985

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod traits;
1212
#[cfg(feature = "serde")]
1313
mod serde_impls;
1414

15-
pub use crate::{range::TextRange, size::TextSize, traits::LenTextSize};
15+
pub use crate::{range::TextRange, size::TextSize, traits::TextLen};
1616

1717
#[cfg(target_pointer_width = "16")]
1818
compile_error!("text-size assumes usize >= u32 and does not work on 16-bit targets");

src/size.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use {
2-
crate::LenTextSize,
2+
crate::TextLen,
33
std::{
44
convert::TryFrom,
55
fmt, iter,
@@ -44,7 +44,7 @@ impl TextSize {
4444
///
4545
/// Accepts `char`, `&str`, and references to any custom string-like type
4646
/// that dereferences to `str`. Types that don't dereference to `str` but
47-
/// want to be usable in this constructor can implement [`LenTextSize`].
47+
/// want to be usable in this constructor can implement [`TextLen`].
4848
///
4949
/// # Examples
5050
///
@@ -57,8 +57,8 @@ impl TextSize {
5757
/// assert_eq!(str_size, TextSize::from(13));
5858
/// ```
5959
#[inline]
60-
pub fn of<T: LenTextSize>(text: T) -> TextSize {
61-
text.len_text_size()
60+
pub fn of<T: TextLen>(text: T) -> TextSize {
61+
text.text_len()
6262
}
6363

6464
/// A size of zero.

src/traits.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,52 @@ use {
44
};
55

66
/// Text-like structures that have a text size.
7-
pub trait LenTextSize: Copy {
7+
pub trait TextLen: Copy {
88
/// The size of this text-alike.
9-
fn len_text_size(self) -> TextSize;
9+
fn text_len(self) -> TextSize;
1010
}
1111

12-
impl LenTextSize for &'_ str {
12+
impl TextLen for &'_ str {
1313
#[inline]
14-
fn len_text_size(self) -> TextSize {
14+
fn text_len(self) -> TextSize {
1515
self.len().try_into().unwrap()
1616
}
1717
}
1818

19-
impl LenTextSize for char {
19+
impl TextLen for char {
2020
#[inline]
21-
fn len_text_size(self) -> TextSize {
21+
fn text_len(self) -> TextSize {
2222
(self.len_utf8() as u32).into()
2323
}
2424
}
2525

26-
impl<D> LenTextSize for &'_ D
26+
impl<D> TextLen for &'_ D
2727
where
28-
D: LenTextSize + Copy,
28+
D: TextLen + Copy,
2929
{
30-
fn len_text_size(self) -> TextSize {
31-
D::len_text_size(*self)
30+
fn text_len(self) -> TextSize {
31+
D::text_len(*self)
3232
}
3333
}
3434

3535
// Because we could not find a smart blanket impl to do this automatically and
3636
// cleanly (rust-analyzer/text-size#36), just provide a bunch of manual impls.
37-
// If a type fits in this macro and you need it to impl LenTextSize, just open
38-
// a PR and we are likely to accept it. Or use `TextSize::of::<&str>` for now.
39-
macro_rules! impl_lentextsize_for_string {
37+
// If a standard type fits in this macro and you need it to impl TextLen, just
38+
// open a PR and we are likely to accept it. Or convince Rust to deref to &str.
39+
macro_rules! impl_textlen_for_string {
4040
($($ty:ty),+ $(,)?) => {$(
41-
impl LenTextSize for $ty {
41+
impl TextLen for $ty {
4242
#[inline]
43-
fn len_text_size(self) -> TextSize {
44-
<&str>::len_text_size(self)
43+
fn text_len(self) -> TextSize {
44+
<&str>::text_len(self)
4545
}
4646
}
4747
)+};
4848
}
4949

50-
impl_lentextsize_for_string! {
50+
impl_textlen_for_string! {
5151
&Box<str>,
52-
&'_ String,
52+
&String,
5353
&Cow<'_, str>,
5454
&Cow<'_, String>,
5555
&Arc<str>,

tests/constructors.rs

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

9-
impl LenTextSize for BadRope<'_> {
10-
fn len_text_size(self) -> TextSize {
9+
impl TextLen for BadRope<'_> {
10+
fn text_len(self) -> TextSize {
1111
self.0.iter().map(TextSize::of).sum()
1212
}
1313
}

0 commit comments

Comments
 (0)