Table

Struct Table 

Source
pub struct Table<V>{ /* 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>

Source

pub fn add(&mut self, value: V) -> Option<V>
where V: Clone, V::PrimaryKeyType: Clone,

Adds an entry to the table, returns the value or None if the key already exists in that table.

Source

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 value.

Source

pub fn get_mut(&mut self, key: &V::PrimaryKeyType) -> Option<&mut V>

Gets a mutable entry from the table, returns the value or None if it couldn’t find the value.

Source

pub fn edit(&mut self, key: &V::PrimaryKeyType, new_value: V) -> Option<V>
where V: Clone, V::PrimaryKeyType: Clone,

Edits an entry in the table, returns the new_value or None if the entry couldn’t be found.

Source

pub fn delete(&mut self, key: &V::PrimaryKeyType) -> Option<V>

Deletes an entry from the table, returns the value or None if the key wasn’t found.

Source

pub fn search<F>(&self, predicate: F) -> Vec<&V>
where F: Fn(&V) -> bool,

Searches the table by a predicate function.

Source

pub fn search_ordered<F, O>(&self, predicate: F, comparator: O) -> Vec<&V>
where F: Fn(&V) -> bool, O: Fn(&&V, &&V) -> Ordering,

Searches the table by a predicate function and a custom ordering with a comparator function.

Source

pub fn values(&self) -> Values<'_, V::PrimaryKeyType, V>

Gets an iterator over the values of the map, in order by key.

Source

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>

Source§

fn clone(&self) -> Table<V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<V> Debug for Table<V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<V> Default for Table<V>

Source§

fn default() -> Table<V>

Returns the “default value” for a type. Read more
Source§

impl<'de, V> Deserialize<'de> for Table<V>

Source§

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>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<V> Freeze for Table<V>

§

impl<V> RefUnwindSafe for Table<V>

§

impl<V> Send for Table<V>
where <V as PrimaryKey>::PrimaryKeyType: Sized + Send, V: Send,

§

impl<V> Sync for Table<V>
where <V as PrimaryKey>::PrimaryKeyType: Sized + Sync, V: Sync,

§

impl<V> Unpin for Table<V>

§

impl<V> UnwindSafe for Table<V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,