Trait simple_tables_core::IdTable
source ·
[−]pub trait IdTable<UidType: PartialEq, Row: TableRow>: Table<Row> {
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> { ... }
}
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