pointer-identity

Crates.iopointer-identity
lib.rspointer-identity
version0.1.1
sourcesrc
created_at2023-11-17 16:24:28.909132
updated_at2023-11-21 08:03:59.291171
descriptionWrapper type to treat the pointer address of types as identity rather than the value
homepage
repositoryhttps://github.com/xfbs/pointer-identity
max_upload_size
id1039218
size17,916
Patrick Elsen (xfbs)

documentation

README

Pointer Identity

This crate gives you a wrapper type that you can use to change how your values are compared and hashed.

Identity

Rust has some traits that operate on values:

  • Ord and PartialOrd check the ordering of values,
  • Eq and PartialEq check equality of values,
  • Hash computes a hash sum of values.

These work on the values of the data. For example, two strings "Hello" and "Hello" will compute as being identical, even if they exist in different places in memory:

let left = String::from("Hello");
let right = String::from("Hello");
assert_eq!(left, right);

When using smart pointers such as Arc, Rc and Box in Rust, these will forward the implementation to the underlying types. Generally speaking, this makes sense and is the intended behavior.

However, in some cases this might not be what you need. For example, in some cases where you have a cache of reference-counted values, you may want it to determine if two values are the same just by looking at their pointer address rather than inspecting the value.

This crate lets you achieve that by offering a wrapper type, PointerIdentity, that you can use to wrap any value which implements Pointer to get it to use the pointer address as identity for comparisons and hashing rather than the value of the data it is holding.

License

MIT.

Commit count: 10

cargo fmt