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 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.
Trait Implementations§
source§impl<V> Clone for Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a> + Clone,
V::PrimaryKeyType: Ord + FromStr + Display + Debug + Clone,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
impl<V> Clone for Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a> + Clone,
V::PrimaryKeyType: Ord + FromStr + Display + Debug + Clone,
<<V as PrimaryKey>::PrimaryKeyType as FromStr>::Err: Display,
source§impl<V> Debug for Table<V>where
V: PrimaryKey + Serialize + for<'a> Deserialize<'a> + Debug,
V::PrimaryKeyType: Ord + FromStr + Display + Debug + Clone,
<<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 + Clone,
<<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 + Clone,
<<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 + Clone,
<<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 + Clone,
<<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 + Clone,
<<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>,
source§impl<V> Serialize for 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> Serialize for 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,
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)