canic_core/model/memory/state/
subnet.rs

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