This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use anyhow_ext::Result;
|
||||
use std::fmt::Debug;
|
||||
use std::sync::Arc;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
@@ -8,7 +9,7 @@ use crate::StringKey;
|
||||
|
||||
/// A data library is a collection of methods to set up a default library, where values are stored
|
||||
/// by both key, while keeping their insertion order.
|
||||
pub trait DataLibrary<T: ?Sized> {
|
||||
pub trait DataLibrary<T: ?Sized>: Debug {
|
||||
/// Returns the underlying map.
|
||||
fn map(&self) -> &IndexMap<StringKey, Arc<T>>;
|
||||
/// Returns the underlying map in mutable manner.
|
||||
@@ -25,32 +26,22 @@ pub trait DataLibrary<T: ?Sized> {
|
||||
fn remove(&self, key: &StringKey) {
|
||||
#[allow(clippy::unwrap_used)] // We know this cant fail.
|
||||
let self_mut = unsafe { (self as *const Self as *mut Self).as_mut() }.unwrap();
|
||||
self_mut.get_modify().remove(key);
|
||||
self_mut.get_modify().swap_remove(key);
|
||||
}
|
||||
|
||||
/// Gets a value from the library.
|
||||
fn get(&self, key: &StringKey) -> Option<Arc<T>> {
|
||||
self.map().get::<StringKey>(key).cloned()
|
||||
}
|
||||
fn get(&self, key: &StringKey) -> Option<Arc<T>> { self.map().get::<StringKey>(key).cloned() }
|
||||
|
||||
/// Gets a value from the library.
|
||||
fn get_by_hash(&self, key: u32) -> Option<Arc<T>> {
|
||||
self.map().get::<u32>(&key).cloned()
|
||||
}
|
||||
fn get_by_hash(&self, key: u32) -> Option<Arc<T>> { self.map().get::<u32>(&key).cloned() }
|
||||
|
||||
/// Gets a value from the library by the index where it is stored.
|
||||
fn get_key_by_index(&self, index: usize) -> Option<StringKey> {
|
||||
self.map().get_index(index).map(|a| a.0.clone())
|
||||
}
|
||||
fn get_key_by_index(&self, index: usize) -> Option<StringKey> { self.map().get_index(index).map(|a| a.0.clone()) }
|
||||
|
||||
/// Gets the amount of values in the library.
|
||||
fn len(&self) -> usize {
|
||||
self.map().len()
|
||||
}
|
||||
fn len(&self) -> usize { self.map().len() }
|
||||
/// Returns whether the library has no values.
|
||||
fn is_empty(&self) -> bool {
|
||||
self.map().is_empty()
|
||||
}
|
||||
fn is_empty(&self) -> bool { self.map().is_empty() }
|
||||
|
||||
/// Gets a random value from the library.
|
||||
fn random_value(&self, rand: &mut Random) -> Result<&Arc<T>> {
|
||||
|
||||
Reference in New Issue
Block a user