Skip to content

Commit

Permalink
impl PartialEq and Eq for sync and unsync Gc
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonwramsey committed Nov 9, 2023
1 parent 8e32c78 commit 628265f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
35 changes: 35 additions & 0 deletions dumpster/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,41 @@ impl<T: Collectable + Send + Sync + ?Sized> Deref for Gc<T> {
}
}

impl<T> PartialEq<Gc<T>> for Gc<T>
where
T: Collectable + Send + Sync + ?Sized + PartialEq,
{
/// Test for equality on two `Gc`s.
///
/// Two `Gc`s are equal if their inner values are equal, even if they are stored in different
/// allocations.
/// Because `PartialEq` does not imply reflexivity, and there is no current path for trait
/// specialization, this function does not do a "fast-path" check for reference equality.
/// Therefore, if two `Gc`s point to the same allocation, the implementation of `eq` will still
/// require a direct call to `eq` on the values.
///
/// # Panics
///
/// This function may panic if it is called from within the implementation of `std::ops::Drop`
/// of its owning value, since returning such a reference could cause a use-after-free.
/// It is not guaranteed to panic.
/// Additionally, if this `Gc` is moved out of an allocation during a `Drop` implementation, it
/// could later cause a panic.
/// For further details, refer to the main documentation for `Gc`.
///
/// ```
/// use dumpster::sync::Gc;
///
/// let gc = Gc::new(6);
/// assert!(gc == Gc::new(6));
/// ```
fn eq(&self, other: &Gc<T>) -> bool {
self.as_ref() == other.as_ref()
}
}

impl<T> Eq for Gc<T> where T: Collectable + Send + Sync + ?Sized + PartialEq {}

impl<T: Collectable + Send + Sync + ?Sized> AsRef<T> for Gc<T> {
fn as_ref(&self) -> &T {
self
Expand Down
37 changes: 37 additions & 0 deletions dumpster/src/unsync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,43 @@ impl<T: Collectable + ?Sized> Drop for Gc<T> {
}
}

impl<T> PartialEq<Gc<T>> for Gc<T>
where
T: Collectable + ?Sized + PartialEq,
{
/// Test for equality on two `Gc`s.
///
/// Two `Gc`s are equal if their inner values are equal, even if they are stored in different
/// allocations.
/// Because `PartialEq` does not imply reflexivity, and there is no current path for trait
/// specialization, this function does not do a "fast-path" check for reference equality.
/// Therefore, if two `Gc`s point to the same allocation, the implementation of `eq` will still
/// require a direct call to `eq` on the values.
///
/// # Panics
///
/// This function may panic if it is called from within the implementation of `std::ops::Drop`
/// of its owning value, since returning such a reference could cause a use-after-free.
/// It is not guaranteed to panic.
/// Additionally, if this `Gc` is moved out of an allocation during a `Drop` implementation, it
/// could later cause a panic.
/// For further details, refer to the main documentation for `Gc`.
///
/// # Examples
///
/// ```
/// use dumpster::unsync::Gc;
///
/// let gc = Gc::new(6);
/// assert!(gc == Gc::new(6));
/// ```
fn eq(&self, other: &Gc<T>) -> bool {
self.as_ref() == other.as_ref()
}
}

impl<T> Eq for Gc<T> where T: Collectable + ?Sized + PartialEq {}

impl CollectInfo {
#[must_use]
/// Get the number of times that a [`Gc`] has been dropped since the last time a collection
Expand Down

0 comments on commit 628265f

Please sign in to comment.