Skip to content

Commit

Permalink
Merge pull request #40 from zbraniecki/inline
Browse files Browse the repository at this point in the history
Inline one-line perf sensitive traits
  • Loading branch information
zbraniecki authored Apr 11, 2021
2 parents 045568d + 5914741 commit 36865a4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/tinystr16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ impl TinyStr16 {
}

impl fmt::Display for TinyStr16 {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.deref())
}
Expand Down Expand Up @@ -297,20 +298,23 @@ impl Deref for TinyStr16 {
}

impl PartialEq<&str> for TinyStr16 {
#[inline(always)]
fn eq(&self, other: &&str) -> bool {
self.deref() == *other
}
}

impl PartialOrd for TinyStr16 {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for TinyStr16 {
#[inline(always)]
fn cmp(&self, other: &Self) -> Ordering {
self.0.get().to_be().cmp(&other.0.get().to_be())
self.0.get().to_le().cmp(&other.0.get().to_le()).reverse()
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/tinystr4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ impl TinyStr4 {
}

impl fmt::Display for TinyStr4 {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.deref())
}
Expand Down Expand Up @@ -279,20 +280,23 @@ impl Deref for TinyStr4 {
}

impl PartialEq<&str> for TinyStr4 {
#[inline(always)]
fn eq(&self, other: &&str) -> bool {
self.deref() == *other
}
}

impl PartialOrd for TinyStr4 {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for TinyStr4 {
#[inline(always)]
fn cmp(&self, other: &Self) -> Ordering {
self.0.get().to_be().cmp(&other.0.get().to_be())
self.0.get().to_le().cmp(&other.0.get().to_le()).reverse()
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/tinystr8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ impl TinyStr8 {
}

impl fmt::Display for TinyStr8 {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.deref())
}
Expand Down Expand Up @@ -290,20 +291,23 @@ impl Deref for TinyStr8 {
}

impl PartialEq<&str> for TinyStr8 {
#[inline(always)]
fn eq(&self, other: &&str) -> bool {
self.deref() == *other
}
}

impl PartialOrd for TinyStr8 {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for TinyStr8 {
#[inline(always)]
fn cmp(&self, other: &Self) -> Ordering {
self.0.get().to_be().cmp(&other.0.get().to_be())
self.0.get().to_le().cmp(&other.0.get().to_le()).reverse()
}
}

Expand Down
18 changes: 7 additions & 11 deletions src/tinystrauto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum TinyStrAuto {
}

impl fmt::Display for TinyStrAuto {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.deref().fmt(f)
}
Expand All @@ -43,6 +44,7 @@ impl fmt::Display for TinyStrAuto {
impl Deref for TinyStrAuto {
type Target = str;

#[inline(always)]
fn deref(&self) -> &str {
use TinyStrAuto::*;
match self {
Expand All @@ -53,6 +55,7 @@ impl Deref for TinyStrAuto {
}

impl PartialEq<&str> for TinyStrAuto {
#[inline(always)]
fn eq(&self, other: &&str) -> bool {
self.deref() == *other
}
Expand All @@ -63,18 +66,11 @@ impl FromStr for TinyStrAuto {

fn from_str(text: &str) -> Result<Self, Self::Err> {
if text.len() <= 16 {
match TinyStr16::from_str(text) {
Ok(result) => Ok(TinyStrAuto::Tiny(result)),
Err(err) => Err(err),
}
TinyStr16::from_str(text).map(TinyStrAuto::Tiny)
} else if text.is_ascii() {
Ok(TinyStrAuto::Heap(text.into()))
} else {
if !text.is_ascii() {
return Err(Error::NonAscii);
}
match String::from_str(text) {
Ok(result) => Ok(TinyStrAuto::Heap(result)),
Err(_) => unreachable!(),
}
Err(Error::NonAscii)
}
}
}
Expand Down

0 comments on commit 36865a4

Please sign in to comment.