Trait reindeer::AutoIncrementEntity
source · pub trait AutoIncrementEntity: Entity<Key = u32> {
// Required methods
fn get_next_key(db: &Db) -> Result<u32>;
fn save_next(&mut self, db: &Db) -> Result<u32>;
}
Expand description
AutoIncrementEntity
is a trait aimed to automatically be
implemented on Entities that have u32
as their Key
type.
It provides the save_next()
method that updates the key of the entity
with a new, incremented one before saving it to the database.
Required Methods§
sourcefn get_next_key(db: &Db) -> Result<u32>
fn get_next_key(db: &Db) -> Result<u32>
Returns a new key that is currently not used in the store
sourcefn save_next(&mut self, db: &Db) -> Result<u32>
fn save_next(&mut self, db: &Db) -> Result<u32>
Saves the entity to the database after having modified its key to an auto-incremented one.
Example
let m_struct = MyStruct { key : 0, prop9 : 44};
m_struct.save_next(&db)?; // will have key 0
let m_struct_2 = MyStruct { key : 0, prop9 :59};
m_struct_2.save_next(&db)?; // creates a new entry with key 1, and so on
// m_struct2.key is now 1