Struct internment::Intern
source · pub struct Intern<T: 'static + ?Sized> { /* private fields */ }
Expand description
A pointer to an interned object
An Intern
points to an object that has been leaked and may be used in any
thread without locking.
Implementations§
source§impl<T: Eq + Hash + Send + Sync + 'static> Intern<T>
impl<T: Eq + Hash + Send + Sync + 'static> Intern<T>
sourcepub fn new(val: T) -> Intern<T>
pub fn new(val: T) -> Intern<T>
Intern a value.
If this value has not previously been interned, then new
will allocate
a spot for the value on the heap. Otherwise, it will return a pointer
to the object previously allocated.
Note that Intern::new
is a bit slow, since it needs to check a
HashSet
protected by a Mutex
.
source§impl<T: Eq + Hash + Send + Sync + 'static + ?Sized> Intern<T>
impl<T: Eq + Hash + Send + Sync + 'static + ?Sized> Intern<T>
sourcepub fn as_ref(self) -> &'static T
pub fn as_ref(self) -> &'static T
Get a long-lived reference to the data pointed to by an Intern
, which
is never freed from the intern pool.
sourcepub fn num_objects_interned() -> usize
pub fn num_objects_interned() -> usize
See how many objects have been interned. This may be helpful in analyzing memory use.
Trait Implementations§
source§impl<'de, T: Eq + Hash + Send + Sync + 'static + Deserialize<'de>> Deserialize<'de> for Intern<T>
impl<'de, T: Eq + Hash + Send + Sync + 'static + Deserialize<'de>> Deserialize<'de> for Intern<T>
source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
source§impl<T: Eq + Hash + Send + Sync + 'static + Copy, const N: usize> From<&[T; N]> for Intern<[T]>
impl<T: Eq + Hash + Send + Sync + 'static + Copy, const N: usize> From<&[T; N]> for Intern<[T]>
source§impl<T: Eq + Hash + Send + Sync + ?Sized> Hash for Intern<T>
impl<T: Eq + Hash + Send + Sync + ?Sized> Hash for Intern<T>
The hash implementation returns the hash of the pointer value, not the hash of the value pointed to. This should be irrelevant, since there is a unique pointer for every value, but it is observable, since you could compare the hash of the pointer with hash of the data itself.
source§impl<T: Eq + Hash + Send + Sync + Ord + ?Sized> Ord for Intern<T>
impl<T: Eq + Hash + Send + Sync + Ord + ?Sized> Ord for Intern<T>
source§impl<T: Eq + Hash + Send + Sync + ?Sized> PartialEq<Intern<T>> for Intern<T>
impl<T: Eq + Hash + Send + Sync + ?Sized> PartialEq<Intern<T>> for Intern<T>
source§impl<T: Eq + Hash + Send + Sync + PartialOrd + ?Sized> PartialOrd<Intern<T>> for Intern<T>
impl<T: Eq + Hash + Send + Sync + PartialOrd + ?Sized> PartialOrd<Intern<T>> for Intern<T>
impl<T: ?Sized> Copy for Intern<T>
An Intern
is Copy
, which is unusal for a pointer. This is safe
because we never free the data pointed to by an Intern
.