canic_core/model/memory/state/
subnet.rs

1use crate::{
2    cdk::structures::{DefaultMemoryImpl, cell::Cell, memory::VirtualMemory},
3    eager_static, ic_memory,
4    memory::impl_storable_bounded,
5    model::memory::id::state::SUBNET_STATE_ID,
6};
7use candid::CandidType;
8use serde::{Deserialize, Serialize};
9use std::cell::RefCell;
10
11//
12// SUBNET_STATE
13// EMPTY FOR NOW - if we ever want to store subnet-specific state it's here
14//
15
16eager_static! {
17    static SUBNET_STATE: RefCell<Cell<SubnetStateData, VirtualMemory<DefaultMemoryImpl>>> =
18        RefCell::new(Cell::init(
19            ic_memory!(SubnetState, SUBNET_STATE_ID),
20            SubnetStateData::default(),
21        ));
22}
23
24///
25/// SubnetStateData
26///
27
28#[derive(CandidType, Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
29pub struct SubnetStateData {}
30
31impl_storable_bounded!(SubnetStateData, 32, true);
32
33///
34/// SubnetState (public API)
35///
36
37pub struct SubnetState;
38
39impl SubnetState {
40    pub(crate) fn import(data: SubnetStateData) {
41        SUBNET_STATE.with_borrow_mut(|cell| cell.set(data));
42    }
43
44    #[must_use]
45    pub(crate) fn export() -> SubnetStateData {
46        SUBNET_STATE.with_borrow(|cell| *cell.get())
47    }
48}