messaging_thread_pool/id_targeted.rs
1use std::fmt::Debug;
2
3/// A trait for types that have an ID used for routing within the thread pool.
4///
5/// This trait is fundamental to how `messaging_thread_pool` works. The ID is used to:
6/// - Route requests to the correct thread (`id % thread_count`)
7/// - Identify which pool item should process a message
8/// - Associate responses with their original requests
9///
10/// # Implementation Requirements
11///
12/// - IDs must be unique within a thread pool for pool items
13/// - IDs should be stable (not change during the object's lifetime)
14/// - The `id()` method should be cheap to call (typically just returning a field)
15///
16/// # Example
17///
18/// ```rust
19/// use messaging_thread_pool::IdTargeted;
20///
21/// #[derive(Debug)]
22/// struct MyItem {
23/// id: u64,
24/// data: String,
25/// }
26///
27/// impl IdTargeted for MyItem {
28/// fn id(&self) -> u64 {
29/// self.id
30/// }
31/// }
32///
33/// // Request types also implement IdTargeted
34/// #[derive(Debug)]
35/// struct MyRequest(u64, String);
36///
37/// impl IdTargeted for MyRequest {
38/// fn id(&self) -> u64 {
39/// self.0 // First field is the target ID
40/// }
41/// }
42/// ```
43///
44/// # Note on Generated Types
45///
46/// When using the `#[pool_item]` macro, `IdTargeted` is automatically implemented
47/// for all generated request and response types. You only need to implement it
48/// manually for:
49/// - Your pool item struct
50/// - Custom initialization request types (when using `Init = "..."`)
51pub trait IdTargeted: Debug {
52 /// Returns the ID used for routing this type within the thread pool.
53 fn id(&self) -> u64;
54}
55
56impl IdTargeted for u64 {
57 fn id(&self) -> u64 {
58 *self
59 }
60}