Skip to content

Commit f9d7005

Browse files
committed
Add #[inline] to most things
1 parent 8d19701 commit f9d7005

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/range.rs

+12
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ impl fmt::Debug for TextRange {
4040
///
4141
/// Panics if `end < start`.
4242
#[allow(non_snake_case)]
43+
#[inline]
4344
pub fn TextRange(start: TextSize, end: TextSize) -> TextRange {
4445
assert!(start <= end);
4546
TextRange { start, end }
4647
}
4748

4849
impl TextRange {
4950
/// Create a zero-length range at the specified offset (`offset..offset`).
51+
#[inline]
5052
pub const fn empty(offset: TextSize) -> TextRange {
5153
TextRange {
5254
start: offset,
@@ -55,6 +57,7 @@ impl TextRange {
5557
}
5658

5759
/// Create a range up to the given end (`..end`).
60+
#[inline]
5861
pub const fn before(end: TextSize) -> TextRange {
5962
TextRange {
6063
start: TextSize::zero(),
@@ -68,6 +71,7 @@ impl TextRange {
6871
/// `TextRange` does not support right-unbounded ranges. As such, this
6972
/// should only be used for direct indexing, and bounded ranges should be
7073
/// used for persistent ranges (`TextRange(start, TextSize::of(text))`).
74+
#[inline]
7175
pub const fn after(start: TextSize) -> RangeFrom<usize> {
7276
start.raw as usize..
7377
}
@@ -76,6 +80,7 @@ impl TextRange {
7680
///
7781
/// This is typically used to convert a range from one coordinate space to
7882
/// another, such as from within a substring to within an entire document.
83+
#[inline]
7984
pub fn offset(self, offset: TextSize) -> TextRange {
8085
TextRange(
8186
self.start().checked_add(offset).unwrap(),
@@ -87,22 +92,26 @@ impl TextRange {
8792
/// Identity methods.
8893
impl TextRange {
8994
/// The start point of this range.
95+
#[inline]
9096
pub const fn start(self) -> TextSize {
9197
self.start
9298
}
9399

94100
/// The end point of this range.
101+
#[inline]
95102
pub const fn end(self) -> TextSize {
96103
self.end
97104
}
98105

99106
/// The size of this range.
107+
#[inline]
100108
pub const fn len(self) -> TextSize {
101109
// HACK for const fn: math on primitives only
102110
TextSize(self.end().raw - self.start().raw)
103111
}
104112

105113
/// Check if this range is empty.
114+
#[inline]
106115
pub const fn is_empty(self) -> bool {
107116
// HACK for const fn: math on primitives only
108117
self.start().raw == self.end().raw
@@ -151,12 +160,14 @@ impl TextRange {
151160

152161
impl Index<TextRange> for str {
153162
type Output = str;
163+
#[inline]
154164
fn index(&self, index: TextRange) -> &Self::Output {
155165
&self[Range::<usize>::from(index)]
156166
}
157167
}
158168

159169
impl IndexMut<TextRange> for str {
170+
#[inline]
160171
fn index_mut(&mut self, index: TextRange) -> &mut Self::Output {
161172
&mut self[Range::<usize>::from(index)]
162173
}
@@ -176,6 +187,7 @@ impl<T> From<TextRange> for Range<T>
176187
where
177188
T: From<TextSize>,
178189
{
190+
#[inline]
179191
fn from(r: TextRange) -> Self {
180192
r.start().into()..r.end().into()
181193
}

src/size.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl fmt::Debug for TextSize {
4646

4747
impl TextSize {
4848
/// The text size of some text-like object.
49+
#[inline]
4950
pub fn of(text: impl TextSized) -> TextSize {
5051
text.text_size()
5152
}
@@ -54,11 +55,13 @@ impl TextSize {
5455
///
5556
/// This is equivalent to `TextSize::default()` or [`TextSize::MIN`],
5657
/// but is more explicit on intent.
58+
#[inline]
5759
pub const fn zero() -> TextSize {
5860
TextSize(0)
5961
}
6062

6163
/// A size of one.
64+
#[inline]
6265
pub const fn one() -> TextSize {
6366
TextSize(1)
6467
}
@@ -74,37 +77,43 @@ impl TextSize {
7477
/// The text size of a single ASCII character.
7578
pub const ONE: TextSize = TextSize(1);
7679

77-
#[allow(missing_docs)]
80+
/// Checked addition. Returns `None` if overflow occurred.
81+
#[inline]
7882
pub fn checked_add(self, rhs: TextSize) -> Option<TextSize> {
7983
self.raw.checked_add(rhs.raw).map(TextSize)
8084
}
8185

82-
#[allow(missing_docs)]
86+
/// Checked subtraction. Returns `None` if overflow occurred.
87+
#[inline]
8388
pub fn checked_sub(self, rhs: TextSize) -> Option<TextSize> {
8489
self.raw.checked_sub(rhs.raw).map(TextSize)
8590
}
8691
}
8792

8893
impl From<u32> for TextSize {
94+
#[inline]
8995
fn from(raw: u32) -> Self {
9096
TextSize(raw)
9197
}
9298
}
9399

94100
impl From<TextSize> for u32 {
101+
#[inline]
95102
fn from(value: TextSize) -> Self {
96103
value.raw
97104
}
98105
}
99106

100107
impl TryFrom<usize> for TextSize {
101108
type Error = TryFromIntError;
109+
#[inline]
102110
fn try_from(value: usize) -> Result<Self, TryFromIntError> {
103111
Ok(u32::try_from(value)?.into())
104112
}
105113
}
106114

107115
impl From<TextSize> for usize {
116+
#[inline]
108117
fn from(value: TextSize) -> Self {
109118
value.raw as usize
110119
}
@@ -114,12 +123,14 @@ macro_rules! ops {
114123
(impl $Op:ident for TextSize by fn $f:ident = $op:tt) => {
115124
impl $Op<TextSize> for TextSize {
116125
type Output = TextSize;
126+
#[inline]
117127
fn $f(self, other: TextSize) -> TextSize {
118128
TextSize(self.raw $op other.raw)
119129
}
120130
}
121131
impl $Op<&TextSize> for TextSize {
122132
type Output = TextSize;
133+
#[inline]
123134
fn $f(self, other: &TextSize) -> TextSize {
124135
self $op *other
125136
}
@@ -129,6 +140,7 @@ macro_rules! ops {
129140
TextSize: $Op<T, Output=TextSize>,
130141
{
131142
type Output = TextSize;
143+
#[inline]
132144
fn $f(self, other: T) -> TextSize {
133145
*self $op other
134146
}
@@ -143,6 +155,7 @@ impl<A> AddAssign<A> for TextSize
143155
where
144156
TextSize: Add<A, Output = TextSize>,
145157
{
158+
#[inline]
146159
fn add_assign(&mut self, rhs: A) {
147160
*self = *self + rhs
148161
}
@@ -152,6 +165,7 @@ impl<S> SubAssign<S> for TextSize
152165
where
153166
TextSize: Sub<S, Output = TextSize>,
154167
{
168+
#[inline]
155169
fn sub_assign(&mut self, rhs: S) {
156170
*self = *self - rhs
157171
}
@@ -161,6 +175,7 @@ impl<A> iter::Sum<A> for TextSize
161175
where
162176
TextSize: Add<A, Output = TextSize>,
163177
{
178+
#[inline]
164179
fn sum<I: Iterator<Item = A>>(iter: I) -> TextSize {
165180
iter.fold(TextSize::zero(), Add::add)
166181
}

src/traits.rs

+4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ pub trait TextSized: Copy {
66
fn text_size(self) -> TextSize;
77
}
88

9+
/// This will panic for strings larger than `TextSize::MAX` when
10+
/// debug assertions are enabled, and wrap when they are disabled.
911
impl TextSized for &'_ str {
12+
#[inline]
1013
fn text_size(self) -> TextSize {
1114
self.len()
1215
.try_into()
@@ -15,6 +18,7 @@ impl TextSized for &'_ str {
1518
}
1619

1720
impl TextSized for char {
21+
#[inline]
1822
fn text_size(self) -> TextSize {
1923
TextSize(self.len_utf8() as u32)
2024
}

0 commit comments

Comments
 (0)