pub struct Intern<T: 'static + ?Sized> { /* private fields */ }
Expand description
A pointer to an interned object.
The interned object will be held in memory indefinitely. On the
plus side, this means that lifetime issues are simple when using
Intern
.
§Example
use internment::Intern;
let x = Intern::new("hello");
let y = Intern::new("world");
assert_ne!(x, y);
assert_eq!(x, Intern::new("hello"));
assert_eq!(*x, "hello"); // dereference an Intern like a pointer
§Example with owned String
data
use internment::Intern;
let x = Intern::new("hello".to_string());
let y = Intern::<String>::from_ref("world");
assert_ne!(x, y);
assert_eq!(x, Intern::from_ref("hello"));
assert_eq!(y, Intern::from_ref("world"));
assert_eq!(&*x, "hello"); // dereference a Intern like a pointer
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.
Sourcepub fn benchmarking_only_clear_interns()
pub fn benchmarking_only_clear_interns()
Only for benchmarking, this will cause problems
Sourcepub fn is_interned<'a, Q: ?Sized + Eq + Hash + 'a>(val: &'a Q) -> boolwhere
T: Borrow<Q>,
pub fn is_interned<'a, Q: ?Sized + Eq + Hash + 'a>(val: &'a Q) -> boolwhere
T: Borrow<Q>,
Check if a value already is interned.
If this value has previously been interned, return true, else returns false/// Checking if an object is already interned
use internment::Intern;
assert!(!Intern::<String>::is_interned("Fortunato"));
let x = Intern::new("Fortunato".to_string());
assert!(Intern::<String>::is_interned("Fortunato"));
assert!(!Intern::<str>::is_interned("Fortunato"));
let x: Intern<str> = "Fortunato".into();
assert!(Intern::<str>::is_interned("Fortunato"));
Trait Implementations§
Source§impl<T: 'static + ?Sized> DeepSizeOf for Intern<T>
impl<T: 'static + ?Sized> DeepSizeOf for Intern<T>
Source§fn deep_size_of_children(&self, _context: &mut Context) -> usize
fn deep_size_of_children(&self, _context: &mut Context) -> usize
Source§fn deep_size_of(&self) -> usize
fn deep_size_of(&self) -> usize
Source§impl<'de, T: Eq + Hash + Send + Sync + ?Sized + 'static + Deserialize<'de>> Deserialize<'de> for Intern<T>
Available on crate feature serde
only.
impl<'de, T: Eq + Hash + Send + Sync + ?Sized + 'static + Deserialize<'de>> Deserialize<'de> for Intern<T>
serde
only.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: Debug> Fits64 for Intern<T>
Available on crate feature tinyset
only.The Fits64
implementation for Intern<T>
is designed to normally give
(relatively) small numbers, by XORing with a fixed pointer that is also on
the heap. The pointer is also divided by its alignment to eliminate those
redundant insignificant zeros. This should make the most significant bits
of the resulting u64 be zero, which will mean that Set64
(which is
space-efficient in storing small integers) can store this result in far
fewer than 8 bytes.
impl<T: Debug> Fits64 for Intern<T>
tinyset
only.The Fits64
implementation for Intern<T>
is designed to normally give
(relatively) small numbers, by XORing with a fixed pointer that is also on
the heap. The pointer is also divided by its alignment to eliminate those
redundant insignificant zeros. This should make the most significant bits
of the resulting u64 be zero, which will mean that Set64
(which is
space-efficient in storing small integers) can store this result in far
fewer than 8 bytes.
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>
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.
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 + PartialOrd + ?Sized> PartialOrd for Intern<T>
impl<T: Eq + Hash + Send + Sync + PartialOrd + ?Sized> PartialOrd for Intern<T>
Source§impl<T: Eq + Hash + Send + Sync + Serialize + ?Sized> Serialize for Intern<T>
Available on crate feature serde
only.
impl<T: Eq + Hash + Send + Sync + Serialize + ?Sized> Serialize for Intern<T>
serde
only.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
.
impl<T: Eq + Hash + Send + Sync + ?Sized> Eq for Intern<T>
Auto Trait Implementations§
impl<T> Freeze for Intern<T>where
T: ?Sized,
impl<T> RefUnwindSafe for Intern<T>where
T: RefUnwindSafe + ?Sized,
impl<T> Send for Intern<T>
impl<T> Sync for Intern<T>
impl<T> Unpin for Intern<T>where
T: ?Sized,
impl<T> UnwindSafe for Intern<T>where
T: RefUnwindSafe + ?Sized,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.