Skip to content

Commit 73aae9e

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

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/lib.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ trait CheckRaw {
122122
/// which are returned via `callback`.
123123
///
124124
/// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
125+
#[cfg_attr(any(target_arch = "aarch64", target_arch = "arm"), inline)]
125126
fn check_raw(
126127
src: &str,
127128
mut callback: impl FnMut(Range<usize>, Result<Self::RawUnit, EscapeError>),
@@ -154,6 +155,7 @@ trait CheckRaw {
154155
impl CheckRaw for str {
155156
type RawUnit = char;
156157

158+
#[inline]
157159
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
158160
Ok(c)
159161
}
@@ -162,12 +164,14 @@ impl CheckRaw for str {
162164
impl CheckRaw for [u8] {
163165
type RawUnit = u8;
164166

167+
#[inline]
165168
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
166169
char2byte(c)
167170
}
168171
}
169172

170173
/// Turn an ascii char into a byte
174+
#[inline]
171175
fn char2byte(c: char) -> Result<u8, EscapeError> {
172176
// do NOT do: c.try_into().ok_or(EscapeError::NonAsciiCharInByte)
173177
if c.is_ascii() {
@@ -180,6 +184,7 @@ fn char2byte(c: char) -> Result<u8, EscapeError> {
180184
impl CheckRaw for CStr {
181185
type RawUnit = char;
182186

187+
#[inline]
183188
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
184189
if c == '\0' {
185190
Err(EscapeError::NulInCStr)
@@ -193,6 +198,7 @@ impl CheckRaw for CStr {
193198
///
194199
/// Takes the contents of a char literal (without quotes),
195200
/// and returns an unescaped char or an error.
201+
#[inline]
196202
pub fn unescape_char(src: &str) -> Result<char, EscapeError> {
197203
str::unescape_single(&mut src.chars())
198204
}
@@ -201,6 +207,7 @@ pub fn unescape_char(src: &str) -> Result<char, EscapeError> {
201207
///
202208
/// Takes the contents of a byte literal (without quotes),
203209
/// and returns an unescaped byte or an error.
210+
#[inline]
204211
pub fn unescape_byte(src: &str) -> Result<u8, EscapeError> {
205212
<[u8]>::unescape_single(&mut src.chars())
206213
}
@@ -258,12 +265,14 @@ pub enum MixedUnit {
258265
}
259266

260267
impl From<char> for MixedUnit {
268+
#[inline]
261269
fn from(c: char) -> Self {
262270
MixedUnit::Char(c)
263271
}
264272
}
265273

266274
impl From<u8> for MixedUnit {
275+
#[inline]
267276
fn from(n: u8) -> Self {
268277
if n.is_ascii() {
269278
MixedUnit::Char(n as char)
@@ -364,6 +373,7 @@ trait Unescape {
364373
/// Interpret a non-nul ASCII escape
365374
///
366375
/// Parses the character of an ASCII escape (except nul) without the leading backslash.
376+
#[inline] // single use in Unescape::unescape_1
367377
fn simple_escape(c: char) -> Result<u8, char> {
368378
// Previous character was '\\', unescape what follows.
369379
Ok(match c {
@@ -380,6 +390,7 @@ fn simple_escape(c: char) -> Result<u8, char> {
380390
/// Interpret a hexadecimal escape
381391
///
382392
/// Parses the two hexadecimal characters of a hexadecimal escape without the leading r"\x".
393+
#[inline] // single use in Unescape::unescape_1
383394
fn hex_escape(chars: &mut impl Iterator<Item = char>) -> Result<u8, EscapeError> {
384395
let hi = chars.next().ok_or(EscapeError::TooShortHexEscape)?;
385396
let hi = hi.to_digit(16).ok_or(EscapeError::InvalidCharInHexEscape)?;
@@ -394,6 +405,7 @@ fn hex_escape(chars: &mut impl Iterator<Item = char>) -> Result<u8, EscapeError>
394405
///
395406
/// Parse the braces with hexadecimal characters (and underscores) part of a unicode escape.
396407
/// This r"{...}" normally comes after r"\u" and cannot start with an underscore.
408+
#[inline] // single use in Unescape::unescape_1
397409
fn unicode_escape(chars: &mut impl Iterator<Item = char>) -> Result<u32, EscapeError> {
398410
if chars.next() != Some('{') {
399411
return Err(EscapeError::NoBraceInUnicodeEscape);
@@ -444,10 +456,12 @@ fn unicode_escape(chars: &mut impl Iterator<Item = char>) -> Result<u32, EscapeE
444456
/// Skip ASCII whitespace, except for the formfeed character
445457
/// (see [this issue](https://github.com/rust-lang/rust/issues/136600)).
446458
/// 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-
{
459+
#[inline] // single use in Unescape::unescape
460+
fn skip_ascii_whitespace(
461+
chars: &mut Chars<'_>,
462+
start: usize,
463+
mut callback: impl FnMut(Range<usize>, EscapeError),
464+
) {
451465
let rest = chars.as_str();
452466
let first_non_space = rest
453467
.bytes()
@@ -476,10 +490,12 @@ impl Unescape for str {
476490

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

493+
#[inline]
479494
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
480495
Ok(c)
481496
}
482497

498+
#[inline]
483499
fn hex2unit(b: u8) -> Result<Self::Unit, EscapeError> {
484500
if b.is_ascii() {
485501
Ok(b as char)
@@ -488,7 +504,7 @@ impl Unescape for str {
488504
}
489505
}
490506

491-
/// Converts the result of a unicode escape to the unit type
507+
#[inline]
492508
fn unicode2unit(r: Result<char, EscapeError>) -> Result<Self::Unit, EscapeError> {
493509
r
494510
}
@@ -499,15 +515,17 @@ impl Unescape for [u8] {
499515

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

518+
#[inline]
502519
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
503520
char2byte(c)
504521
}
505522

523+
#[inline]
506524
fn hex2unit(b: u8) -> Result<Self::Unit, EscapeError> {
507525
Ok(b)
508526
}
509527

510-
/// Converts the result of a unicode escape to the unit type
528+
#[inline]
511529
fn unicode2unit(_r: Result<char, EscapeError>) -> Result<Self::Unit, EscapeError> {
512530
Err(EscapeError::UnicodeEscapeInByte)
513531
}
@@ -518,6 +536,7 @@ impl Unescape for CStr {
518536

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

539+
#[inline]
521540
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
522541
if c == '\0' {
523542
Err(EscapeError::NulInCStr)
@@ -526,6 +545,7 @@ impl Unescape for CStr {
526545
}
527546
}
528547

548+
#[inline]
529549
fn hex2unit(byte: u8) -> Result<Self::Unit, EscapeError> {
530550
if byte == b'\0' {
531551
Err(EscapeError::NulInCStr)
@@ -536,7 +556,7 @@ impl Unescape for CStr {
536556
}
537557
}
538558

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

0 commit comments

Comments
 (0)