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 65 66 67 68 69 70 71 72 73 74 75 76
/// Implements the Id trait for an object. For example, if an object Animal has /// a field 'species_id' which is a pointer toward another Object of type /// Species, you can implements Id with the following example. You can also /// implement Id for Animal itself (the identifier of an Animal is its own /// field that must be named `id`). /// ``` /// # use typed_index_collection::impl_id; /// # fn main() { /// struct Species { /// id: String, /// name: String, /// } /// struct Animal { /// id: String, /// name: String, /// species_id: String, /// } /// impl_id!(Animal, Species, species_id); /// impl_id!(Animal); /// # } /// ``` #[macro_export] macro_rules! impl_id { ($ty:ty, $gen:ty, $id: ident) => { impl typed_index_collection::Id<$gen> for $ty { fn id(&self) -> &str { &self.$id } fn set_id(&mut self, id: std::string::String) { self.$id = id; } } }; ($ty:ty) => { impl_id!($ty, $ty, id); }; } /// Implement trait `WithId` automatically for a type. /// /// The type must implement `Default` and have at least 2 fields: `id` and /// `name`. Both `id` and `name` will be set with the value of the input /// parameter `id`. /// ``` /// # use typed_index_collection::{impl_with_id, WithId}; /// # fn main() { /// #[derive(Default)] /// struct Animal { /// id: String, /// name: String, /// species: String, /// } /// impl_with_id!(Animal); /// let animal = Animal::with_id("cat"); /// assert_eq!("cat", animal.id); /// assert_eq!("cat", animal.name); /// assert_eq!("", animal.species); /// # } /// ``` #[macro_export] macro_rules! impl_with_id { ($ty:ty) => { impl typed_index_collection::WithId for $ty { // This warning occurs when the type only has id and name members and no other #[allow(clippy::needless_update)] fn with_id(id: &str) -> Self { Self { id: id.to_owned(), name: id.to_owned(), ..Default::default() } } } }; }