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>
impl<T> HashableRc<T>
Sourcepub fn new(value: Rc<T>) -> Self
pub fn new(value: Rc<T>) -> Self
Constructs a new HashableRc<T>
wrapping an
Rc<T>.
Sourcepub fn get_cloned(&self) -> Rc<T>
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>
impl<T: Debug> Debug for HashableRc<T>
Source§impl<T> Hash for HashableRc<T>
impl<T> Hash for HashableRc<T>
Source§impl<T> PartialEq for HashableRc<T>
impl<T> PartialEq for HashableRc<T>
Source§fn eq(&self, other: &Self) -> bool
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.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more