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
77
78
79
80
81
82
83
84
85
86
87
88
89
//! # ID Provider Utilities
//!
//! This module provides traits and implementations for generating unique IDs
//! for pool items.
//!
//! ## When You Need an ID Provider
//!
//! For simple cases where you create pool items with explicit IDs, you don't need
//! an ID provider:
//!
//! ```rust
//! use messaging_thread_pool::{ThreadPool, samples::*};
//!
//! let pool = ThreadPool::<Randoms>::new(4);
//!
//! // Manually assign IDs
//! pool.send_and_receive((0..100u64).map(RandomsAddRequest)).unwrap();
//! ```
//!
//! ID providers become useful when:
//! - Multiple components create pool items and need unique IDs
//! - IDs need to be generated dynamically at runtime
//! - Multiple thread pools share a single namespace of IDs
//!
//! ## Example: Shared ID Provider
//!
//! ```rust
//! use std::sync::Arc;
//! use messaging_thread_pool::id_provider::{IdProvider, id_provider_mutex::IdProviderMutex};
//!
//! // Create a shared ID provider
//! let id_provider = Arc::new(IdProviderMutex::new(0));
//!
//! // Multiple components can safely get unique IDs
//! let id1 = id_provider.next_id(); // 0
//! let id2 = id_provider.next_id(); // 1
//!
//! // Or clone the Arc to share across threads
//! let provider_clone = Arc::clone(&id_provider);
//! ```
//!
//! ## Implementations
//!
//! - [`id_provider_mutex::IdProviderMutex`] - Thread-safe provider using a `Mutex`
//! - [`id_provider_static::IdProviderStatic`] - Static/global provider (for testing)
use Debug;
/// A trait for generating unique IDs for pool items.
///
/// Implementations must be thread-safe (`Send + Sync`) since ID providers
/// are often shared across multiple threads.
///
/// # Example Implementation
///
/// ```rust
/// use std::sync::atomic::{AtomicU64, Ordering};
/// use messaging_thread_pool::id_provider::IdProvider;
///
/// #[derive(Debug)]
/// struct AtomicIdProvider {
/// counter: AtomicU64,
/// }
///
/// impl IdProvider for AtomicIdProvider {
/// fn next_id(&self) -> u64 {
/// self.counter.fetch_add(1, Ordering::SeqCst)
/// }
///
/// fn peek_next_id(&self) -> u64 {
/// self.counter.load(Ordering::SeqCst)
/// }
/// }
/// ```