Skip to content

Commit

Permalink
Inline/cold
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed May 30, 2021
1 parent 8797f81 commit 96b2d24
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 53 deletions.
11 changes: 9 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl ErrorCode {
}

/// Helper function for the library
#[inline]
pub fn to_result(self) -> Result<(), Error> {
match NonZeroU32::new(self.0) {
None => Ok(()),
Expand All @@ -29,14 +30,14 @@ impl ErrorCode {

impl Error {
/// Panics if the code is 0
#[inline(always)]
#[cold]
pub fn new(code: u32) -> Self {
Self(NonZeroU32::new(code).unwrap())
}
}

impl From<ErrorCode> for Result<(), Error> {
#[inline(always)]
#[cold]
fn from(err: ErrorCode) -> Self {
err.to_result()
}
Expand All @@ -50,18 +51,21 @@ impl From<Error> for ErrorCode {
}

impl fmt::Debug for Error {
#[cold]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", ErrorCode(self.0.get()).as_str(), self.0)
}
}

impl fmt::Debug for ErrorCode {
#[cold]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", self.as_str(), self.0)
}
}

impl fmt::Display for Error {
#[cold]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(ErrorCode(self.0.get()).as_str())
}
Expand All @@ -72,6 +76,7 @@ impl error::Error for Error {

#[doc(hidden)]
impl std::convert::From<io::Error> for Error {
#[cold]
fn from(err: io::Error) -> Error {
match err.kind() {
io::ErrorKind::NotFound | io::ErrorKind::UnexpectedEof => Error::new(78),
Expand All @@ -85,6 +90,7 @@ This returns the description of a numerical error code in English. This is also
the documentation of all the error codes.
*/
impl ErrorCode {
#[cold]
pub fn c_description(&self) -> &'static [u8] {
match self.0 {
0 => "no error, everything went ok\0",
Expand Down Expand Up @@ -196,6 +202,7 @@ impl ErrorCode {
}

impl From<fallible_collections::TryReserveError> for Error {
#[cold]
fn from(_: fallible_collections::TryReserveError) -> Self {
Self(NonZeroU32::new(83).unwrap())
}
Expand Down
3 changes: 3 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub struct ColorMode {
}

impl fmt::Debug for ColorMode {
#[cold]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut s = f.debug_struct("ColorMode");
s.field("colortype", &self.colortype);
Expand Down Expand Up @@ -417,6 +418,7 @@ pub struct ColorProfile {
}

impl fmt::Debug for ColorProfile {
#[cold]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ColorProfile")
.field("colored", &self.colored)
Expand All @@ -433,6 +435,7 @@ impl fmt::Debug for ColorProfile {

impl fmt::Debug for CompressSettings {
#[allow(deprecated)]
#[cold]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = f.debug_struct("CompressSettings");
s.field("minmatch", &self.minmatch);
Expand Down
5 changes: 5 additions & 0 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct TextKeysIter<'a> {
impl<'a> Iterator for TextKeysIter<'a> {
/// key value
type Item = (&'a [u8], &'a [u8]);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if let Some((first, rest)) = self.s.split_first() {
self.s = rest;
Expand All @@ -32,6 +33,7 @@ pub struct ITextKeysIter<'a> {
impl<'a> Iterator for ITextKeysIter<'a> {
/// key langtag transkey value
type Item = (&'a str, &'a str, &'a str, &'a str);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if let Some((first, rest)) = self.s.split_first() {
self.s = rest;
Expand All @@ -52,6 +54,7 @@ pub struct ChunksIter<'a> {
}

impl<'a> ChunksIter<'a> {
#[inline(always)]
pub fn new(data: &'a [u8]) -> Self {
Self {
iter: ChunksIterFallible {
Expand All @@ -63,6 +66,7 @@ impl<'a> ChunksIter<'a> {

impl<'a> Iterator for ChunksIter<'a> {
type Item = ChunkRef<'a>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().and_then(|item| item.ok())
}
Expand All @@ -75,6 +79,7 @@ pub struct ChunksIterFallible<'a> {
impl<'a> Iterator for ChunksIterFallible<'a> {
type Item = Result<ChunkRef<'a>, Error>;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.data.is_empty() {
return None;
Expand Down
Loading

0 comments on commit 96b2d24

Please sign in to comment.