Skip to content

Commit 7a1373e

Browse files
committed
Scale back the exposed API surface
1 parent 5ee8fea commit 7a1373e

File tree

7 files changed

+133
-335
lines changed

7 files changed

+133
-335
lines changed

Cargo.toml

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

66
authors = [
@@ -14,7 +14,6 @@ documentation = "https://docs.rs/text_unit"
1414

1515
[dependencies]
1616
serde = { version = "1.0", optional = true, default_features = false }
17-
deepsize = { version = "0.1", optional = true, default_features = false }
1817

1918
[dev-dependencies]
2019
serde_test = "1.0"

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,3 @@ mod traits;
1111
mod serde_impls;
1212

1313
pub use crate::{range::TextRange, size::TextSize, traits::TextSized};
14-
15-
#[cfg(feature = "deepsize")]
16-
deepsize::known_deep_size!(0, TextSize, TextRange);

src/range.rs

Lines changed: 25 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
use {
2-
crate::{TextSize, TextSized},
2+
crate::TextSize,
33
std::{
44
cmp,
55
convert::{TryFrom, TryInto},
66
fmt,
7-
num::TryFromIntError,
8-
ops::{
9-
Add, AddAssign, Bound, Index, IndexMut, Range, RangeBounds, RangeInclusive, RangeTo,
10-
RangeToInclusive, Sub, SubAssign,
11-
},
7+
ops::{Bound, Index, IndexMut, Range, RangeBounds},
128
},
139
};
1410

@@ -19,17 +15,17 @@ use {
1915
///
2016
/// # Translation from `text_unit`
2117
///
22-
/// - `TextRange::from_to(from, to)` ⟹ `TextRange::from(from..to)`
23-
/// - `TextRange::offset_len(offset, size)` ⟹ `TextRange::at(offset).with_len(size)`
24-
/// - `range.start()` ⟹ `range.start()`
25-
/// - `range.end()` ⟹ `range.end()`
26-
/// - `range.len()` ⟹ `range.len()`<sup>†</sup>
27-
/// - `range.is_empty()` ⟹ `range.is_empty()`
28-
/// - `a.is_subrange(b)` ⟹ `b.contains(a)`
29-
/// - `a.intersection(b)` ⟹ `TextRange::intersection(a, b)`
30-
/// - `a.extend_to(b)` ⟹ `TextRange::covering(a, b)`
31-
/// - `range.contains(offset)` ⟹ `range.contains_point(point)`
32-
/// - `range.contains_inclusive(offset)` ⟹ `range.contains_point_inclusive(point)`
18+
/// - `TextRange::from_to(from, to)` ⟹ `TextRange::from(from..to)`
19+
/// - `TextRange::offset_len(offset, size)` ⟹ `TextRange::from(offset..offset + size)`
20+
/// - `range.start()` ⟹ `range.start()`
21+
/// - `range.end()` ⟹ `range.end()`
22+
/// - `range.len()` ⟹ `range.len()`<sup>†</sup>
23+
/// - `range.is_empty()` ⟹ `range.is_empty()`
24+
/// - `a.is_subrange(b)` ⟹ `b.contains(a)`
25+
/// - `a.intersection(b)` ⟹ `TextRange::intersection(a, b)`
26+
/// - `a.extend_to(b)` ⟹ `TextRange::covering(a, b)`
27+
/// - `range.contains(offset)` ⟹ `range.contains_exclusive(point)`
28+
/// - `range.contains_inclusive(offset)` ⟹ `range.contains_inclusive(point)`
3329
///
3430
/// † See the note on [`TextRange::len`] for differing behavior for incorrect reverse ranges.
3531
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
@@ -44,12 +40,6 @@ pub(crate) const fn TextRange(start: TextSize, end: TextSize) -> TextRange {
4440
}
4541

4642
impl fmt::Debug for TextRange {
47-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48-
fmt::Display::fmt(self, f)
49-
}
50-
}
51-
52-
impl fmt::Display for TextRange {
5343
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5444
write!(f, "[{}..{})", self.start(), self.end())
5545
}
@@ -91,27 +81,6 @@ impl TextRange {
9181

9282
/// Manipulation methods.
9383
impl TextRange {
94-
/// A range covering the text size of some text-like object.
95-
pub fn of(size: impl TextSized) -> TextRange {
96-
TextRange(0.into(), size.text_size())
97-
}
98-
99-
/// An empty range at some text size offset.
100-
pub fn at(size: impl Into<TextSize>) -> TextRange {
101-
let size = size.into();
102-
TextRange(size, size)
103-
}
104-
105-
/// Set the length without changing the starting offset.
106-
pub fn with_len(self, len: impl Into<TextSize>) -> TextRange {
107-
TextRange(self.start(), self.start() + len.into())
108-
}
109-
110-
/// Set the starting offset without changing the length.
111-
pub fn with_offset(self, offset: impl Into<TextSize>) -> TextRange {
112-
TextRange::at(offset).with_len(self.len())
113-
}
114-
11584
/// Check if this range completely contains another range.
11685
pub fn contains(self, other: TextRange) -> bool {
11786
self.start() <= other.start() && other.end() <= self.end()
@@ -135,48 +104,35 @@ impl TextRange {
135104
/// Check if this range contains a point.
136105
///
137106
/// The end index is considered excluded.
138-
pub fn contains_point(self, point: impl Into<TextSize>) -> bool {
107+
pub fn contains_exclusive(self, point: impl Into<TextSize>) -> bool {
139108
let point = point.into();
140109
self.start() <= point && point < self.end()
141110
}
142111

143112
/// Check if this range contains a point.
144113
///
145114
/// The end index is considered included.
146-
pub fn contains_point_inclusive(self, point: impl Into<TextSize>) -> bool {
115+
pub fn contains_inclusive(self, point: impl Into<TextSize>) -> bool {
147116
let point = point.into();
148117
self.start() <= point && point <= self.end()
149118
}
119+
}
150120

151-
/// Offset the entire range by some text size.
152-
pub fn checked_add(self, rhs: impl TryInto<TextSize>) -> Option<TextRange> {
153-
let rhs = rhs.try_into().ok()?;
154-
Some(TextRange(
155-
self.start().checked_add(rhs)?,
156-
self.end().checked_add(rhs)?,
157-
))
158-
}
159-
160-
/// Offset the entire range by some text size.
161-
pub fn checked_sub(self, rhs: impl TryInto<TextSize>) -> Option<TextRange> {
162-
let rhs = rhs.try_into().ok()?;
163-
Some(TextRange(
164-
self.start().checked_sub(rhs)?,
165-
self.end().checked_sub(rhs)?,
166-
))
167-
}
121+
fn ix(size: TextSize) -> usize {
122+
size.try_into()
123+
.unwrap_or_else(|_| panic!("overflow when converting TextSize to usize index"))
168124
}
169125

170126
impl Index<TextRange> for str {
171127
type Output = str;
172128
fn index(&self, index: TextRange) -> &Self::Output {
173-
&self[index.start().ix()..index.end().ix()]
129+
&self[ix(index.start())..ix(index.end())]
174130
}
175131
}
176132

177133
impl IndexMut<TextRange> for str {
178134
fn index_mut(&mut self, index: TextRange) -> &mut Self::Output {
179-
&mut self[index.start().ix()..index.end().ix()]
135+
&mut self[ix(index.start())..ix(index.end())]
180136
}
181137
}
182138

@@ -197,29 +153,7 @@ macro_rules! conversions {
197153
TextRange(value.start.into(), value.end.into())
198154
}
199155
}
200-
impl TryFrom<RangeInclusive<$lte>> for TextRange {
201-
type Error = TryFromIntError;
202-
fn try_from(value: RangeInclusive<$lte>) -> Result<TextRange, Self::Error> {
203-
let (start, end) = value.into_inner();
204-
let end: TextSize = end.into();
205-
// This is the only way to get a TryFromIntError currently.
206-
let end = end.checked_add(1).ok_or_else(|| u8::try_from(-1).unwrap_err())?;
207-
Ok(TextRange(start.into(), end))
208-
}
209-
}
210-
impl From<RangeTo<$lte>> for TextRange {
211-
fn from(value: RangeTo<$lte>) -> TextRange {
212-
TextRange(0.into(), value.end.into())
213-
}
214-
}
215-
impl TryFrom<RangeToInclusive<$lte>> for TextRange {
216-
type Error = TryFromIntError;
217-
fn try_from(value: RangeToInclusive<$lte>) -> Result<TextRange, Self::Error> {
218-
let start: TextSize = 0.into();
219-
let end: TextSize = value.end.into();
220-
TextRange::try_from(start..=end)
221-
}
222-
}
156+
// Just support `start..end` for now, not `..end`, `start..=end`, `..=end`.
223157
};
224158
(TryFrom<$gt:ident> for TextRange) => {
225159
impl TryFrom<Range<$gt>> for TextRange {
@@ -228,30 +162,7 @@ macro_rules! conversions {
228162
Ok(TextRange(value.start.try_into()?, value.end.try_into()?))
229163
}
230164
}
231-
impl TryFrom<RangeInclusive<$gt>> for TextRange {
232-
type Error = TryFromIntError;
233-
fn try_from(value: RangeInclusive<$gt>) -> Result<TextRange, Self::Error> {
234-
let (start, end) = value.into_inner();
235-
let end: TextSize = end.try_into()?;
236-
// This is the only way to get a TryFromIntError currently.
237-
let end = end.checked_add(1).ok_or_else(|| u8::try_from(-1).unwrap_err())?;
238-
Ok(TextRange(start.try_into()?, end))
239-
}
240-
}
241-
impl TryFrom<RangeTo<$gt>> for TextRange {
242-
type Error = TryFromIntError;
243-
fn try_from(value: RangeTo<$gt>) -> Result<TextRange, Self::Error> {
244-
Ok(TextRange(0.into(), value.end.try_into()?))
245-
}
246-
}
247-
impl TryFrom<RangeToInclusive<$gt>> for TextRange {
248-
type Error = TryFromIntError;
249-
fn try_from(value: RangeToInclusive<$gt>) -> Result<TextRange, Self::Error> {
250-
let start: TextSize = 0.into();
251-
let end: TextSize = value.end.try_into()?;
252-
TextRange::try_from(start..=end)
253-
}
254-
}
165+
// Just support `start..end` for now, not `..end`, `start..=end`, `..=end`.
255166
};
256167
{
257168
lt TextSize [$($lt:ident)*]
@@ -260,11 +171,8 @@ macro_rules! conversions {
260171
varries [$($var:ident)*]
261172
} => {
262173
$(
263-
// Not `From` yet because of integer type fallback. We want e.g.
264-
// `TextRange::from(0)` and `range + 1` to work, and more `From`
265-
// impls means that this will try (and fail) to use i32 rather
266-
// than one of the unsigned integer types that actually work.
267-
conversions!(TryFrom<$lt> for TextRange);
174+
conversions!(From<$lt> for TextRange);
175+
// unlike TextSize, we do not provide conversions in the "out" direction.
268176
)*
269177

270178
$(
@@ -288,68 +196,3 @@ conversions! {
288196
gt TextSize [u64]
289197
varries [usize]
290198
}
291-
292-
impl Into<TextRange> for &'_ TextRange {
293-
fn into(self) -> TextRange {
294-
*self
295-
}
296-
}
297-
298-
impl Into<TextRange> for &'_ mut TextRange {
299-
fn into(self) -> TextRange {
300-
*self
301-
}
302-
}
303-
304-
macro_rules! op {
305-
(impl $Op:ident for TextRange by fn $f:ident = $op:tt) => {
306-
impl<IntoSize: Copy> $Op<IntoSize> for TextRange
307-
where
308-
TextSize: $Op<IntoSize, Output = TextSize>,
309-
{
310-
type Output = TextRange;
311-
fn $f(self, rhs: IntoSize) -> TextRange {
312-
TextRange(self.start() $op rhs, self.end() $op rhs)
313-
}
314-
}
315-
impl<IntoSize> $Op<IntoSize> for &'_ TextRange
316-
where
317-
TextRange: $Op<IntoSize, Output = TextRange>,
318-
{
319-
type Output = TextRange;
320-
fn $f(self, rhs: IntoSize) -> TextRange {
321-
*self $op rhs
322-
}
323-
}
324-
impl<IntoSize> $Op<IntoSize> for &'_ mut TextRange
325-
where
326-
TextRange: $Op<IntoSize, Output = TextRange>,
327-
{
328-
type Output = TextRange;
329-
fn $f(self, rhs: IntoSize) -> TextRange {
330-
*self $op rhs
331-
}
332-
}
333-
};
334-
}
335-
336-
op!(impl Add for TextRange by fn add = +);
337-
op!(impl Sub for TextRange by fn sub = -);
338-
339-
impl<A> AddAssign<A> for TextRange
340-
where
341-
TextRange: Add<A, Output = TextRange>,
342-
{
343-
fn add_assign(&mut self, rhs: A) {
344-
*self = *self + rhs
345-
}
346-
}
347-
348-
impl<S> SubAssign<S> for TextRange
349-
where
350-
TextRange: Sub<S, Output = TextRange>,
351-
{
352-
fn sub_assign(&mut self, rhs: S) {
353-
*self = *self - rhs
354-
}
355-
}

0 commit comments

Comments
 (0)