diff --git a/lib/text-size/Cargo.toml b/lib/text-size/Cargo.toml index 19c5a92670..7882f7cc35 100644 --- a/lib/text-size/Cargo.toml +++ b/lib/text-size/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "text-size" -version = "1.1.0" +version = "1.1.1" edition = "2018" authors = [ diff --git a/lib/text-size/src/range.rs b/lib/text-size/src/range.rs index 4a98deec56..9b981642d1 100644 --- a/lib/text-size/src/range.rs +++ b/lib/text-size/src/range.rs @@ -44,8 +44,8 @@ impl TextRange { /// assert_eq!(range.len(), end - start); /// ``` #[inline] - pub fn new(start: TextSize, end: TextSize) -> TextRange { - assert!(start <= end); + pub const fn new(start: TextSize, end: TextSize) -> TextRange { + assert!(start.raw <= end.raw); TextRange { start, end } } @@ -65,8 +65,8 @@ impl TextRange { /// assert_eq!(&text[range], "23456") /// ``` #[inline] - pub fn at(offset: TextSize, len: TextSize) -> TextRange { - TextRange::new(offset, offset + len) + pub const fn at(offset: TextSize, len: TextSize) -> TextRange { + TextRange::new(offset, TextSize::new(offset.raw + len.raw)) } /// Create a zero-length range at the specified offset (`offset..offset`). @@ -82,7 +82,7 @@ impl TextRange { /// assert_eq!(range, TextRange::new(point, point)); /// ``` #[inline] - pub fn empty(offset: TextSize) -> TextRange { + pub const fn empty(offset: TextSize) -> TextRange { TextRange { start: offset, end: offset, @@ -104,9 +104,9 @@ impl TextRange { /// assert_eq!(range, TextRange::at(0.into(), point)); /// ``` #[inline] - pub fn up_to(end: TextSize) -> TextRange { + pub const fn up_to(end: TextSize) -> TextRange { TextRange { - start: 0.into(), + start: TextSize::new(0), end, } } diff --git a/lib/text-size/src/size.rs b/lib/text-size/src/size.rs index ab2ec9a730..c950d2edd0 100644 --- a/lib/text-size/src/size.rs +++ b/lib/text-size/src/size.rs @@ -33,6 +33,12 @@ impl fmt::Debug for TextSize { } impl TextSize { + /// Creates a new instance of `TextSize` from a raw `u32`. + #[inline] + pub const fn new(raw: u32) -> TextSize { + TextSize { raw } + } + /// The text size of some primitive text-like object. /// /// Accepts `char`, `&str`, and `&String`. @@ -58,14 +64,20 @@ impl TextSize { impl TextSize { /// Checked addition. Returns `None` if overflow occurred. #[inline] - pub fn checked_add(self, rhs: TextSize) -> Option { - self.raw.checked_add(rhs.raw).map(|raw| TextSize { raw }) + pub const fn checked_add(self, rhs: TextSize) -> Option { + match self.raw.checked_add(rhs.raw) { + Some(raw) => Some(TextSize { raw }), + None => None, + } } /// Checked subtraction. Returns `None` if overflow occurred. #[inline] - pub fn checked_sub(self, rhs: TextSize) -> Option { - self.raw.checked_sub(rhs.raw).map(|raw| TextSize { raw }) + pub const fn checked_sub(self, rhs: TextSize) -> Option { + match self.raw.checked_sub(rhs.raw) { + Some(raw) => Some(TextSize { raw }), + None => None, + } } } diff --git a/lib/text-size/tests/indexing.rs b/lib/text-size/tests/indexing.rs index ebbed7700d..93ba3c7cab 100644 --- a/lib/text-size/tests/indexing.rs +++ b/lib/text-size/tests/indexing.rs @@ -3,6 +3,6 @@ use text_size::*; #[test] fn main() { let range = TextRange::default(); - &""[range]; - &String::new()[range]; + _ = &""[range]; + _ = &String::new()[range]; }