Skip to content

Commit eb3294d

Browse files
committed
add #[inline] to small and single-use functions
1 parent 8f9eee9 commit eb3294d

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/lib.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl EscapeError {
8585
/// and produces a sequence of characters or errors,
8686
/// which are returned by invoking `callback`.
8787
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
88+
#[inline]
8889
pub fn check_raw_str(src: &str, callback: impl FnMut(Range<usize>, Result<char, EscapeError>)) {
8990
str::check_raw(src, callback);
9091
}
@@ -95,6 +96,7 @@ pub fn check_raw_str(src: &str, callback: impl FnMut(Range<usize>, Result<char,
9596
/// and produces a sequence of bytes or errors,
9697
/// which are returned by invoking `callback`.
9798
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
99+
#[inline]
98100
pub fn check_raw_byte_str(src: &str, callback: impl FnMut(Range<usize>, Result<u8, EscapeError>)) {
99101
<[u8]>::check_raw(src, callback);
100102
}
@@ -122,6 +124,7 @@ trait CheckRaw {
122124
/// which are returned via `callback`.
123125
///
124126
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
127+
#[inline]
125128
fn check_raw(
126129
src: &str,
127130
mut callback: impl FnMut(Range<usize>, Result<Self::RawUnit, EscapeError>),
@@ -154,6 +157,7 @@ trait CheckRaw {
154157
impl CheckRaw for str {
155158
type RawUnit = char;
156159

160+
#[inline]
157161
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
158162
Ok(c)
159163
}
@@ -162,12 +166,14 @@ impl CheckRaw for str {
162166
impl CheckRaw for [u8] {
163167
type RawUnit = u8;
164168

169+
#[inline]
165170
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
166171
char2byte(c)
167172
}
168173
}
169174

170175
/// Turn an ascii char into a byte
176+
#[inline]
171177
fn char2byte(c: char) -> Result<u8, EscapeError> {
172178
// do NOT do: c.try_into().ok_or(EscapeError::NonAsciiCharInByte)
173179
if c.is_ascii() {
@@ -180,6 +186,7 @@ fn char2byte(c: char) -> Result<u8, EscapeError> {
180186
impl CheckRaw for CStr {
181187
type RawUnit = char;
182188

189+
#[inline]
183190
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
184191
if c == '\0' {
185192
Err(EscapeError::NulInCStr)
@@ -193,6 +200,7 @@ impl CheckRaw for CStr {
193200
///
194201
/// Takes the contents of a char literal (without quotes),
195202
/// and returns an unescaped char or an error.
203+
#[inline]
196204
pub fn unescape_char(src: &str) -> Result<char, EscapeError> {
197205
str::unescape_single(&mut src.chars())
198206
}
@@ -201,6 +209,7 @@ pub fn unescape_char(src: &str) -> Result<char, EscapeError> {
201209
///
202210
/// Takes the contents of a byte literal (without quotes),
203211
/// and returns an unescaped byte or an error.
212+
#[inline]
204213
pub fn unescape_byte(src: &str) -> Result<u8, EscapeError> {
205214
<[u8]>::unescape_single(&mut src.chars())
206215
}
@@ -258,12 +267,14 @@ pub enum MixedUnit {
258267
}
259268

260269
impl From<char> for MixedUnit {
270+
#[inline]
261271
fn from(c: char) -> Self {
262272
MixedUnit::Char(c)
263273
}
264274
}
265275

266276
impl From<u8> for MixedUnit {
277+
#[inline]
267278
fn from(n: u8) -> Self {
268279
if n.is_ascii() {
269280
MixedUnit::Char(n as char)
@@ -364,6 +375,7 @@ trait Unescape {
364375
/// Interpret a non-nul ASCII escape
365376
///
366377
/// Parses the character of an ASCII escape (except nul) without the leading backslash.
378+
#[inline] // single use in Unescape::unescape_1
367379
fn simple_escape(c: char) -> Result<u8, char> {
368380
// Previous character was '\\', unescape what follows.
369381
Ok(match c {
@@ -380,6 +392,7 @@ fn simple_escape(c: char) -> Result<u8, char> {
380392
/// Interpret a hexadecimal escape
381393
///
382394
/// Parses the two hexadecimal characters of a hexadecimal escape without the leading r"\x".
395+
#[inline] // single use in Unescape::unescape_1
383396
fn hex_escape(chars: &mut impl Iterator<Item = char>) -> Result<u8, EscapeError> {
384397
let hi = chars.next().ok_or(EscapeError::TooShortHexEscape)?;
385398
let hi = hi.to_digit(16).ok_or(EscapeError::InvalidCharInHexEscape)?;
@@ -394,6 +407,7 @@ fn hex_escape(chars: &mut impl Iterator<Item = char>) -> Result<u8, EscapeError>
394407
///
395408
/// Parse the braces with hexadecimal characters (and underscores) part of a unicode escape.
396409
/// This r"{...}" normally comes after r"\u" and cannot start with an underscore.
410+
#[inline] // single use in Unescape::unescape_1
397411
fn unicode_escape(chars: &mut impl Iterator<Item = char>) -> Result<u32, EscapeError> {
398412
if chars.next() != Some('{') {
399413
return Err(EscapeError::NoBraceInUnicodeEscape);
@@ -444,10 +458,12 @@ fn unicode_escape(chars: &mut impl Iterator<Item = char>) -> Result<u32, EscapeE
444458
/// Skip ASCII whitespace, except for the formfeed character
445459
/// (see [this issue](https://github.com/rust-lang/rust/issues/136600)).
446460
/// Warns on unescaped newline and following non-ASCII whitespace.
447-
fn skip_ascii_whitespace<F>(chars: &mut Chars<'_>, start: usize, mut callback: F)
448-
where
449-
F: FnMut(Range<usize>, EscapeError),
450-
{
461+
#[inline] // single use in Unescape::unescape
462+
fn skip_ascii_whitespace(
463+
chars: &mut Chars<'_>,
464+
start: usize,
465+
mut callback: impl FnMut(Range<usize>, EscapeError),
466+
) {
451467
let rest = chars.as_str();
452468
let first_non_space = rest
453469
.bytes()
@@ -476,10 +492,12 @@ impl Unescape for str {
476492

477493
const ZERO_RESULT: Result<Self::Unit, EscapeError> = Ok('\0');
478494

495+
#[inline]
479496
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
480497
Ok(c)
481498
}
482499

500+
#[inline]
483501
fn hex2unit(b: u8) -> Result<Self::Unit, EscapeError> {
484502
if b.is_ascii() {
485503
Ok(b as char)
@@ -488,7 +506,7 @@ impl Unescape for str {
488506
}
489507
}
490508

491-
/// Converts the result of a unicode escape to the unit type
509+
#[inline]
492510
fn unicode2unit(r: Result<char, EscapeError>) -> Result<Self::Unit, EscapeError> {
493511
r
494512
}
@@ -499,15 +517,17 @@ impl Unescape for [u8] {
499517

500518
const ZERO_RESULT: Result<Self::Unit, EscapeError> = Ok(b'\0');
501519

520+
#[inline]
502521
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
503522
char2byte(c)
504523
}
505524

525+
#[inline]
506526
fn hex2unit(b: u8) -> Result<Self::Unit, EscapeError> {
507527
Ok(b)
508528
}
509529

510-
/// Converts the result of a unicode escape to the unit type
530+
#[inline]
511531
fn unicode2unit(_r: Result<char, EscapeError>) -> Result<Self::Unit, EscapeError> {
512532
Err(EscapeError::UnicodeEscapeInByte)
513533
}
@@ -518,6 +538,7 @@ impl Unescape for CStr {
518538

519539
const ZERO_RESULT: Result<Self::Unit, EscapeError> = Err(EscapeError::NulInCStr);
520540

541+
#[inline]
521542
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
522543
if c == '\0' {
523544
Err(EscapeError::NulInCStr)
@@ -526,6 +547,7 @@ impl Unescape for CStr {
526547
}
527548
}
528549

550+
#[inline]
529551
fn hex2unit(byte: u8) -> Result<Self::Unit, EscapeError> {
530552
if byte == b'\0' {
531553
Err(EscapeError::NulInCStr)
@@ -536,7 +558,7 @@ impl Unescape for CStr {
536558
}
537559
}
538560

539-
/// Converts the result of a unicode escape to the unit type
561+
#[inline]
540562
fn unicode2unit(r: Result<char, EscapeError>) -> Result<Self::Unit, EscapeError> {
541563
Self::char2unit(r?)
542564
}

0 commit comments

Comments
 (0)