inkpad_support/traits/
frame.rs1use crate::{traits::Cache, types::State};
3use inkpad_std::Rc;
4use core::cell::RefCell;
5
6pub trait Frame<Memory: 'static + Clone>: Cache<Memory> {
8 fn active(&self) -> Option<[u8; 32]> {
9 Some(self.frame().last()?.borrow().hash)
10 }
11
12 fn active_set(&self, key: [u8; 32], value: Vec<u8>) -> Option<Vec<u8>> {
13 self.frame()
14 .last()
15 .map(|state| state.borrow_mut().set(key.to_vec(), value))?
16 }
17
18 fn active_get(&self, key: &[u8]) -> Option<Vec<u8>> {
19 self.frame()
20 .last()
21 .map(|state| state.borrow().get(key).map(|v| v.to_vec()))?
22 }
23
24 fn push(&mut self, code_hash: [u8; 32], memory: Memory) {
25 self.frame_mut()
26 .push(Rc::new(RefCell::new(State::new(code_hash, memory))));
27 }
28
29 fn switch(&mut self, code_hash: [u8; 32]) -> Option<()> {
30 let frames = self.frame().clone();
31 self.frame_mut().push(
32 frames
33 .iter()
34 .filter(|v| v.borrow().hash == code_hash)
35 .last()?
36 .clone(),
37 );
38 Some(())
39 }
40
41 fn back(&mut self) -> Option<()> {
42 let frame_mut = self.frame_mut();
43 if frame_mut.len() < 2 {
44 None
45 } else {
46 frame_mut.push(frame_mut[frame_mut.len() - 2].clone());
47 Some(())
48 }
49 }
50
51 fn top(&mut self) -> Option<()> {
52 let frame_mut = self.frame_mut();
53 frame_mut.push(frame_mut[0].clone());
54 Some(())
55 }
56}