Skip to content

Commit 6d75655

Browse files
committed
Make TextRange constructors more boring
Remove `fn TextRange(` as that's slightly unusual and surprising, which will add up to a lot of confusion over the long run. Instead add: * `new` as the biased, canonical way to create range from bounds * `from_len` as an alternative ctor from starting position and len * `empty` for empty ranges at a given offset * `up_to` for ranges at zero offset with given length * `default` for an empty range at zero
1 parent 79d87c4 commit 6d75655

File tree

6 files changed

+41
-41
lines changed

6 files changed

+41
-41
lines changed

src/range.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ use {
1010
///
1111
/// # Translation from `text_unit`
1212
///
13-
/// - `TextRange::from_to(from, to)` ⟹ `TextRange(from, to)`
14-
/// - `TextRange::offset_len(offset, size)` ⟹ `TextRange::up_to(size) + offset`
13+
/// - `TextRange::from_to(from, to)` ⟹ `TextRange::new(from, to)`
14+
/// - `TextRange::offset_len(offset, size)` ⟹ `TextRange::from_len(offset, size)`
1515
/// - `range.start()` ⟹ `range.start()`
1616
/// - `range.end()` ⟹ `range.end()`
1717
/// - `range.len()` ⟹ `range.len()`
1818
/// - `range.is_empty()` ⟹ `range.is_empty()`
1919
/// - `a.is_subrange(b)` ⟹ `b.contains_range(a)`
20-
/// - `a.intersection(b)` ⟹ `TextRange::intersection(a, b)`
21-
/// - `a.extend_to(b)` ⟹ `TextRange::covering(a, b)`
20+
/// - `a.intersection(b)` ⟹ `a.intersect(b)`
21+
/// - `a.extend_to(b)` ⟹ `a.cover(b)`
2222
/// - `range.contains(offset)` ⟹ `range.contains(point)`
2323
/// - `range.contains_inclusive(offset)` ⟹ `range.contains_inclusive(point)`
24-
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
24+
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
2525
pub struct TextRange {
2626
// Invariant: start <= end
2727
start: TextSize,
@@ -34,19 +34,24 @@ impl fmt::Debug for TextRange {
3434
}
3535
}
3636

37-
/// Creates a new `TextRange` with the given `start` and `end` (`start..end`).
38-
///
39-
/// # Panics
40-
///
41-
/// Panics if `end < start`.
42-
#[allow(non_snake_case)]
43-
#[inline]
44-
pub fn TextRange(start: TextSize, end: TextSize) -> TextRange {
45-
assert!(start <= end);
46-
TextRange { start, end }
47-
}
48-
4937
impl TextRange {
38+
/// Creates a new `TextRange` with the given `start` and `end` (`start..end`).
39+
///
40+
/// # Panics
41+
///
42+
/// Panics if `end < start`.
43+
#[inline]
44+
pub fn new(start: TextSize, end: TextSize) -> TextRange {
45+
assert!(start <= end);
46+
TextRange { start, end }
47+
}
48+
49+
/// Create a new `TextRange` with the given `start` and `len` (`start..start + len`).
50+
#[inline]
51+
pub fn from_len(start: TextSize, len: TextSize) -> TextRange {
52+
TextRange::new(start, start + len)
53+
}
54+
5055
/// Create a zero-length range at the specified offset (`offset..offset`).
5156
#[inline]
5257
pub const fn empty(offset: TextSize) -> TextRange {
@@ -59,10 +64,8 @@ impl TextRange {
5964
/// Create a range up to the given end (`..end`).
6065
#[inline]
6166
pub const fn up_to(end: TextSize) -> TextRange {
62-
TextRange {
63-
start: TextSize::zero(),
64-
end,
65-
}
67+
let start = TextSize::zero();
68+
TextRange { start, end }
6669
}
6770
}
6871

@@ -84,7 +87,9 @@ impl TextRange {
8487
#[inline]
8588
pub const fn len(self) -> TextSize {
8689
// HACK for const fn: math on primitives only
87-
TextSize(self.end().raw - self.start().raw)
90+
TextSize {
91+
raw: self.end().raw - self.start().raw,
92+
}
8893
}
8994

9095
/// Check if this range is empty.
@@ -124,14 +129,14 @@ impl TextRange {
124129
if end < start {
125130
return None;
126131
}
127-
Some(TextRange(start, end))
132+
Some(TextRange::new(start, end))
128133
}
129134

130135
/// Extends the range to cover `other` as well.
131136
pub fn cover(self, other: TextRange) -> TextRange {
132137
let start = cmp::min(self.start(), other.start());
133138
let end = cmp::max(self.end(), other.end());
134-
TextRange(start, end)
139+
TextRange::new(start, end)
135140
}
136141

137142
/// Extends the range to cover `other` offsets as well.

src/serde_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<'de> Deserialize<'de> for TextSize {
1717
where
1818
D: Deserializer<'de>,
1919
{
20-
Deserialize::deserialize(deserializer).map(TextSize)
20+
u32::deserialize(deserializer).map(TextSize::from)
2121
}
2222
}
2323

@@ -43,6 +43,6 @@ impl<'de> Deserialize<'de> for TextRange {
4343
start, end
4444
)));
4545
}
46-
Ok(TextRange(start, end))
46+
Ok(TextRange::new(start, end))
4747
}
4848
}

