Skip to content

Commit 5841236

Browse files
hkBstUrgau
authored andcommitted
add #[inline] to small and single-use functions
1 parent 8f9eee9 commit 5841236

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/lib.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ trait CheckRaw {
154154
impl CheckRaw for str {
155155
type RawUnit = char;
156156

157+
#[inline]
157158
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
158159
Ok(c)
159160
}
@@ -162,12 +163,14 @@ impl CheckRaw for str {
162163
impl CheckRaw for [u8] {
163164
type RawUnit = u8;
164165

166+
#[inline]
165167
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
166168
char2byte(c)
167169
}
168170
}
169171

170172
/// Turn an ascii char into a byte
173+
#[inline]
171174
fn char2byte(c: char) -> Result<u8, EscapeError> {
172175
// do NOT do: c.try_into().ok_or(EscapeError::NonAsciiCharInByte)
173176
if c.is_ascii() {
@@ -180,6 +183,7 @@ fn char2byte(c: char) -> Result<u8, EscapeError> {
180183
impl CheckRaw for CStr {
181184
type RawUnit = char;
182185

186+
#[inline]
183187
fn char2raw_unit(c: char) -> Result<Self::RawUnit, EscapeError> {
184188
if c == '\0' {
185189
Err(EscapeError::NulInCStr)
@@ -193,6 +197,7 @@ impl CheckRaw for CStr {
193197
///
194198
/// Takes the contents of a char literal (without quotes),
195199
/// and returns an unescaped char or an error.
200+
#[inline]
196201
pub fn unescape_char(src: &str) -> Result<char, EscapeError> {
197202
str::unescape_single(&mut src.chars())
198203
}
@@ -201,6 +206,7 @@ pub fn unescape_char(src: &str) -> Result<char, EscapeError> {
201206
///
202207
/// Takes the contents of a byte literal (without quotes),
203208
/// and returns an unescaped byte or an error.
209+
#[inline]
204210
pub fn unescape_byte(src: &str) -> Result<u8, EscapeError> {
205211
<[u8]>::unescape_single(&mut src.chars())
206212
}
@@ -258,12 +264,14 @@ pub enum MixedUnit {
258264
}
259265

260266
impl From<char> for MixedUnit {
267+
#[inline]
261268
fn from(c: char) -> Self {
262269
MixedUnit::Char(c)
263270
}
264271
}
265272

266273
impl From<u8> for MixedUnit {
274+
#[inline]
267275
fn from(n: u8) -> Self {
268276
if n.is_ascii() {
269277
MixedUnit::Char(n as char)
@@ -364,6 +372,7 @@ trait Unescape {
364372
/// Interpret a non-nul ASCII escape
365373
///
366374
/// Parses the character of an ASCII escape (except nul) without the leading backslash.
375+
#[inline] // single use in Unescape::unescape_1
367376
fn simple_escape(c: char) -> Result<u8, char> {
368377
// Previous character was '\\', unescape what follows.
369378
Ok(match c {
@@ -380,6 +389,7 @@ fn simple_escape(c: char) -> Result<u8, char> {
380389
/// Interpret a hexadecimal escape
381390
///
382391
/// Parses the two hexadecimal characters of a hexadecimal escape without the leading r"\x".
392+
#[inline] // single use in Unescape::unescape_1
383393
fn hex_escape(chars: &mut impl Iterator<Item = char>) -> Result<u8, EscapeError> {
384394
let hi = chars.next().ok_or(EscapeError::TooShortHexEscape)?;
385395
let hi = hi.to_digit(16).ok_or(EscapeError::InvalidCharInHexEscape)?;
@@ -394,6 +404,7 @@ fn hex_escape(chars: &mut impl Iterator<Item = char>) -> Result<u8, EscapeError>
394404
///
395405
/// Parse the braces with hexadecimal characters (and underscores) part of a unicode escape.
396406
/// This r"{...}" normally comes after r"\u" and cannot start with an underscore.
407+
#[inline] // single use in Unescape::unescape_1
397408
fn unicode_escape(chars: &mut impl Iterator<Item = char>) -> Result<u32, EscapeError> {
398409
if chars.next() != Some('{') {
399410
return Err(EscapeError::NoBraceInUnicodeEscape);
@@ -444,10 +455,12 @@ fn unicode_escape(chars: &mut impl Iterator<Item = char>) -> Result<u32, EscapeE
444455
/// Skip ASCII whitespace, except for the formfeed character
445456
/// (see [this issue](https://github.com/rust-lang/rust/issues/136600)).
446457
/// 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-
{
458+
#[inline] // single use in Unescape::unescape
459+
fn skip_ascii_whitespace(
460+
chars: &mut Chars<'_>,
461+
start: usize,
462+
mut callback: impl FnMut(Range<usize>, EscapeError),
463+
) {
451464
let rest = chars.as_str();
452465
let first_non_space = rest
453466
.bytes()
@@ -476,10 +489,12 @@ impl Unescape for str {
476489

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

492+
#[inline]
479493
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
480494
Ok(c)
481495
}
482496

497+
#[inline]
483498
fn hex2unit(b: u8) -> Result<Self::Unit, EscapeError> {
484499
if b.is_ascii() {
485500
Ok(b as char)
@@ -488,7 +503,7 @@ impl Unescape for str {
488503
}
489504
}
490505

491-
/// Converts the result of a unicode escape to the unit type
506+
#[inline]
492507
fn unicode2unit(r: Result<char, EscapeError>) -> Result<Self::Unit, EscapeError> {
493508
r
494509
}
@@ -499,15 +514,17 @@ impl Unescape for [u8] {
499514

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

517+
#[inline]
502518
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
503519
char2byte(c)
504520
}
505521

522+
#[inline]
506523
fn hex2unit(b: u8) -> Result<Self::Unit, EscapeError> {
507524
Ok(b)
508525
}
509526

510-
/// Converts the result of a unicode escape to the unit type
527+
#[inline]
511528
fn unicode2unit(_r: Result<char, EscapeError>) -> Result<Self::Unit, EscapeError> {
512529
Err(EscapeError::UnicodeEscapeInByte)
513530
}
@@ -518,6 +535,7 @@ impl Unescape for CStr {
518535

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

538+
#[inline]
521539
fn char2unit(c: char) -> Result<Self::Unit, EscapeError> {
522540
if c == '\0' {
523541
Err(EscapeError::NulInCStr)
@@ -526,6 +544,7 @@ impl Unescape for CStr {
526544
}
527545
}
528546

547+
#[inline]
529548
fn hex2unit(byte: u8) -> Result<Self::Unit, EscapeError> {
530549
if byte == b'\0' {
531550
Err(EscapeError::NulInCStr)
@@ -536,7 +555,7 @@ impl Unescape for CStr {
536555
}
537556
}
538557

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

0 commit comments

Comments
 (0)