[][src]Struct hashable_rc::HashableRc

pub struct HashableRc<T> { /* fields omitted */ }

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

impl<T> HashableRc<T>[src]

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

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

Trait Implementations

impl<T: Debug> Debug for HashableRc<T>[src]

impl<T: Eq> Eq for HashableRc<T>[src]

impl<T> Hash for HashableRc<T>[src]

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

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.

impl<T> PartialEq<HashableRc<T>> for HashableRc<T>[src]

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

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> StructuralEq for HashableRc<T>[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for HashableRc<T>

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.