Struct artichoke_backend::module::Registry
source · [−]pub struct Registry<S = RandomState>(_);
Expand description
A registry for Module specs that uses types as keys.
This data structure is used for associating data type metadata with a Rust type which can be used to ensure the lifetime of the associated metadata.
Example: mrb_data_type
In the mruby C API, custom data types define a mrb_data_type
struct which
contains the custom data type’s module name and free function. The C API
requires that this struct live at least as long as the mrb_state
.
Typically, the mrb_data_type
is static
.
static const struct mrb_data_type mrb_time_type = { "Time", mrb_free };
The registry resembles an append-only HashMap
.
The registry stores Module specs behind a Box
pointer to ensure pointers
to the interior of the spec, like the CString
module
name, are not invalidated as the underlying storage reallocates.
Implementations
sourceimpl Registry<RandomState>
impl Registry<RandomState>
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Construct a new Registry
with the given capacity
.
sourceimpl<S> Registry<S>
impl<S> Registry<S>
sourcepub fn with_hasher(hash_builder: S) -> Self
pub fn with_hasher(hash_builder: S) -> Self
Construct a new Registry
with the given hash_builder
.
sourcepub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
Construct a new Registry
with the given capacity
and hash_builder
.
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of module specs the registry can hold without reallocating.
This number is a lower bound; the Registry
might be able to hold more,
but is guaranteed to be able to hold at least this many.
sourcepub fn module_specs(&self) -> ModuleSpecs<'_>
pub fn module_specs(&self) -> ModuleSpecs<'_>
An iterator of all module specs stored in the Registry
in
arbitrary order.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of module specs in the registry.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the registry contains module specs.
sourcepub fn hasher(&self) -> &S
pub fn hasher(&self) -> &S
Returns a reference to the registry’s BuildHasher
.
sourceimpl<S> Registry<S> where
S: BuildHasher,
impl<S> Registry<S> where
S: BuildHasher,
sourcepub fn contains<K>(&self) -> bool where
K: Any,
pub fn contains<K>(&self) -> bool where
K: Any,
Returns true if the registry contains a module spec for the specified type.
sourcepub fn insert<K>(&mut self, spec: Box<Spec>) where
K: Any,
pub fn insert<K>(&mut self, spec: Box<Spec>) where
K: Any,
Inserts a type-module spec pair into the registry.
This operation will only succeed if K
has never been inserted into the
registry.
Panics
If insert
has previously been called with type K
, this function will
panic. Registry
is append-only and does not allow mutations.
sourcepub fn get<K>(&self) -> Option<&Spec> where
K: Any,
pub fn get<K>(&self) -> Option<&Spec> where
K: Any,
Returns a reference to the module spec corresponding to the type key.
If the type K
has not been registered, None
is returned.
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity
for at least additional more elements to be inserted
in the Registry
. The collection may reserve more space to avoid
frequent reallocations.
sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Tries to reserve capacity for at least additional more elements to be
inserted in the Registry
. The collection may reserve more space to
avoid frequent reallocations.
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
Trait Implementations
sourceimpl<'a, S> IntoIterator for &'a Registry<S>
impl<'a, S> IntoIterator for &'a Registry<S>
sourceimpl<S> PartialEq<Registry<S>> for Registry<S> where
S: BuildHasher,
impl<S> PartialEq<Registry<S>> for Registry<S> where
S: BuildHasher,
impl<S> Eq for Registry<S> where
S: BuildHasher,
Auto Trait Implementations
impl<S> RefUnwindSafe for Registry<S> where
S: RefUnwindSafe,
impl<S> Send for Registry<S> where
S: Send,
impl<S> Sync for Registry<S> where
S: Sync,
impl<S> Unpin for Registry<S> where
S: Unpin,
impl<S> UnwindSafe for Registry<S> where
S: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more