Unnamed repository; edit this file 'description' to name the repository.
Add #[inline] to most things
CAD97 2020-03-19
parent 6d6e97e · commit 0c77efb
-rw-r--r--lib/text-size/src/range.rs12
-rw-r--r--lib/text-size/src/size.rs19
-rw-r--r--lib/text-size/src/traits.rs4
3 files changed, 33 insertions, 2 deletions
diff --git a/lib/text-size/src/range.rs b/lib/text-size/src/range.rs
index 35c487afed..6484d2ea34 100644
--- a/lib/text-size/src/range.rs
+++ b/lib/text-size/src/range.rs
@@ -40,6 +40,7 @@ impl fmt::Debug for TextRange {
///
/// Panics if `end < start`.
#[allow(non_snake_case)]
+#[inline]
pub fn TextRange(start: TextSize, end: TextSize) -> TextRange {
assert!(start <= end);
TextRange { start, end }
@@ -47,6 +48,7 @@ pub fn TextRange(start: TextSize, end: TextSize) -> TextRange {
impl TextRange {
/// Create a zero-length range at the specified offset (`offset..offset`).
+ #[inline]
pub const fn empty(offset: TextSize) -> TextRange {
TextRange {
start: offset,
@@ -55,6 +57,7 @@ impl TextRange {
}
/// Create a range up to the given end (`..end`).
+ #[inline]
pub const fn before(end: TextSize) -> TextRange {
TextRange {
start: TextSize::zero(),
@@ -68,6 +71,7 @@ impl TextRange {
/// `TextRange` does not support right-unbounded ranges. As such, this
/// should only be used for direct indexing, and bounded ranges should be
/// used for persistent ranges (`TextRange(start, TextSize::of(text))`).
+ #[inline]
pub const fn after(start: TextSize) -> RangeFrom<usize> {
start.raw as usize..
}
@@ -76,6 +80,7 @@ impl TextRange {
///
/// This is typically used to convert a range from one coordinate space to
/// another, such as from within a substring to within an entire document.
+ #[inline]
pub fn offset(self, offset: TextSize) -> TextRange {
TextRange(
self.start().checked_add(offset).unwrap(),
@@ -87,22 +92,26 @@ impl TextRange {
/// Identity methods.
impl TextRange {
/// The start point of this range.
+ #[inline]
pub const fn start(self) -> TextSize {
self.start
}
/// The end point of this range.
+ #[inline]
pub const fn end(self) -> TextSize {
self.end
}
/// The size of this range.
+ #[inline]
pub const fn len(self) -> TextSize {
// HACK for const fn: math on primitives only
TextSize(self.end().raw - self.start().raw)
}
/// Check if this range is empty.
+ #[inline]
pub const fn is_empty(self) -> bool {
// HACK for const fn: math on primitives only
self.start().raw == self.end().raw
@@ -151,12 +160,14 @@ impl TextRange {
impl Index<TextRange> for str {
type Output = str;
+ #[inline]
fn index(&self, index: TextRange) -> &Self::Output {
&self[Range::<usize>::from(index)]
}
}
impl IndexMut<TextRange> for str {
+ #[inline]
fn index_mut(&mut self, index: TextRange) -> &mut Self::Output {
&mut self[Range::<usize>::from(index)]
}
@@ -176,6 +187,7 @@ impl<T> From<TextRange> for Range<T>
where
T: From<TextSize>,
{
+ #[inline]
fn from(r: TextRange) -> Self {
r.start().into()..r.end().into()
}
diff --git a/lib/text-size/src/size.rs b/lib/text-size/src/size.rs
index 5b435e896c..4436b9f6f2 100644
--- a/lib/text-size/src/size.rs
+++ b/lib/text-size/src/size.rs
@@ -46,6 +46,7 @@ impl fmt::Debug for TextSize {
impl TextSize {
/// The text size of some text-like object.
+ #[inline]
pub fn of(text: impl TextSized) -> TextSize {
text.text_size()
}
@@ -54,11 +55,13 @@ impl TextSize {
///
/// This is equivalent to `TextSize::default()` or [`TextSize::MIN`],
/// but is more explicit on intent.
+ #[inline]
pub const fn zero() -> TextSize {
TextSize(0)
}
/// A size of one.
+ #[inline]
pub const fn one() -> TextSize {
TextSize(1)
}
@@ -74,24 +77,28 @@ impl TextSize {
/// The text size of a single ASCII character.
pub const ONE: TextSize = TextSize(1);
- #[allow(missing_docs)]
+ /// Checked addition. Returns `None` if overflow occurred.
+ #[inline]
pub fn checked_add(self, rhs: TextSize) -> Option<TextSize> {
self.raw.checked_add(rhs.raw).map(TextSize)
}
- #[allow(missing_docs)]
+ /// Checked subtraction. Returns `None` if overflow occurred.
+ #[inline]
pub fn checked_sub(self, rhs: TextSize) -> Option<TextSize> {
self.raw.checked_sub(rhs.raw).map(TextSize)
}
}
impl From<u32> for TextSize {
+ #[inline]
fn from(raw: u32) -> Self {
TextSize(raw)
}
}
impl From<TextSize> for u32 {
+ #[inline]
fn from(value: TextSize) -> Self {
value.raw
}
@@ -99,12 +106,14 @@ impl From<TextSize> for u32 {
impl TryFrom<usize> for TextSize {
type Error = TryFromIntError;
+ #[inline]
fn try_from(value: usize) -> Result<Self, TryFromIntError> {
Ok(u32::try_from(value)?.into())
}
}
impl From<TextSize> for usize {
+ #[inline]
fn from(value: TextSize) -> Self {
value.raw as usize
}
@@ -114,12 +123,14 @@ macro_rules! ops {
(impl $Op:ident for TextSize by fn $f:ident = $op:tt) => {
impl $Op<TextSize> for TextSize {
type Output = TextSize;
+ #[inline]
fn $f(self, other: TextSize) -> TextSize {
TextSize(self.raw $op other.raw)
}
}
impl $Op<&TextSize> for TextSize {
type Output = TextSize;
+ #[inline]
fn $f(self, other: &TextSize) -> TextSize {
self $op *other
}
@@ -129,6 +140,7 @@ macro_rules! ops {
TextSize: $Op<T, Output=TextSize>,
{
type Output = TextSize;
+ #[inline]
fn $f(self, other: T) -> TextSize {
*self $op other
}
@@ -143,6 +155,7 @@ impl<A> AddAssign<A> for TextSize
where
TextSize: Add<A, Output = TextSize>,
{
+ #[inline]
fn add_assign(&mut self, rhs: A) {
*self = *self + rhs
}
@@ -152,6 +165,7 @@ impl<S> SubAssign<S> for TextSize
where
TextSize: Sub<S, Output = TextSize>,
{
+ #[inline]
fn sub_assign(&mut self, rhs: S) {
*self = *self - rhs
}
@@ -161,6 +175,7 @@ impl<A> iter::Sum<A> for TextSize
where
TextSize: Add<A, Output = TextSize>,
{
+ #[inline]
fn sum<I: Iterator<Item = A>>(iter: I) -> TextSize {
iter.fold(TextSize::zero(), Add::add)
}
diff --git a/lib/text-size/src/traits.rs b/lib/text-size/src/traits.rs
index ca4b7d9b42..fac1eccdc6 100644
--- a/lib/text-size/src/traits.rs
+++ b/lib/text-size/src/traits.rs
@@ -6,7 +6,10 @@ pub trait TextSized: Copy {
fn text_size(self) -> TextSize;
}
+/// This will panic for strings larger than `TextSize::MAX` when
+/// debug assertions are enabled, and wrap when they are disabled.
impl TextSized for &'_ str {
+ #[inline]
fn text_size(self) -> TextSize {
self.len()
.try_into()
@@ -15,6 +18,7 @@ impl TextSized for &'_ str {
}
impl TextSized for char {
+ #[inline]
fn text_size(self) -> TextSize {
TextSize(self.len_utf8() as u32)
}