1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
//! Traits to access and change the key of a Toql derived struct.
/// Trait to read the key of a struct.
///
/// Implemented automatically for every Toql derived struct.
///
/// The trait can be used by library users.
///
/// ### Example
/// Basic usage (assume a Toql derived User struct):
/// ```rust, ignore
/// use toql_core::keyed::Keyed;
/// use toql_derive::Toql;
///
/// #[derive(Toql)]
/// struct User{
/// #[toql(key)]
/// id: u64,
/// name: String
/// }
///
/// let u = User {id: 5, name: "Sue".to_string()};
/// let k = u.key();
///
/// assert_eq!(k.id , 5);
/// ```
/// For collections there is [map_key](crate::map_key::map_key). It makes use of this trait.
pub trait Keyed {
/// Type of key.
type Key: Eq + std::hash::Hash + crate::key::Key;
/// Return value of the key for a given entity.
fn key(&self) -> Self::Key;
}
/// Trait to set the key of a `struct`.
///
/// Implemented automatically for every Toql derived struct.
/// The trait can be used by library users.
///
/// ### Example
/// Basic usage (assume a Toql derived User struct):
/// ```rust, ignore
/// use crate::keyed::Keyed;
/// use toql_derive::Toql;
///
/// #[derive(Toql)]
/// struct User{
/// #[toql(key)]
/// id: u64,
/// name: String
/// }
///
/// let u = User {id: 5, name: "Sue".to_string()};
/// u.set_key(55.into());
///
/// /// assert_eq!(u.id , 55);
/// ```
/// Here the number 5 is converted into the key type of `User`. Then this key is
/// set.
pub trait KeyedMut: Keyed {
/// Set the key of the implementing `struct`.
fn set_key(&mut self, key: Self::Key);
}