Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

#entity

🏭 Macro

#[entity(id)]

Provides a quick way to identify a custom field as the ID for an entity.

📝 Macro Example:

use repox::{Entity, Identity};

#[derive(Debug, Clone, PartialEq, Entity)]
pub struct UserPasswordHash {
    #[entity(id)]
    pub user_id: u32,
    pub data: Vec<u8>,
}

// Quick Demo of the ID in action:
let hash = UserPasswordHash { user_id: 42, data: vec![1, 2, 3] };
assert_eq!(hash.id(), 42);

🔬 Macro Details:

Here is the same example, but, without using the macro. 🧙‍♂️

use repox::{Entity, Identity};

#[derive(Debug, Clone, PartialEq, Entity)]
pub struct UserPasswordHash {
    pub user_id: u32,
    pub data: Vec<u8>,
}

// this is what the `entity(id)` macro generates for us
impl Identity for UserPasswordHash {
    type ID = u32;
    fn id(&self) -> Self::ID { self.user_id }
}

// Quick Demo of the ID in action:
let hash = UserPasswordHash { user_id: 67, data: vec![1, 2, 3] };
assert_eq!(hash.id(), 67);