Skip to content

Commit 8b792e9

Browse files
authored
Merge pull request #52 from rust-analyzer/constify
2 parents e4d0f2b + 5f22fbe commit 8b792e9

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
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 = "1.1.0"
3+
version = "1.1.1"
44
edition = "2018"
55

66
authors = [

src/range.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ impl TextRange {
4444
/// assert_eq!(range.len(), end - start);
4545
/// ```
4646
#[inline]
47-
pub fn new(start: TextSize, end: TextSize) -> TextRange {
48-
assert!(start <= end);
47+
pub const fn new(start: TextSize, end: TextSize) -> TextRange {
48+
assert!(start.raw <= end.raw);
4949
TextRange { start, end }
5050
}
5151

@@ -65,8 +65,8 @@ impl TextRange {
6565
/// assert_eq!(&text[range], "23456")
6666
/// ```
6767
#[inline]
68-
pub fn at(offset: TextSize, len: TextSize) -> TextRange {
69-
TextRange::new(offset, offset + len)
68+
pub const fn at(offset: TextSize, len: TextSize) -> TextRange {
69+
TextRange::new(offset, TextSize::new(offset.raw + len.raw))
7070
}
7171

7272
/// Create a zero-length range at the specified offset (`offset..offset`).
@@ -82,7 +82,7 @@ impl TextRange {
8282
/// assert_eq!(range, TextRange::new(point, point));
8383
/// ```
8484
#[inline]
85-
pub fn empty(offset: TextSize) -> TextRange {
85+
pub const fn empty(offset: TextSize) -> TextRange {
8686
TextRange {
8787
start: offset,
8888
end: offset,
@@ -104,9 +104,9 @@ impl TextRange {
104104
/// assert_eq!(range, TextRange::at(0.into(), point));
105105
/// ```
106106
#[inline]
107-
pub fn up_to(end: TextSize) -> TextRange {
107+
pub const fn up_to(end: TextSize) -> TextRange {
108108
TextRange {
109-
start: 0.into(),
109+
start: TextSize::new(0),
110110
end,
111111
}
112112
}

src/size.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ impl fmt::Debug for TextSize {
3333
}
3434

3535
impl TextSize {
36+
/// Creates a new instance of `TextSize` from a raw `u32`.
37+
#[inline]
38+
pub const fn new(raw: u32) -> TextSize {
39+
TextSize { raw }
40+
}
41+
3642
/// The text size of some primitive text-like object.
3743
///
3844
/// Accepts `char`, `&str`, and `&String`.
@@ -58,14 +64,20 @@ impl TextSize {
5864
impl TextSize {
5965
/// Checked addition. Returns `None` if overflow occurred.
6066
#[inline]
61-
pub fn checked_add(self, rhs: TextSize) -> Option<TextSize> {
62-
self.raw.checked_add(rhs.raw).map(|raw| TextSize { raw })
67+
pub const fn checked_add(self, rhs: TextSize) -> Option<TextSize> {
68+
match self.raw.checked_add(rhs.raw) {
69+
Some(raw) => Some(TextSize { raw }),
70+
None => None,
71+
}
6372
}
6473

6574
/// Checked subtraction. Returns `None` if overflow occurred.
6675
#[inline]
67-
pub fn checked_sub(self, rhs: TextSize) -> Option<TextSize> {
68-
self.raw.checked_sub(rhs.raw).map(|raw| TextSize { raw })
76+
pub const fn checked_sub(self, rhs: TextSize) -> Option<TextSize> {
77+
match self.raw.checked_sub(rhs.raw) {
78+
Some(raw) => Some(TextSize { raw }),
79+
None => None,
80+
}
6981
}
7082
}
7183

tests/indexing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ use text_size::*;
33
#[test]
44
fn main() {
55
let range = TextRange::default();
6-
&""[range];
7-
&String::new()[range];
6+
_ = &""[range];
7+
_ = &String::new()[range];
88
}

0 commit comments

Comments
 (0)