Skip to content

Commit c0b0f2e

Browse files
committed
Optimize PartialEq impls for Ident
1 parent aeae459 commit c0b0f2e

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ impl Ident {
822822

823823
impl PartialEq for Ident {
824824
fn eq(&self, other: &Ident) -> bool {
825-
self.to_string() == other.to_string()
825+
self.inner == other.inner
826826
}
827827
}
828828

@@ -831,7 +831,7 @@ where
831831
T: ?Sized + AsRef<str>,
832832
{
833833
fn eq(&self, other: &T) -> bool {
834-
self.to_string() == other.as_ref()
834+
self.inner == other
835835
}
836836
}
837837

src/stable.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,26 @@ fn validate_term(string: &str) {
500500
}
501501
}
502502

503+
impl PartialEq for Ident {
504+
fn eq(&self, other: &Ident) -> bool {
505+
self.sym == other.sym && self.raw == other.raw
506+
}
507+
}
508+
509+
impl<T> PartialEq<T> for Ident
510+
where
511+
T: ?Sized + AsRef<str>,
512+
{
513+
fn eq(&self, other: &T) -> bool {
514+
let other = other.as_ref();
515+
if self.raw {
516+
other.starts_with("r#") && self.sym == other[2..]
517+
} else {
518+
self.sym == other
519+
}
520+
}
521+
}
522+
503523
impl fmt::Display for Ident {
504524
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
505525
if self.raw {

src/unstable.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,29 @@ impl Ident {
573573
}
574574
}
575575

576+
impl PartialEq for Ident {
577+
fn eq(&self, other: &Ident) -> bool {
578+
match (self, other) {
579+
(Ident::Nightly(t), Ident::Nightly(o)) => t.to_string() == o.to_string(),
580+
(Ident::Stable(t), Ident::Stable(o)) => t == o,
581+
_ => mismatch(),
582+
}
583+
}
584+
}
585+
586+
impl<T> PartialEq<T> for Ident
587+
where
588+
T: ?Sized + AsRef<str>,
589+
{
590+
fn eq(&self, other: &T) -> bool {
591+
let other = other.as_ref();
592+
match self {
593+
Ident::Nightly(t) => t.to_string() == other,
594+
Ident::Stable(t) => t == other,
595+
}
596+
}
597+
}
598+
576599
impl fmt::Display for Ident {
577600
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
578601
match self {

0 commit comments

Comments
 (0)