Struct internment::LocalIntern [−][src]
A pointer to a thread-local interned object.
The interned object will be held in memory as long as the thread
is still running. Thus you can arrange a crude sort of arena
allocation by running code using LocalIntern
on a temporary
thread. Lifetime issues are as simple as when using Intern
.
LocalIntern
differs in that it is neigher Send
nor Share
, so
it cannot be used in a multithreaded manner. On the benefit side,
it is faster than Intern
, and the memory can be freed (by
running in a temporary thread).
Example
use internment::LocalIntern; let x = LocalIntern::new("hello"); let y = LocalIntern::new("world"); assert_ne!(x, y); assert_eq!(x, LocalIntern::new("hello")); assert_eq!(*x, "hello"); // dereference a LocalIntern like a pointer
Example with owned String
data
use internment::LocalIntern; let x = LocalIntern::new("hello".to_string()); let y = LocalIntern::<String>::from("world"); assert_ne!(x, y); assert_eq!(x, LocalIntern::from("hello")); assert_eq!(&*x, "hello"); // dereference a LocalIntern like a pointer
Implementations
impl<T: Eq + Hash + 'static> LocalIntern<T>
[src]
pub fn new(val: T) -> LocalIntern<T>
[src]
Intern a value in a thread-local way. 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 LocalIntern::new
is a bit slow, since it needs to check
a HashMap
protected by a Mutex
.
pub fn from<'a, Q: ?Sized + Eq + Hash + 'a>(val: &'a Q) -> LocalIntern<T> where
T: Borrow<Q> + From<&'a Q>,
[src]
T: Borrow<Q> + From<&'a Q>,
Intern a value from a reference in a thread-local way (fastest).
If this value has not previously been
interned, then new
will allocate a spot for the value on the
heap and generate that value using T::from(val)
.
pub fn num_objects_interned() -> usize
[src]
See how many objects have been interned. This may be helpful in analyzing memory use.
Trait Implementations
impl<T> AsRef<T> for LocalIntern<T>
[src]
impl<T> Borrow<T> for LocalIntern<T>
[src]
impl<T> Clone for LocalIntern<T>
[src]
fn clone(&self) -> Self
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<T> Copy for LocalIntern<T>
[src]
An LocalIntern
is Copy
, which is unusal for a pointer. This
is safe because we never free the data pointed to by an
LocalIntern
until the thread itself is destroyed.
impl<T: Debug> Debug for LocalIntern<T>
[src]
impl<T: Eq + Hash + Default + 'static> Default for LocalIntern<T>
[src]
fn default() -> LocalIntern<T>
[src]
impl<T> Deref for LocalIntern<T>
[src]
impl<T: Display> Display for LocalIntern<T>
[src]
impl<T> Eq for LocalIntern<T>
[src]
impl<T: Eq + Hash + 'static> From<T> for LocalIntern<T>
[src]
impl<T> Hash for LocalIntern<T>
[src]
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.
fn hash<H: Hasher>(&self, state: &mut H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<T: Ord> Ord for LocalIntern<T>
[src]
fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl<T> PartialEq<LocalIntern<T>> for LocalIntern<T>
[src]
fn eq(&self, other: &LocalIntern<T>) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<T: PartialOrd> PartialOrd<LocalIntern<T>> for LocalIntern<T>
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
fn lt(&self, other: &Self) -> bool
[src]
fn le(&self, other: &Self) -> bool
[src]
fn gt(&self, other: &Self) -> bool
[src]
fn ge(&self, other: &Self) -> bool
[src]
impl<T> Pointer for LocalIntern<T>
[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for LocalIntern<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> !Send for LocalIntern<T>
impl<T> !Sync for LocalIntern<T>
impl<T> Unpin for LocalIntern<T>
impl<T> UnwindSafe for LocalIntern<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> CallHasher for T where
T: Hash,
T: Hash,
impl<T> From<!> for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,