HashableRc

Struct HashableRc 

Source
pub struct HashableRc<T> { /* private fields */ }
Expand description

A hashable wrapper around the Rc<T> type.

A hash of a HashableRc<T> is taken from the underlying pointer. Therefore, two separate objects that may be considered equal will, when contained in a HashableRc<T>, almost always have different hashed values. For example, the following holds:

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::rc::Rc;

use hashable_rc::HashableRc;

fn hash<H: Hash>(value: &H) -> u64 {
    let mut state = DefaultHasher::new();
    value.hash(&mut state);
    state.finish()
}

// While the underlying values are considered equal, the hash values
// will be different, due to being separate allocated objects with 
// different underlying pointers.
let rc1 = Rc::new(42);
let rc2 = Rc::new(42);
 
// The hashes of the two reference countings are different.
assert_ne!(hash(&HashableRc::new(rc1.clone())), 
           hash(&HashableRc::new(rc2.clone())));

// Contrastingly, the hashes of clone reference countings pointing to 
// the same object are the equal.
assert_eq!(hash(&HashableRc::new(rc1.clone())), 
           hash(&HashableRc::new(rc1.clone())));

Similarly, equality of HashableRc<T> objects is done by evaluating pointer equality (using ptr_eq()). The equality is not from the value itself, but from the pointer.

use std::rc::Rc;

use hashable_rc::HashableRc;

// Again, the values contained are equal, but the underlying pointers
// are different.
let rc1 = Rc::new(42);
let rc2 = Rc::new(42);

// Therefore, two HashableRc wrappers containing these reference 
// counters are not equal.
assert_ne!(HashableRc::new(rc1.clone()),
           HashableRc::new(rc2.clone()));

// But HashableRc holding the same underlying object are equal.
assert_eq!(HashableRc::new(rc1.clone()),
           HashableRc::new(rc1.clone()));

Implementations§

Source§

impl<T> HashableRc<T>

Source

pub fn new(value: Rc<T>) -> Self

Constructs a new HashableRc<T> wrapping an Rc<T>.

Source

pub fn get_cloned(&self) -> Rc<T>

Returns a clone of the wrapped Rc<T> without consuming self.

Trait Implementations§

Source§

impl<T: Debug> Debug for HashableRc<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Hash for HashableRc<T>

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Generate a hash value for the HashableRc<T>.

This hash value is based on the underlying pointer. Two unique objects will most likely have different hashes, even if their values are the same.

1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> PartialEq for HashableRc<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality for two HashableRc<T> objects.

Equality is determined by pointer equality, rather than value equality. Objects are only considered equal if they point to the same object.

1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for HashableRc<T>

Auto Trait Implementations§

§

impl<T> Freeze for HashableRc<T>

§

impl<T> RefUnwindSafe for HashableRc<T>
where T: RefUnwindSafe,

§

impl<T> !Send for HashableRc<T>

§

impl<T> !Sync for HashableRc<T>

§

impl<T> Unpin for HashableRc<T>

§

impl<T> UnwindSafe for HashableRc<T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.