hibana 0.4.1

Const-projected Affine Multiparty Session Types for choreography-first Rust protocols
Documentation
use crate::{
    control::cluster::effects::EffectEnvelopeRef,
    endpoint::kernel::EndpointArenaLayout,
    global::const_dsl::{PolicyMode, ScopeId},
};

use super::{
    program::{CompiledProgramFacts, ControlSemanticsTable, DynamicPolicySite},
    role::CompiledRoleImage,
};
use crate::global::compiled::lowering::ProgramStamp;

/// Sealed runtime owner for immutable program-wide compiled facts.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct CompiledProgramRef {
    stamp: ProgramStamp,
    compiled: *const CompiledProgramFacts,
}

impl CompiledProgramRef {
    #[inline(always)]
    pub(crate) unsafe fn from_raw(
        stamp: ProgramStamp,
        compiled: *const CompiledProgramFacts,
    ) -> Self {
        debug_assert!(!compiled.is_null());
        Self { stamp, compiled }
    }

    #[inline(always)]
    pub(crate) const fn stamp(&self) -> ProgramStamp {
        self.stamp
    }

    #[inline(always)]
    fn compiled(&self) -> &CompiledProgramFacts {
        debug_assert!(!self.compiled.is_null());
        unsafe { &*self.compiled }
    }

    #[inline(always)]
    pub(crate) fn effect_envelope(&self) -> EffectEnvelopeRef<'_> {
        self.compiled().effect_envelope()
    }

    #[inline(always)]
    pub(crate) fn role_count(&self) -> usize {
        self.compiled().role_count()
    }

    #[inline(always)]
    pub(crate) fn dynamic_policy_sites_for(
        &self,
        policy_id: u16,
    ) -> impl Iterator<Item = &DynamicPolicySite> + '_ {
        self.compiled().dynamic_policy_sites_for(policy_id)
    }

    #[inline(always)]
    pub(crate) fn control_semantics(&self) -> &ControlSemanticsTable {
        self.compiled().control_semantics()
    }

    #[inline(always)]
    pub(crate) fn route_controller_role(&self, scope_id: ScopeId) -> Option<u8> {
        self.compiled().route_controller_role(scope_id)
    }

    #[inline(always)]
    pub(crate) fn route_controller(
        &self,
        scope_id: ScopeId,
    ) -> Option<(
        PolicyMode,
        crate::eff::EffIndex,
        u8,
        crate::control::cap::mint::ControlOp,
    )> {
        self.compiled().route_controller(scope_id)
    }
}

/// Sealed runtime owner for role-local immutable compiled facts within a compiled program ref.
#[derive(Clone, Copy)]
pub(crate) struct RoleImageSlice<const ROLE: u8> {
    program: CompiledProgramRef,
    compiled: *const CompiledRoleImage,
}

impl<const ROLE: u8> RoleImageSlice<ROLE> {
    #[inline(always)]
    pub(crate) unsafe fn from_raw(
        program: CompiledProgramRef,
        compiled: *const CompiledRoleImage,
    ) -> Self {
        debug_assert!(!compiled.is_null());
        Self { program, compiled }
    }

    #[inline(always)]
    pub(crate) const fn program(&self) -> CompiledProgramRef {
        self.program
    }

    #[inline(always)]
    pub(crate) const fn compiled_ptr(&self) -> *const CompiledRoleImage {
        self.compiled
    }

    #[inline(always)]
    fn compiled(&self) -> &CompiledRoleImage {
        debug_assert!(!self.compiled.is_null());
        unsafe { &*self.compiled }
    }

    #[inline(always)]
    pub(crate) fn has_active_lane(&self, lane_idx: usize) -> bool {
        self.compiled().has_active_lane(lane_idx)
    }

    #[inline(always)]
    pub(crate) fn first_active_lane(&self) -> Option<usize> {
        self.compiled().first_active_lane()
    }

    #[inline(always)]
    pub(crate) fn endpoint_lane_slot_count(&self) -> usize {
        self.compiled().endpoint_lane_slot_count()
    }

    #[inline(always)]
    pub(crate) fn logical_lane_count(&self) -> usize {
        self.compiled().logical_lane_count()
    }

    #[inline(always)]
    pub(crate) fn route_table_frame_slots(&self) -> usize {
        self.compiled().route_table_frame_slots()
    }

    #[inline(always)]
    pub(crate) fn route_table_lane_slots(&self) -> usize {
        self.compiled().route_table_lane_slots()
    }

    #[inline(always)]
    pub(crate) fn loop_table_slots(&self) -> usize {
        self.compiled().loop_table_slots()
    }

    #[inline(always)]
    pub(crate) fn resident_cap_entries(&self) -> usize {
        self.compiled().resident_cap_entries()
    }

    #[inline(always)]
    pub(crate) fn endpoint_arena_layout_for_binding(
        &self,
        binding_enabled: bool,
    ) -> EndpointArenaLayout {
        self.compiled()
            .endpoint_arena_layout_for_binding(binding_enabled)
    }
}