Skip to content

Commit

Permalink
Add pointer equality comparison for EcoString
Browse files Browse the repository at this point in the history
Fixes #35
  • Loading branch information
laurmaedje committed Nov 27, 2023
1 parent ed469f8 commit fa87e6f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ std = []
serde = { version = "1.0", optional = true, default-features = false }

[target.'cfg(loom)'.dependencies]
loom = { version = "0.5", optional = true }
loom = { version = "0.7", optional = true }
17 changes: 17 additions & 0 deletions src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ impl DynamicVec {
}
}

impl Eq for DynamicVec {}

impl PartialEq for DynamicVec {
#[inline]
fn eq(&self, other: &Self) -> bool {
if let (Variant::Spilled(a), Variant::Spilled(b)) =
(self.variant(), other.variant())
{
if EcoVec::ptr_eq(a, b) {
return true;
}
}

self.as_slice() == other.as_slice()
}
}

impl Clone for DynamicVec {
#[inline]
fn clone(&self) -> Self {
Expand Down
11 changes: 1 addition & 10 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ macro_rules! eco_format {
/// The above holds true for normal 32-bit or 64-bit little endian systems. On
/// 64-bit big-endian systems, the type's size increases to 24 bytes and the
/// amount of inline storage to 23 bytes.
#[derive(Clone)]
#[derive(Clone, Eq, PartialEq)]
pub struct EcoString(DynamicVec);

impl EcoString {
Expand Down Expand Up @@ -281,15 +281,6 @@ impl Display for EcoString {
}
}

impl Eq for EcoString {}

impl PartialEq for EcoString {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_str().eq(other.as_str())
}
}

impl PartialEq<str> for EcoString {
#[inline]
fn eq(&self, other: &str) -> bool {
Expand Down
7 changes: 7 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ impl<T> EcoVec<T> {
ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.data_mut(), prev));
}
}

/// Whether the two vectors point to the same underlying allocation.
///
/// Also returns `true` if both are empty and unallocated.
pub fn ptr_eq(a: &Self, b: &Self) -> bool {
std::ptr::eq(a.ptr.as_ptr(), b.ptr.as_ptr())
}
}

impl<T: Clone> EcoVec<T> {
Expand Down
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ fn test_str_push() {
v.push('a');
v.push('b');
v.push_str("cd😀");
assert_eq!(v, v);
assert_eq!(v, "abcd😀");
assert_eq!(v.len(), 8);

Expand Down

0 comments on commit fa87e6f

Please sign in to comment.