Struct internment::Arena
source · pub struct Arena<T: ?Sized> { /* private fields */ }
Expand description
A arena for storing interned data
You can use an Arena<T>
to intern data of type T
. This data is then
freed when the Arena
is dropped. An arena can hold some kinds of !Sized
data, such as str
.
§Example
let arena = internment::Arena::<str>::new();
// You can intern a `&str` object.
let x = arena.intern("world");
// You can also intern a `String`, in which case the data will not be copied
// if the value has not yet been interned.
let y = arena.intern_string(format!("hello {}", x));
// Interning a boxed `str` will also never require copying the data.
let v: Box<str> = "hello world".into();
let z = arena.intern_box(v);
// Any comparison of interned values will only need to check that the pointers
// are equal and will thus be fast.
assert_eq!(y, z);
assert!(x != z);
§Another example
use internment::Arena;
let arena: Arena<&'static str> = Arena::new();
let x = arena.intern("hello");
let y = arena.intern("world");
assert_ne!(x, y);
println!("The conventional greeting is '{} {}'", x, y);
Implementations§
source§impl<T: Eq + Hash> Arena<T>
impl<T: Eq + Hash> Arena<T>
sourcepub fn intern(&self, val: T) -> ArenaIntern<'_, T>
pub fn intern(&self, val: T) -> ArenaIntern<'_, T>
Intern a value.
If this value has not previously been interned, then intern
will
allocate a spot for the value on the heap. Otherwise, it will return a
pointer to the object previously allocated.
source§impl Arena<str>
impl Arena<str>
sourcepub fn intern<'a, 'b>(&'a self, val: &'b str) -> ArenaIntern<'a, str>
pub fn intern<'a, 'b>(&'a self, val: &'b str) -> ArenaIntern<'a, str>
Intern a &str
as ArenaIntern<str>
.
If this value has not previously been interned, then intern
will
allocate a spot for the value on the heap. Otherwise, it will return a
pointer to the str
previously allocated.
sourcepub fn intern_string(&self, val: String) -> ArenaIntern<'_, str>
pub fn intern_string(&self, val: String) -> ArenaIntern<'_, str>
Intern a String
as ArenaIntern<str>
.
If this value has not previously been interned, then intern
will save
the provided String
. Otherwise, it will free its input String
and
return a pointer to the str
previously saved.
sourcepub fn intern_box(&self, val: Box<str>) -> ArenaIntern<'_, str>
pub fn intern_box(&self, val: Box<str>) -> ArenaIntern<'_, str>
Intern a Box<str>
as ArenaIntern<str>
.
If this value has not previously been interned, then intern
will save
the provided Box<str>
. Otherwise, it will free its input Box<str>
and return a pointer to the str
previously saved.
source§impl Arena<CStr>
impl Arena<CStr>
sourcepub fn intern<'a, 'b>(&'a self, val: &'b CStr) -> ArenaIntern<'a, CStr>
pub fn intern<'a, 'b>(&'a self, val: &'b CStr) -> ArenaIntern<'a, CStr>
Intern a &CStr
as ArenaIntern<CStr>
.
If this value has not previously been interned, then intern
will
allocate a spot for the value on the heap. Otherwise, it will return a
pointer to the CStr
previously allocated.
§Example
let x = arena.intern(std::ffi::CString::new("hello").unwrap().as_c_str());
let y = arena.intern(std::ffi::CString::new("hello").unwrap().as_c_str());
assert_eq!(x, y);
sourcepub fn intern_cstring(&self, val: CString) -> ArenaIntern<'_, CStr>
pub fn intern_cstring(&self, val: CString) -> ArenaIntern<'_, CStr>
Intern a CString
as ArenaIntern<CStr>
.
If this value has not previously been interned, then intern
will save
the provided CString
. Otherwise, it will free its input CString
and
return a pointer to the CStr
previously saved.
§Example
let x = arena.intern_cstring(std::ffi::CString::new("hello").unwrap());
let y = arena.intern_cstring(std::ffi::CString::new("hello").unwrap());
assert_eq!(x, y);
sourcepub fn intern_box(&self, val: Box<CStr>) -> ArenaIntern<'_, CStr>
pub fn intern_box(&self, val: Box<CStr>) -> ArenaIntern<'_, CStr>
Intern a Box<CStr>
as ArenaIntern<CStr>
.
If this value has not previously been interned, then intern
will save
the provided Box<CSr>
. Otherwise, it will free its input Box<CStr>
and return a pointer to the CStr
previously saved.
§Example
let x = arena.intern_cstring(std::ffi::CString::new("hello").unwrap());
let y = arena.intern_box(std::ffi::CString::new("hello").unwrap().into_boxed_c_str());
assert_eq!(x, y);
source§impl Arena<OsStr>
impl Arena<OsStr>
sourcepub fn intern<'a, 'b>(&'a self, val: &'b OsStr) -> ArenaIntern<'a, OsStr>
pub fn intern<'a, 'b>(&'a self, val: &'b OsStr) -> ArenaIntern<'a, OsStr>
Intern a &OsStr
as ArenaIntern<OsStr>
.
If this value has not previously been interned, then intern
will
allocate a spot for the value on the heap. Otherwise, it will return a
pointer to the OsStr
previously allocated.
§Example
let x = arena.intern(std::ffi::OsStr::new("hello"));
let y = arena.intern(std::ffi::OsStr::new("hello"));
assert_eq!(x, y);
sourcepub fn intern_osstring(&self, val: OsString) -> ArenaIntern<'_, OsStr>
pub fn intern_osstring(&self, val: OsString) -> ArenaIntern<'_, OsStr>
Intern a OsString
as ArenaIntern<OsStr>
.
If this value has not previously been interned, then intern
will save
the provided OsString
. Otherwise, it will free its input OsString
and
return a pointer to the OsStr
previously saved.
§Example
let x = arena.intern_osstring(std::ffi::OsString::from("hello"));
let y = arena.intern_osstring(std::ffi::OsString::from("hello"));
assert_eq!(x, y);
sourcepub fn intern_box(&self, val: Box<OsStr>) -> ArenaIntern<'_, OsStr>
pub fn intern_box(&self, val: Box<OsStr>) -> ArenaIntern<'_, OsStr>
Intern a Box<OsStr>
as ArenaIntern<OsStr>
.
If this value has not previously been interned, then intern
will save
the provided Box<CSr>
. Otherwise, it will free its input Box<OsStr>
and return a pointer to the OsStr
previously saved.
§Example
let x = arena.intern_osstring(std::ffi::OsString::from("hello"));
let y = arena.intern_box(std::ffi::OsString::from("hello").into_boxed_os_str());
assert_eq!(x, y);
source§impl Arena<Path>
impl Arena<Path>
sourcepub fn intern<'a, 'b>(&'a self, val: &'b Path) -> ArenaIntern<'a, Path>
pub fn intern<'a, 'b>(&'a self, val: &'b Path) -> ArenaIntern<'a, Path>
Intern a &Path
as ArenaIntern<Path>
.
If this value has not previously been interned, then intern
will
allocate a spot for the value on the heap. Otherwise, it will return a
pointer to the Path
previously allocated.
§Example
let x = arena.intern(std::path::Path::new("hello"));
let y = arena.intern(std::path::Path::new("hello"));
assert_eq!(x, y);
sourcepub fn intern_pathbuf(&self, val: PathBuf) -> ArenaIntern<'_, Path>
pub fn intern_pathbuf(&self, val: PathBuf) -> ArenaIntern<'_, Path>
Intern a PathBuf
as ArenaIntern<Path>
.
If this value has not previously been interned, then intern
will save
the provided PathBuf
. Otherwise, it will free its input PathBuf
and
return a pointer to the Path
previously saved.
§Example
let x = arena.intern_pathbuf(std::path::PathBuf::from("hello"));
let y = arena.intern_pathbuf(std::path::PathBuf::from("hello"));
assert_eq!(x, y);
sourcepub fn intern_box(&self, val: Box<Path>) -> ArenaIntern<'_, Path>
pub fn intern_box(&self, val: Box<Path>) -> ArenaIntern<'_, Path>
Intern a Box<Path>
as ArenaIntern<Path>
.
If this value has not previously been interned, then intern
will save
the provided Box<CSr>
. Otherwise, it will free its input Box<Path>
and return a pointer to the Path
previously saved.
§Example
let x = arena.intern_pathbuf(std::path::PathBuf::from("hello"));
let y = arena.intern_box(std::path::PathBuf::from("hello").into_boxed_path());
assert_eq!(x, y);
source§impl<T: Eq + Hash + Copy> Arena<[T]>
impl<T: Eq + Hash + Copy> Arena<[T]>
sourcepub fn intern<'a, 'b>(&'a self, val: &'b [T]) -> ArenaIntern<'a, [T]>
pub fn intern<'a, 'b>(&'a self, val: &'b [T]) -> ArenaIntern<'a, [T]>
Intern a &\[T\]
as ArenaIntern<[T]>
.
If this value has not previously been interned, then intern
will
allocate a spot for the value on the heap. Otherwise, it will return a
pointer to the \[T\]
previously allocated.
sourcepub fn intern_vec(&self, val: Vec<T>) -> ArenaIntern<'_, [T]>
pub fn intern_vec(&self, val: Vec<T>) -> ArenaIntern<'_, [T]>
Intern a Vec<T>
as ArenaIntern<[T]>
.
If this value has not previously been interned, then intern
will save
the provided Vec<T>
. Otherwise, it will free its input Vec<T>
and
return a pointer to the [T]
previously saved.
sourcepub fn intern_box(&self, val: Box<[T]>) -> ArenaIntern<'_, [T]>
pub fn intern_box(&self, val: Box<[T]>) -> ArenaIntern<'_, [T]>
Intern a Box<[T]>
as ArenaIntern<[T]>
.
If this value has not previously been interned, then intern
will save
the provided Box<CSr>
. Otherwise, it will free its input Box<[T]>
and return a pointer to the [T]
previously saved.