use crate::state::WithOverlay;
use std::thread::LocalKey;
thread_local! {
pub(super) static MSG_NONCE: WithOverlay<u64> = WithOverlay::new(1);
pub(super) static ID_NONCE: WithOverlay<u64> = WithOverlay::new(1);
}
fn msg_nonce_storage() -> &'static LocalKey<WithOverlay<u64>> {
&MSG_NONCE
}
fn id_nonce_storage() -> &'static LocalKey<WithOverlay<u64>> {
&ID_NONCE
}
#[derive(Debug, Clone, Default)]
pub(crate) struct NonceManager;
impl NonceManager {
pub(crate) fn fetch_inc_message_nonce(&self) -> u64 {
msg_nonce_storage().with(|nonce| {
let value = *nonce.data();
*nonce.data_mut() = value + 1;
value
})
}
pub(crate) fn id_nonce(&self) -> u64 {
id_nonce_storage().with(|nonce| *nonce.data())
}
pub(crate) fn inc_id_nonce(&self) {
id_nonce_storage().with(|nonce| {
let value = *nonce.data();
*nonce.data_mut() = value + 1;
});
}
pub(crate) fn reset(&self) {
msg_nonce_storage().with(|nonce| *nonce.data_mut() = 1);
id_nonce_storage().with(|nonce| *nonce.data_mut() = 1);
}
}