Struct internment::ArenaIntern
source · pub struct ArenaIntern<'a, T: ?Sized> { /* private fields */ }
Expand description
An interned object reference with the data stored in an Arena<T>
.
Implementations§
source§impl<'a, T: ?Sized> ArenaIntern<'a, T>
impl<'a, T: ?Sized> ArenaIntern<'a, T>
sourcepub fn into_ref(self) -> &'a T
pub fn into_ref(self) -> &'a T
Get a reference to a value interned into an arena.
This function allows you to store values into a structure
inline, without having to take a &'a
reference to an
ArenaIntern<'a, T>
. This is required as using
std::ops::Deref
or std::convert::AsRef
requires a &self
receiver, but doing so, due to the bounds
of these traits’ functions, would implicitly require that
this reference lives for 'a
.
§Example
Consider the following structures.
struct Bar {
baz: String,
}
struct Foo<'a>(ArenaIntern<'a, Bar>);
The following code does not compile.
impl<'a> Foo<'a> {
pub fn get_baz(self) -> &'a str {
&self.0.as_ref().baz
// ^^^ ERROR: cannot return value referencing local data `self.0`
}
}
This similar code, which uses into_ref
, does compile.
impl<'a> Foo<'a> {
pub fn get_baz(self) -> &'a str {
&self.0.into_ref().baz
}
}
Trait Implementations§
source§impl<'a, T: ?Sized> AsRef<T> for ArenaIntern<'a, T>
impl<'a, T: ?Sized> AsRef<T> for ArenaIntern<'a, T>
source§impl<'a, T: ?Sized> Clone for ArenaIntern<'a, T>
impl<'a, T: ?Sized> Clone for ArenaIntern<'a, T>
source§impl<'a, T: ?Sized + DeepSizeOf> DeepSizeOf for ArenaIntern<'a, T>
impl<'a, T: ?Sized + DeepSizeOf> DeepSizeOf for ArenaIntern<'a, 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<'a, T: ?Sized> Deref for ArenaIntern<'a, T>
impl<'a, T: ?Sized> Deref for ArenaIntern<'a, T>
source§impl<'a, T: ?Sized> Hash for ArenaIntern<'a, T>
impl<'a, T: ?Sized> Hash for ArenaIntern<'a, 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.