Skip to content

Commit 73ad7ea

Browse files
bors[bot]CAD97
andauthored
Merge #28
28: Add doctests to things r=matklad a=CAD97 This also includes a test to make sure the types implement the common std traits. Co-authored-by: CAD97 <[email protected]>
2 parents cf9e29e + 1d2de42 commit 73ad7ea

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ serde = { version = "1.0", optional = true, default_features = false }
1717

1818
[dev-dependencies]
1919
serde_test = "1.0"
20+
static_assertions = "1.1"
2021

2122
[[test]]
2223
name = "serde"

src/range.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use {
88

99
/// A range in text, represented as a pair of [`TextSize`][struct@TextSize].
1010
///
11+
/// It is a logic error for `start` to be greater than `end`.
12+
///
1113
/// # Translation from `text_unit`
1214
///
1315
/// - `TextRange::from_to(from, to)` ⟹ `TextRange::new(from, to)`
@@ -40,19 +42,57 @@ impl TextRange {
4042
/// # Panics
4143
///
4244
/// Panics if `end < start`.
45+
///
46+
/// # Examples
47+
///
48+
/// ```rust
49+
/// # use text_size::*;
50+
/// let start = TextSize::from(5);
51+
/// let end = TextSize::from(10);
52+
/// let range = TextRange::new(start, end);
53+
///
54+
/// assert_eq!(range.start(), start);
55+
/// assert_eq!(range.end(), end);
56+
/// assert_eq!(range.len(), end - start);
57+
/// ```
4358
#[inline]
4459
pub fn new(start: TextSize, end: TextSize) -> TextRange {
4560
assert!(start <= end);
4661
TextRange { start, end }
4762
}
4863

4964
/// Create a new `TextRange` with the given `start` and `len` (`start..start + len`).
65+
///
66+
/// # Examples
67+
///
68+
/// ```rust
69+
/// # use text_size::*;
70+
/// let text = "0123456789";
71+
///
72+
/// let start = TextSize::from(2);
73+
/// let length = TextSize::from(5);
74+
/// let range = TextRange::from_len(start, length);
75+
///
76+
/// assert_eq!(range, TextRange::new(start, start + length));
77+
/// assert_eq!(&text[range], "23456")
78+
/// ```
5079
#[inline]
5180
pub fn from_len(start: TextSize, len: TextSize) -> TextRange {
5281
TextRange::new(start, start + len)
5382
}
5483

5584
/// Create a zero-length range at the specified offset (`offset..offset`).
85+
///
86+
/// # Examples
87+
///
88+
/// ```rust
89+
/// # use text_size::*;
90+
/// let point: TextSize;
91+
/// # point = TextSize::from(3);
92+
/// let range = TextRange::empty(point);
93+
/// assert!(range.is_empty());
94+
/// assert_eq!(range, TextRange::new(point, point));
95+
/// ```
5696
#[inline]
5797
pub const fn empty(offset: TextSize) -> TextRange {
5898
TextRange {
@@ -62,6 +102,19 @@ impl TextRange {
62102
}
63103

64104
/// Create a range up to the given end (`..end`).
105+
///
106+
/// # Examples
107+
///
108+
/// ```rust
109+
/// # use text_size::*;
110+
/// let point: TextSize;
111+
/// # point = TextSize::from(12);
112+
/// let range = TextRange::up_to(point);
113+
///
114+
/// assert_eq!(range.len(), point);
115+
/// assert_eq!(range, TextRange::new(TextSize::zero(), point));
116+
/// assert_eq!(range, TextRange::from_len(TextSize::zero(), point));
117+
/// ```
65118
#[inline]
66119
pub const fn up_to(end: TextSize) -> TextRange {
67120
let start = TextSize::zero();
@@ -105,24 +158,73 @@ impl TextRange {
105158
/// Check if this range contains an offset.
106159
///
107160
/// The end index is considered excluded.
161+
///
162+
/// # Examples
163+
///
164+
/// ```rust
165+
/// # use text_size::*;
166+
/// let (start, end): (TextSize, TextSize);
167+
/// # start = 10.into(); end = 20.into();
168+
/// let range = TextRange::new(start, end);
169+
/// assert!(range.contains(start));
170+
/// assert!(!range.contains(end));
171+
/// ```
108172
pub fn contains(self, offset: TextSize) -> bool {
109173
self.start() <= offset && offset < self.end()
110174
}
111175

112176
/// Check if this range contains an offset.
113177
///
114178
/// The end index is considered included.
179+
///
180+
/// # Examples
181+
///
182+
/// ```rust
183+
/// # use text_size::*;
184+
/// let (start, end): (TextSize, TextSize);
185+
/// # start = 10.into(); end = 20.into();
186+
/// let range = TextRange::new(start, end);
187+
/// assert!(range.contains_inclusive(start));
188+
/// assert!(range.contains_inclusive(end));
189+
/// ```
115190
pub fn contains_inclusive(self, offset: TextSize) -> bool {
116191
self.start() <= offset && offset <= self.end()
117192
}
118193

119194
/// Check if this range completely contains another range.
195+
///
196+
/// # Examples
197+
///
198+
/// ```rust
199+
/// # use text_size::*;
200+
/// let larger = TextRange::new(0.into(), 20.into());
201+
/// let smaller = TextRange::new(5.into(), 15.into());
202+
/// assert!(larger.contains_range(smaller));
203+
/// assert!(!smaller.contains_range(larger));
204+
///
205+
/// // a range always contains itself
206+
/// assert!(larger.contains_range(larger));
207+
/// assert!(smaller.contains_range(smaller));
208+
/// ```
120209
pub fn contains_range(self, other: TextRange) -> bool {
121210
self.start() <= other.start() && other.end() <= self.end()
122211
}
123212

124213
/// The range covered by both ranges, if it exists.
125214
/// If the ranges touch but do not overlap, the output range is empty.
215+
///
216+
/// # Examples
217+
///
218+
/// ```rust
219+
/// # use text_size::*;
220+
/// assert_eq!(
221+
/// TextRange::intersect(
222+
/// TextRange::new(0.into(), 10.into()),
223+
/// TextRange::new(5.into(), 15.into()),
224+
/// ),
225+
/// Some(TextRange::new(5.into(), 10.into())),
226+
/// );
227+
/// ```
126228
pub fn intersect(self, other: TextRange) -> Option<TextRange> {
127229
let start = cmp::max(self.start(), other.start());
128230
let end = cmp::min(self.end(), other.end());
@@ -133,13 +235,36 @@ impl TextRange {
133235
}
134236

135237
/// Extends the range to cover `other` as well.
238+
///
239+
/// # Examples
240+
///
241+
/// ```rust
242+
/// # use text_size::*;
243+
/// assert_eq!(
244+
/// TextRange::cover(
245+
/// TextRange::new(0.into(), 5.into()),
246+
/// TextRange::new(15.into(), 20.into()),
247+
/// ),
248+
/// TextRange::new(0.into(), 20.into()),
249+
/// );
250+
/// ```
136251
pub fn cover(self, other: TextRange) -> TextRange {
137252
let start = cmp::min(self.start(), other.start());
138253
let end = cmp::max(self.end(), other.end());
139254
TextRange::new(start, end)
140255
}
141256

142257
/// Extends the range to cover `other` offsets as well.
258+
///
259+
/// # Examples
260+
///
261+
/// ```rust
262+
/// # use text_size::*;
263+
/// assert_eq!(
264+
/// TextRange::empty(TextSize::zero()).cover_offset(20.into()),
265+
/// TextRange::new(0.into(), 20.into()),
266+
/// )
267+
/// ```
143268
pub fn cover_offset(self, offset: TextSize) -> TextRange {
144269
self.cover(TextRange::empty(offset))
145270
}

src/size.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ impl fmt::Debug for TextSize {
4141

4242
impl TextSize {
4343
/// The text size of some text-like object.
44+
///
45+
/// Accepts `char`, `&str`, and references to any custom string-like type
46+
/// that dereferences to `str`. Types that don't dereference to `str` but
47+
/// want to be usable in this constructor can implement [`TextSized`].
48+
///
49+
/// # Examples
50+
///
51+
/// ```rust
52+
/// # use text_size::*;
53+
/// let char_size = TextSize::of('🦀');
54+
/// assert_eq!(char_size, TextSize::from(4));
55+
///
56+
/// let str_size = TextSize::of("rust-analyzer");
57+
/// assert_eq!(str_size, TextSize::from(13));
58+
/// ```
4459
#[inline]
4560
pub fn of(text: impl TextSized) -> TextSize {
4661
text.text_size()

tests/auto_traits.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use {
2+
static_assertions::*,
3+
std::{
4+
fmt::Debug,
5+
hash::Hash,
6+
marker::{Send, Sync},
7+
panic::{RefUnwindSafe, UnwindSafe},
8+
},
9+
text_size::*,
10+
};
11+
12+
// auto traits
13+
assert_impl_all!(TextSize: Send, Sync, Unpin, UnwindSafe, RefUnwindSafe);
14+
assert_impl_all!(TextRange: Send, Sync, Unpin, UnwindSafe, RefUnwindSafe);
15+
16+
// common traits
17+
assert_impl_all!(TextSize: Copy, Debug, Default, Hash, Ord);
18+
assert_impl_all!(TextRange: Copy, Debug, Default, Hash, Eq);

0 commit comments

Comments
 (0)