Skip to main content

revm_handler/
item_or_result.rs

1use crate::evm::FrameTr;
2
3/// Represents either an item or a result.
4#[derive(Clone, Debug)]
5pub enum ItemOrResult<ITEM, RES> {
6    /// Contains an item that needs further processing.
7    Item(ITEM),
8    /// Contains a final result.
9    Result(RES),
10}
11
12impl<ITEM, RES> ItemOrResult<ITEM, RES> {
13    /// Maps the item variant using the provided function, leaving results unchanged.
14    pub fn map_item<OITEM>(self, f: impl FnOnce(ITEM) -> OITEM) -> ItemOrResult<OITEM, RES> {
15        match self {
16            ItemOrResult::Item(item) => ItemOrResult::Item(f(item)),
17            ItemOrResult::Result(result) => ItemOrResult::Result(result),
18        }
19    }
20
21    /// Maps the item variant using the provided function, leaving results unchanged.
22    #[deprecated(
23        note = "Use map_item() instead; this maps the Item variant (not necessarily a frame)"
24    )]
25    pub fn map_frame<OITEM>(self, f: impl FnOnce(ITEM) -> OITEM) -> ItemOrResult<OITEM, RES> {
26        self.map_item(f)
27    }
28
29    /// Maps the result variant using the provided function, leaving items unchanged.
30    pub fn map_result<ORES>(self, f: impl FnOnce(RES) -> ORES) -> ItemOrResult<ITEM, ORES> {
31        match self {
32            ItemOrResult::Item(item) => ItemOrResult::Item(item),
33            ItemOrResult::Result(result) => ItemOrResult::Result(f(result)),
34        }
35    }
36}
37
38impl<ITEM, RES> ItemOrResult<ITEM, RES> {
39    /// Returns true if this is a result variant.
40    pub fn is_result(&self) -> bool {
41        matches!(self, ItemOrResult::Result(_))
42    }
43
44    /// Returns true if this is an item variant.
45    pub fn is_item(&self) -> bool {
46        matches!(self, ItemOrResult::Item(_))
47    }
48}
49
50/// Type alias for frame initialization or result.
51pub type FrameInitOrResult<FRAME> =
52    ItemOrResult<<FRAME as FrameTr>::FrameInit, <FRAME as FrameTr>::FrameResult>;