data.rs
cdk-rsx/crates/cdk-rsx-engine-nyxus/src/data.rs
1//! A data-only admission contract, independent of physical upgrade certificates.2use core::{marker::PhantomData, num::*};34/// Data whose owned allocations all remain in Nyxus's persistent-heap domain.5///6/// This does not certify Rust layouts across binaries or semantic compatibility.7/// The separately compiled ABI/schema guard and module contract do that before8/// any saved pointer is interpreted. There are deliberately no implementations9/// for Global Vec/String/Rc/Box, references (including static references), raw10/// pointers, callbacks, trait objects, futures, guards or arbitrary opaque types.11/// Derive checks every record/variant field; unions are unsupported.12///13/// # Safety14/// Every reachable owned allocation must use the permanent retained authority;15/// every application field must satisfy this contract. Shared access must not16/// permit logical data mutation without a write capability; approved collection17/// internals may keep private allocator/count metadata. No value carries a vtable,18/// code/static address, transient allocation/reference, active loan or hidden19/// owning module attachment. Implementations for opaque infrastructure must be20/// reviewed and included in the separate compiled ABI/semantic contracts.21/// The required own interpretation policy covers opaque implementation, Drop,22/// helper and encoded generic contracts. Derives capture the selected declaration;23/// custom comparator/Drop/serializer bodies and active configuration remain24/// explicit inputs of the frozen memory owner. Do not recursively evaluate child25/// policies: compiled admission metadata follows physical typed edges with a26/// visited set. Encoded retained carriers separately qualify their codec payloads;27/// an unused Codec API is not part of a native interpretation policy.28/// Ordinary Rust strong cycles/forgotten values remain ordinary leaks.29/// This recursive contract is also the retained-to-Runtime exclusion proof used30/// when a quiescent KEEP startup discards old Runtime page runs. An unsafe impl31/// must audit opaque kernel edges as well as application fields; matching ABI32/// layouts alone do not prove that exclusion. Invalid unsafe impls or raw codec33/// escapes are outside this contract. No recovery guesses a forgotten retained34/// Rc count or calls an unreachable destructor.35///36/// Global-owned values are rejected even when their lifetime is static:37/// ```compile_fail38/// use cdk_rsx_engine_nyxus::NyxusData;39/// #[derive(NyxusData)]40/// struct Bad { bytes: std::vec::Vec<u8> }41/// ```42/// ```compile_fail43/// use cdk_rsx_engine_nyxus::NyxusData;44/// #[derive(NyxusData)]45/// struct Bad { callback: fn(u64) -> u64 }46/// ```47/// ```compile_fail48/// use cdk_rsx_engine_nyxus::NyxusData;49/// #[derive(NyxusData)]50/// struct Bad { text: &'static str }51/// ```52/// ```compile_fail53/// use cdk_rsx_engine_nyxus::NyxusData;54/// #[derive(NyxusData)]55/// struct Bad { text: std::string::String }56/// ```57/// ```compile_fail58/// use cdk_rsx_engine_nyxus::NyxusData;59/// #[derive(NyxusData)]60/// struct Bad { value: std::rc::Rc<u64> }61/// ```62/// ```compile_fail63/// use cdk_rsx_engine_nyxus::NyxusData;64/// #[derive(NyxusData)]65/// struct Bad { value: std::boxed::Box<u64> }66/// ```67/// ```compile_fail68/// use cdk_rsx_engine_nyxus::NyxusData;69/// #[derive(NyxusData)]70/// struct Bad { pointer: *const u64 }71/// ```72/// Recursion does not admit Global owners hidden inside inline wrappers:73/// ```compile_fail74/// use cdk_rsx_engine_nyxus::NyxusData;75/// #[derive(NyxusData)]76/// struct Bad { nested: Option<[std::vec::Vec<u8>; 2]> }77/// ```78/// ```compile_fail79/// use cdk_rsx_engine_nyxus::NyxusData;80/// #[derive(NyxusData)]81/// struct Bad { nested: Result<std::boxed::Box<u64>, u64> }82/// ```83/// A raw Runtime address cannot acquire admission by being wrapped:84/// ```compile_fail85/// use cdk_rsx_engine_nyxus::NyxusData;86/// #[derive(NyxusData)]87/// struct Bad { runtime: Option<*mut u8> }88/// ```89/// ```compile_fail90/// use cdk_rsx_engine_nyxus::NyxusData;91/// #[derive(NyxusData)]92/// struct Bad { borrowed: Option<&'static [u8]> }93/// ```94/// ```compile_fail95/// use cdk_rsx_engine_nyxus::NyxusData;96/// #[derive(NyxusData)]97/// struct Bad { code: [Option<fn(u64) -> u64>; 2] }98/// ```99/// Interior-mutability cells would permit writes through a read-only data view:100/// ```compile_fail101/// use cdk_rsx_engine_nyxus::NyxusData;102/// #[derive(NyxusData)]103/// struct Bad { value: std::cell::Cell<u64> }104/// ```105/// ```compile_fail106/// use cdk_rsx_engine_nyxus::NyxusData;107/// #[derive(NyxusData)]108/// struct Bad { value: std::cell::RefCell<u64> }109/// ```110/// ```compile_fail111/// use cdk_rsx_engine_nyxus::NyxusData;112/// #[derive(NyxusData)]113/// struct Bad { value: std::cell::UnsafeCell<u64> }114/// ```115/// The associated constant intentionally prevents a dyn-compatible marker:116/// ```compile_fail117/// use cdk_rsx_engine_nyxus::NyxusData;118/// use std::rc::Rc;119/// fn bad(_: Rc<dyn NyxusData>) {}120/// ```121pub unsafe trait NyxusData: 'static {122 /// This type's own interpretation recipe, without recursively evaluating123 /// child policies. The compiled ABI walk collects those children once.124 /// Opaque implementations must cover their implementation, Drop and helpers.125 #[doc(hidden)]126 const __NYXUS_POLICY: [u8; 32];127 #[doc(hidden)]128 const __NYXUS_DATA: () = ();129}130/// Metadata-only bridge. No receiver is dereferenced and no data is accessed.131#[doc(hidden)]132pub trait Interpretation: interpretation_sealed::Sealed {133 fn policy(self: *const Self) -> [u8; 32];134}135mod interpretation_sealed {136 pub trait Sealed {}137 impl<T: super::NyxusData + ?Sized> Sealed for T {}138}139impl<T: NyxusData + ?Sized> Interpretation for T {140 fn policy(self: *const Self) -> [u8; 32] {141 T::__NYXUS_POLICY142 }143}144macro_rules! scalar {145 ($($t:ty),* $(,)?) => {$(146 // SAFETY: these scalar values contain no references or allocations.147 unsafe impl NyxusData for $t { const __NYXUS_POLICY: [u8; 32] = [0; 32]; }148 )*};149}150scalar!(151 (),152 bool,153 char,154 u8,155 u16,156 u32,157 u64,158 u128,159 usize,160 i8,161 i16,162 i32,163 i64,164 i128,165 isize,166 f32,167 f64,168 NonZeroU8,169 NonZeroU16,170 NonZeroU32,171 NonZeroU64,172 NonZeroU128,173 NonZeroUsize,174 NonZeroI8,175 NonZeroI16,176 NonZeroI32,177 NonZeroI64,178 NonZeroI128,179 NonZeroIsize180);181// SAFETY: these inline aggregate/data wrappers own only admitted element data.182unsafe impl<T: NyxusData, const N: usize> NyxusData for [T; N] {183 const __NYXUS_POLICY: [u8; 32] = [0; 32];184}185unsafe impl<T: NyxusData> NyxusData for [T] {186 const __NYXUS_POLICY: [u8; 32] = [0; 32];187}188unsafe impl NyxusData for str {189 const __NYXUS_POLICY: [u8; 32] = [0; 32];190}191unsafe impl<T: NyxusData> NyxusData for Option<T> {192 const __NYXUS_POLICY: [u8; 32] = [0; 32];193}194unsafe impl<T: NyxusData, E: NyxusData> NyxusData for Result<T, E> {195 const __NYXUS_POLICY: [u8; 32] = [0; 32];196}197unsafe impl<T: NyxusData + ?Sized> NyxusData for PhantomData<T> {198 const __NYXUS_POLICY: [u8; 32] = [0; 32];199}200macro_rules! tuple {201 ($($t:ident),+) => {202 // SAFETY: all tuple components satisfy the recursive data contract.203 unsafe impl<$($t: NyxusData),+> NyxusData for ($($t,)+) { const __NYXUS_POLICY: [u8; 32] = [0; 32]; }204 };205}206tuple!(A);207tuple!(A, B);208tuple!(A, B, C);209tuple!(A, B, C, D);210tuple!(A, B, C, D, E);211tuple!(A, B, C, D, E, F);212tuple!(A, B, C, D, E, F, G);213tuple!(A, B, C, D, E, F, G, H);214215// SAFETY: pinned ic_principal0.1.5 Principal is an inline Copy record containing216// only len:u8 and bytes:[u8;29]. Its constructors maintain len<=29. It owns no217// allocation, cell, code/static address or borrowed pointer. The exact external218// physical layout/semantic schema still belongs in the separate ABI certificate.219unsafe impl NyxusData for ic_principal::Principal {220 const __NYXUS_POLICY: [u8; 32] = [0; 32];221}