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: ?Sized> Arena<T>

source

pub fn new() -> Self

Allocate a new Arena

source§

impl<T: Eq + Hash> Arena<T>

source

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<T: Eq + Hash + ?Sized> Arena<T>

source

pub fn intern_ref<'a, 'b, I>(&'a self, val: &'b I) -> ArenaIntern<'a, T>
where T: 'a + Borrow<I>, Box<T>: From<&'b I>, I: Eq + Hash + ?Sized,

Tedst

source§

impl Arena<str>

source

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.

source

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.

source

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>

source

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);
source

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);
source

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>

source

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);
source

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);
source

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>

source

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);
source

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);
source

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]>

source

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.

source

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.

source

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.

source§

impl<T: Eq + Hash + ?Sized> Arena<T>

source

pub fn intern_from<'a, 'b, I>(&'a self, val: &'b I) -> ArenaIntern<'a, T>
where T: 'a + Borrow<I> + From<&'b I>, I: Eq + Hash + ?Sized,

Intern a reference to a type that can be converted into a Box<T> as ArenaIntern<T>.

Trait Implementations§

source§

impl<T: ?Sized + DeepSizeOf> DeepSizeOf for Arena<T>

source§

fn deep_size_of_children(&self, context: &mut Context) -> usize

Returns an estimation of the heap-managed storage of this object. This does not include the size of the object itself. Read more
source§

fn deep_size_of(&self) -> usize

Returns an estimation of a total size of memory owned by the object, including heap-managed storage. Read more
source§

impl<T> Default for Arena<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for Arena<T>

§

impl<T> RefUnwindSafe for Arena<T>
where T: ?Sized,

§

impl<T> Send for Arena<T>
where T: Send + ?Sized,

§

impl<T> Sync for Arena<T>
where T: Send + ?Sized,

§

impl<T> Unpin for Arena<T>
where T: ?Sized,

§

impl<T> UnwindSafe for Arena<T>
where T: ?Sized,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V