pub struct Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a>,
V::PrimaryKeyType: Ord + FromStr + Display + Debug + Clone,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,{ /* private fields */ }Expand description
Represents a database table utilizing a BTreeMap for underlying data storage.
Needs the PrimaryKey trait to be implemented for the value type. Offers
enhanced methods for manipulating records, including add, edit, delete, get, and search.
use light_magic::{
serde::{Deserialize, Serialize},
table::{PrimaryKey, Table},
};
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
struct User {
id: usize,
name: String,
age: usize,
}
impl PrimaryKey for User {
type PrimaryKeyType = usize;
fn primary_key(&self) -> &Self::PrimaryKeyType {
&self.id
}
}Implementations§
Source§impl<V> Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a>,
V::PrimaryKeyType: Ord + FromStr + Display + Debug + Clone,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
impl<V> Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a>,
V::PrimaryKeyType: Ord + FromStr + Display + Debug + Clone,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
Sourcepub fn add(&mut self, value: V) -> Option<V>
pub fn add(&mut self, value: V) -> Option<V>
Adds an entry to the table, returns the value or None if the addition failed
Sourcepub fn get(&self, key: &V::PrimaryKeyType) -> Option<&V>
pub fn get(&self, key: &V::PrimaryKeyType) -> Option<&V>
Gets an entry from the table, returns the value or None if it couldn’t find the data
Sourcepub fn get_mut(&mut self, key: &V::PrimaryKeyType) -> Option<&mut V>
pub fn get_mut(&mut self, key: &V::PrimaryKeyType) -> Option<&mut V>
Gets an mutable entry from the table, returns the value or None if it couldn’t find the data
Sourcepub fn edit(&mut self, key: &V::PrimaryKeyType, new_value: V) -> Option<V>
pub fn edit(&mut self, key: &V::PrimaryKeyType, new_value: V) -> Option<V>
Edits an entry in the table, returns the new_value or None if the editing failed
Sourcepub fn delete(&mut self, key: &V::PrimaryKeyType) -> Option<V>
pub fn delete(&mut self, key: &V::PrimaryKeyType) -> Option<V>
Deletes an entry from the table, returns the value or None if the deletion failed
Sourcepub fn search_ordered<F, O>(&self, predicate: F, comparator: O) -> Vec<&V>
pub fn search_ordered<F, O>(&self, predicate: F, comparator: O) -> Vec<&V>
Searches the table by a predicate function and a custom ordering with a comparator function
Sourcepub fn values(&self) -> Values<'_, V::PrimaryKeyType, V>
pub fn values(&self) -> Values<'_, V::PrimaryKeyType, V>
Gets an iterator over the values of the map, in order by key.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, V::PrimaryKeyType, V>
pub fn values_mut(&mut self) -> ValuesMut<'_, V::PrimaryKeyType, V>
Gets a mutable iterator over the values of the map, in order by key.