gtest 2.0.0-pre.1

Testing utils for developing Gear programs.
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

//! Nonce manager.

use crate::state::WithOverlay;
use std::thread::LocalKey;

thread_local! {
    /// Definition of the storage value storing message nonce.
    pub(super) static MSG_NONCE: WithOverlay<u64> = WithOverlay::new(1);
    /// Definition of the storage value storing id nonce.
    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);
    }
}