Skip to main content

midnight_onchain_vm/
state_value_ext.rs

1// This file is part of midnight-ledger.
2// Copyright (C) 2025 Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
4// Licensed under the Apache License, Version 2.0 (the "License");
5// You may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14use storage::{arena::Sp, db::DB};
15
16use crate::error::OnchainProgramError;
17use base_crypto::fab::AlignedValue;
18use runtime_state::state::StateValue;
19
20pub trait StateValueExt<D: DB> {
21    fn as_cell(&self) -> Result<Sp<AlignedValue, D>, OnchainProgramError<D>>;
22    fn as_cell_ref(&self) -> Result<&AlignedValue, OnchainProgramError<D>>;
23}
24
25impl<D: DB> StateValueExt<D> for StateValue<D> {
26    fn as_cell(&self) -> Result<Sp<AlignedValue, D>, OnchainProgramError<D>> {
27        match self {
28            StateValue::Cell(value) => Ok(value.clone()),
29            _ => Err(OnchainProgramError::ExpectedCell(self.clone())),
30        }
31    }
32
33    fn as_cell_ref(&self) -> Result<&AlignedValue, OnchainProgramError<D>> {
34        match self {
35            StateValue::Cell(value) => Ok(value),
36            _ => Err(OnchainProgramError::ExpectedCell(self.clone())),
37        }
38    }
39}