pub struct Intern<T>where
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 pointerImplementations§
Source§impl<T> Intern<T>
impl<T> 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> Intern<T>
impl<T> 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 is_interned<'a, Q>(val: &'a Q) -> bool
pub fn is_interned<'a, Q>(val: &'a Q) -> bool
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§
impl<T> Copy for Intern<T>where
T: ?Sized,
An Intern is Copy, which is unusal for a pointer. This is safe
because we never free the data pointed to by an Intern.
Source§impl<'de, T> Deserialize<'de> for Intern<T>
Available on crate feature serde only.
impl<'de, T> Deserialize<'de> for Intern<T>
serde only.Source§fn deserialize<D>(
deserializer: D,
) -> Result<Intern<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Intern<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
impl<T> Eq for Intern<T>
Source§impl<T> 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> 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> Ord for Intern<T>
impl<T> Ord for Intern<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T> PartialOrd for Intern<T>
impl<T> PartialOrd for Intern<T>
Source§impl<T> Serialize for Intern<T>
Available on crate feature serde only.
impl<T> Serialize for Intern<T>
serde only.Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
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> UnsafeUnpin 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more