Trait simple_tables::IdTable [−][src]
pub trait IdTable<UidType, Row>: Table<Row> where
UidType: PartialEq<UidType>,
Row: TableRow, {
fn get_id_from_row(row: &Row) -> UidType;
fn get_row(&self, uid: UidType) -> Option<&Row> { ... }
fn get_row_mut(&mut self, uid: UidType) -> Option<&mut Row> { ... }
fn get_row_index(&self, uid: UidType) -> Option<usize> { ... }
fn rm_row(&mut self, uid: UidType) -> Result<Row, TableError> { ... }
}
Expand description
Defines a table with a unique identifier. This class should be implemented alongside the
Table
trait.
When you have a table with a uid, this trait has to be implemented manually for now.
To implement
Notes
- If your IDE tells you following error when implementing this trait:you can simply ignore it given that you are using the
the trait bound `MyTable: Table<TableRow>` is not satisfied
table_row
macro. Your IDE just doesn’t know theTable
trait is already implemented for your struct. When you run your program, it will actually compile and run.
Required methods
fn get_id_from_row(row: &Row) -> UidType
fn get_id_from_row(row: &Row) -> UidType
Gets the uid from a row, this should be implemented manually (for now) for structs with uids.
Example
#[table_row]
struct MyTableRow {
id: i32,
name: String
}
#[table(rows = MyTableRow)]
struct MyTable {}
impl simple_tables::IdTable<i32, MyTableRow> for MyTable {
fn get_id_from_row(row: i32) -> i32 {
row.id
}
}
Provided methods
fn get_row_mut(&mut self, uid: UidType) -> Option<&mut Row>
fn get_row_mut(&mut self, uid: UidType) -> Option<&mut Row>
Returns a mutable reference to a row
fn get_row_index(&self, uid: UidType) -> Option<usize>
fn get_row_index(&self, uid: UidType) -> Option<usize>
Returns the index of the row with the uid. Will be None if there is no row with this uid.
fn rm_row(&mut self, uid: UidType) -> Result<Row, TableError>
fn rm_row(&mut self, uid: UidType) -> Result<Row, TableError>
Removes the row with the uid from the table and returns the row. Returns an error if there is no table row with the uid.