Struct light_magic::table::Table
source · pub struct Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a>,
V::PrimaryKeyType: Ord + FromStr + Display + Debug,
<<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::{
atomic::DataStore,
serde::{Deserialize, Serialize},
table::{PrimaryKey, Table},
};
#[derive(Default, Debug, Serialize, Deserialize)]
struct Database {
user: Table<User>,
}
impl DataStore for Database {}
#[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: Clone + PrimaryKey + Serialize + for<'a> Deserialize<'a>,
V::PrimaryKeyType: Clone + Ord + FromStr + Display + Debug,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
impl<V> Table<V>where
V: Clone + PrimaryKey + Serialize + for<'a> Deserialize<'a>,
V::PrimaryKeyType: Clone + Ord + FromStr + Display + Debug,
<<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 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 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.
Trait Implementations§
source§impl<V> Debug for Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a> + Debug,
V::PrimaryKeyType: Ord + FromStr + Display + Debug,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
impl<V> Debug for Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a> + Debug,
V::PrimaryKeyType: Ord + FromStr + Display + Debug,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
source§impl<V> Default for Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a> + Default,
V::PrimaryKeyType: Ord + FromStr + Display + Debug,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
impl<V> Default for Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a> + Default,
V::PrimaryKeyType: Ord + FromStr + Display + Debug,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
source§impl<'de, V> Deserialize<'de> for Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a>,
V::PrimaryKeyType: Ord + FromStr + Display + Debug,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
impl<'de, V> Deserialize<'de> for Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a>,
V::PrimaryKeyType: Ord + FromStr + Display + Debug,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl<V> Serialize for Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a>,
V::PrimaryKeyType: Ord + FromStr + Display + Debug,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
impl<V> Serialize for Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a>,
V::PrimaryKeyType: Ord + FromStr + Display + Debug,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
Auto Trait Implementations§
impl<V> Freeze for Table<V>
impl<V> RefUnwindSafe for Table<V>
impl<V> Send for Table<V>
impl<V> Sync for Table<V>
impl<V> Unpin for Table<V>
impl<V> UnwindSafe for Table<V>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more