src/size.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ pub struct TextSize {
3333
pub(crate) raw: u32,
3434
}
3535

36-
#[allow(non_snake_case)]
37-
pub(crate) const fn TextSize(raw: u32) -> TextSize {
38-
TextSize { raw }
39-
}
40-
4136
impl fmt::Debug for TextSize {
4237
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4338
write!(f, "{}", self.raw)
@@ -57,35 +52,35 @@ impl TextSize {
5752
/// but is more explicit on intent.
5853
#[inline]
5954
pub const fn zero() -> TextSize {
60-
TextSize(0)
55+
TextSize { raw: 0 }
6156
}
6257
}
6358

6459
/// Methods to act like a primitive integer type, where reasonably applicable.
6560
// Last updated for parity with Rust 1.42.0.
6661
impl TextSize {
6762
/// The smallest representable text size. (`u32::MIN`)
68-
pub const MIN: TextSize = TextSize(u32::MIN);
63+
pub const MIN: TextSize = TextSize { raw: u32::MIN };
6964
/// The largest representable text size. (`u32::MAX`)
70-
pub const MAX: TextSize = TextSize(u32::MAX);
65+
pub const MAX: TextSize = TextSize { raw: u32::MAX };
7166

7267
/// Checked addition. Returns `None` if overflow occurred.
7368
#[inline]
7469
pub fn checked_add(self, rhs: TextSize) -> Option<TextSize> {
75-
self.raw.checked_add(rhs.raw).map(TextSize)
70+
self.raw.checked_add(rhs.raw).map(|raw| TextSize { raw })
7671
}
7772

7873
/// Checked subtraction. Returns `None` if overflow occurred.
7974
#[inline]
8075
pub fn checked_sub(self, rhs: TextSize) -> Option<TextSize> {
81-
self.raw.checked_sub(rhs.raw).map(TextSize)
76+
self.raw.checked_sub(rhs.raw).map(|raw| TextSize { raw })
8277
}
8378
}
8479

8580
impl From<u32> for TextSize {
8681
#[inline]
8782
fn from(raw: u32) -> Self {
88-
TextSize(raw)
83+
TextSize { raw }
8984
}
9085
}
9186

@@ -117,7 +112,7 @@ macro_rules! ops {
117112
type Output = TextSize;
118113
#[inline]
119114
fn $f(self, other: TextSize) -> TextSize {
120-
TextSize(self.raw $op other.raw)
115+
TextSize { raw: self.raw $op other.raw }
121116
}
122117
}
123118
impl $Op<&TextSize> for TextSize {

src/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ impl TextSized for &'_ str {
1818
impl TextSized for char {
1919
#[inline]
2020
fn text_size(self) -> TextSize {
21-
TextSize(self.len_utf8() as u32)
21+
(self.len_utf8() as u32).into()
2222
}
2323
}

tests/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn size(x: u32) -> TextSize {
55
}
66

77
fn range(x: ops::Range<u32>) -> TextRange {
8-
TextRange(x.start.into(), x.end.into())
8+
TextRange::new(x.start.into(), x.end.into())
99
}
1010

1111
#[test]

tests/serde.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn size(x: u32) -> TextSize {
55
}
66

77
fn range(x: ops::Range<u32>) -> TextRange {
8-
TextRange(x.start.into(), x.end.into())
8+
TextRange::new(x.start.into(), x.end.into())
99
}
1010

1111
#[test]

0 commit comments

Comments
 (0)