brokapi_finam/subs.rs
1//! Subscription id generator (unique within one connector).
2
3use std::sync::atomic::{AtomicU64, Ordering};
4
5use brokapi_core::SubscriptionId;
6
7/// Hands out [`SubscriptionId`]s: 1, 2, 3, ... (after `start`), never repeating.
8///
9/// The market data connector starts at 0 and the user data connector at 1000, so that ids from the
10/// two connectors of one process are easy to tell apart in logs. Thread safe.
11///
12/// # Examples
13///
14/// ```
15/// use brokapi_finam::subs::SubIdGen;
16///
17/// let ids = SubIdGen::new(1000);
18/// let first = ids.next();
19/// let second = ids.next();
20/// assert_ne!(first, second);
21/// ```
22pub struct SubIdGen(AtomicU64);
23
24impl SubIdGen {
25 /// A generator whose first id is `start + 1`.
26 pub fn new(start: u64) -> Self {
27 Self(AtomicU64::new(start))
28 }
29
30 /// The next unused id.
31 pub fn next(&self) -> SubscriptionId {
32 SubscriptionId::new(self.0.fetch_add(1, Ordering::Relaxed) + 1)
33 }
34}