mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-12-27 16:07:46 +00:00
Constify some constructors
This commit is contained in:
parent
19be44515a
commit
d46914fa78
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "text-size"
|
||||
version = "1.1.0"
|
||||
version = "1.1.1"
|
||||
edition = "2018"
|
||||
|
||||
authors = [
|
||||
|
||||
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<TextSize> {
|
||||
self.raw.checked_add(rhs.raw).map(|raw| TextSize { raw })
|
||||
pub const fn checked_add(self, rhs: TextSize) -> Option<TextSize> {
|
||||
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<TextSize> {
|
||||
self.raw.checked_sub(rhs.raw).map(|raw| TextSize { raw })
|
||||
pub const fn checked_sub(self, rhs: TextSize) -> Option<TextSize> {
|
||||
match self.raw.checked_sub(rhs.raw) {
|
||||
Some(raw) => Some(TextSize { raw }),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -3,6 +3,6 @@ use text_size::*;
|
||||
#[test]
|
||||
fn main() {
|
||||
let range = TextRange::default();
|
||||
&""[range];
|
||||
&String::new()[range];
|
||||
_ = &""[range];
|
||||
_ = &String::new()[range];
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user