Skip to content

Commit b26dc78

Browse files
committed
Add inline(always)
It's useful for cross-crate inlining
1 parent 20486e9 commit b26dc78

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
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_unit"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
authors = ["Aleksey Kladov <[email protected]>"]
55
description = "Newtypes for text offsets"
66
license = "MIT OR Apache-2.0"

src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct TextUnit(u32);
1212

1313
impl TextUnit {
1414
/// `TextUnit` equal to the length of this char.
15+
#[inline(always)]
1516
pub fn of_char(c: char) -> TextUnit {
1617
TextUnit(c.len_utf8() as u32)
1718
}
@@ -20,6 +21,7 @@ impl TextUnit {
2021
///
2122
/// # Panics
2223
/// Panics if the length of the string is greater than `u32::max_value()`
24+
#[inline(always)]
2325
pub fn of_str(s: &str) -> TextUnit {
2426
if s.len() > u32::max_value() as usize {
2527
panic!("string is to long")
@@ -35,18 +37,21 @@ impl fmt::Debug for TextUnit {
3537
}
3638

3739
impl fmt::Display for TextUnit {
40+
#[inline(always)]
3841
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3942
self.0.fmt(f)
4043
}
4144
}
4245

4346
impl From<TextUnit> for u32 {
47+
#[inline(always)]
4448
fn from(tu: TextUnit) -> u32 {
4549
tu.0
4650
}
4751
}
4852

4953
impl From<u32> for TextUnit {
54+
#[inline(always)]
5055
fn from(tu: u32) -> TextUnit {
5156
TextUnit(tu)
5257
}
@@ -57,39 +62,45 @@ macro_rules! ops_impls {
5762

5863
impl ops::$T<TextUnit> for TextUnit {
5964
type Output = TextUnit;
65+
#[inline(always)]
6066
fn $f(self, rhs: TextUnit) -> TextUnit {
6167
TextUnit(self.0 $op rhs.0)
6268
}
6369
}
6470

6571
impl<'a> ops::$T<&'a TextUnit> for TextUnit {
6672
type Output = TextUnit;
73+
#[inline(always)]
6774
fn $f(self, rhs: &'a TextUnit) -> TextUnit {
6875
ops::$T::$f(self, *rhs)
6976
}
7077
}
7178

7279
impl<'a> ops::$T<TextUnit> for &'a TextUnit {
7380
type Output = TextUnit;
81+
#[inline(always)]
7482
fn $f(self, rhs: TextUnit) -> TextUnit {
7583
ops::$T::$f(*self, rhs)
7684
}
7785
}
7886

7987
impl<'a, 'b> ops::$T<&'a TextUnit> for &'b TextUnit {
8088
type Output = TextUnit;
89+
#[inline(always)]
8190
fn $f(self, rhs: &'a TextUnit) -> TextUnit {
8291
ops::$T::$f(*self, *rhs)
8392
}
8493
}
8594

8695
impl ops::$AT<TextUnit> for TextUnit {
96+
#[inline(always)]
8797
fn $af(&mut self, rhs: TextUnit) {
8898
self.0 = self.0 $op rhs.0
8999
}
90100
}
91101

92102
impl<'a> ops::$AT<&'a TextUnit> for TextUnit {
103+
#[inline(always)]
93104
fn $af(&mut self, rhs: &'a TextUnit) {
94105
ops::$AT::$af(self, *rhs)
95106
}
@@ -137,6 +148,7 @@ impl fmt::Display for TextRange {
137148

138149
impl TextRange {
139150
/// The left-inclusive range (`[from..to)`) between to points in the text
151+
#[inline(always)]
140152
pub fn from_to(from: TextUnit, to: TextUnit) -> TextRange {
141153
assert!(from <= to, "Invalid text range [{}; {})", from, to);
142154
TextRange {
@@ -146,26 +158,31 @@ impl TextRange {
146158
}
147159

148160
/// The left-inclusive range (`[offset..offset + len)`) between to points in the text
161+
#[inline(always)]
149162
pub fn offset_len(offset: TextUnit, len: TextUnit) -> TextRange {
150163
TextRange::from_to(offset, offset + len)
151164
}
152165

153166
/// The inclusive start of this range
167+
#[inline(always)]
154168
pub fn start(&self) -> TextUnit {
155169
self.start
156170
}
157171

158172
/// The exclusive end of this range
173+
#[inline(always)]
159174
pub fn end(&self) -> TextUnit {
160175
self.end
161176
}
162177

163178
/// The length of this range
179+
#[inline(always)]
164180
pub fn len(&self) -> TextUnit {
165181
self.end - self.start
166182
}
167183

168184
/// Is this range empty of any content?
185+
#[inline(always)]
169186
pub fn is_empty(&self) -> bool {
170187
self.start() == self.end()
171188
}

0 commit comments

Comments
 (0)