Skip to content

Commit

Permalink
wip: Add cell context (based on #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Sep 19, 2023
1 parent 71cc96f commit 949e4e4
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 68 deletions.
96 changes: 96 additions & 0 deletions src/cell/cell_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use crate::cell::{Cell, CellFamily, CellParts, DefaultFinalizer, DynCell, Finalizer};
use crate::error::Error;

/// Cell family with a noop cell context.
pub trait DefaultCellContext: CellFamily {
/// The default cell context type.
type CellContext: CellContext;

/// Creates a default cell context.
fn default_cell_context() -> Self::CellContext;
}

/// Dictionary insertion mode.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[repr(u8)]
pub enum LoadMode {
/// Do not modify the default behavior.
Noop = 0b00,
/// Count the cost of loading the cell.
UseGas = 0b01,
/// Resolve exotic cells such as libraries or merkle stuff.
Resolve = 0b10,
/// Both `UseGas` and `Resolve`.
Full = 0b11,
}

impl LoadMode {
/// Returns `true` if this mode requires gas accounting.
#[inline]
pub const fn use_gas(self) -> bool {
self as u8 & 0b01 != 0
}

/// Returns `true` if exotic cells are resolved in this mode.
#[inline]
pub const fn resolve(self) -> bool {
self as u8 & 0b10 != 0
}
}

/// Gas accounting and resolcing exotic cells.
pub trait CellContext: Finalizer {
/// Upcasts `dyn CellContext` to `dyn Finalizer`.
fn as_finalizer(&mut self) -> &mut dyn Finalizer;

/// Resolve an owned cell.
fn load_cell(&mut self, cell: Cell, mode: LoadMode) -> Result<Cell, Error>;

/// Resolve a cell reference.
fn load_dyn_cell<'a>(
&mut self,
cell: &'a DynCell,
mode: LoadMode,
) -> Result<&'a DynCell, Error>;
}

/// Default cell context without gas accounting and cells resolution.
///
/// NOTE: Wraps default cell finalizer to reduce generated code size.
#[derive(Default)]
#[repr(transparent)]
pub struct NoopCellContext(<Cell as DefaultFinalizer>::Finalizer);

impl Finalizer for NoopCellContext {
#[inline]
fn finalize_cell(&mut self, cell: CellParts<'_>) -> Result<Cell, Error> {
self.0.finalize_cell(cell)
}
}

impl CellContext for NoopCellContext {
#[inline]
fn as_finalizer(&mut self) -> &mut dyn Finalizer {
// SAFETY: NoopCellContext is #[repr(transparent)]
unsafe { &mut *(self as *mut Self as *mut <Cell as DefaultFinalizer>::Finalizer) }
}

#[inline]
fn load_cell(&mut self, cell: Cell, _: LoadMode) -> Result<Cell, Error> {
Ok(cell)
}

#[inline]
fn load_dyn_cell<'a>(&mut self, cell: &'a DynCell, _: LoadMode) -> Result<&'a DynCell, Error> {
Ok(cell)
}
}

impl DefaultCellContext for Cell {
type CellContext = NoopCellContext;

#[inline]
fn default_cell_context() -> Self::CellContext {
NoopCellContext(Cell::default_finalizer())
}
}
1 change: 1 addition & 0 deletions src/cell/cell_impl/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl CellFamily for Cell {
impl DefaultFinalizer for Cell {
type Finalizer = ArcCellFinalizer;

#[inline]
fn default_finalizer() -> Self::Finalizer {
ArcCellFinalizer
}
Expand Down
4 changes: 4 additions & 0 deletions src/cell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::error::{Error, ParseHashBytesError};
use crate::util::Bitstring;

pub use self::builder::{CellBuilder, CellRefsBuilder, Store};
pub use self::cell_context::{CellContext, DefaultCellContext, LoadMode, NoopCellContext};
pub use self::cell_impl::{StaticCell, VirtualCellWrapper};
pub use self::finalizer::{CellParts, DefaultFinalizer, Finalizer};
pub use self::slice::{CellSlice, CellSliceParts, CellSliceRange, CellSliceSize, ExactSize, Load};
Expand All @@ -23,6 +24,9 @@ pub use everscale_types_proc::{Load, Store};
/// Generic cell implementation.
mod cell_impl;

/// Traits for gas accounting and resolving exotic cells.
mod cell_context;

/// Cell finalization primitives.
mod finalizer;

Expand Down
Loading

0 comments on commit 949e4e4

Please sign in to comment.