mod.rs
cdk-rsx/crates/cdk-rsx-collections/src/hylix/mod.rs
1//! Offset-backed collections for explicitly stable module records.2//!3//! A carrier contains only fixed encoded metadata. Reads decode bounded records4//! into transient Rust values; none of the references returned here point into5//! stable memory. Mutable guards write back their one record when dropped.6//!7//! Decoding does not acquire ownership and ordinary Rust `Drop` does not release8//! persistent allocations. Removing a value transfers its existing persistent9//! ownership. Retire removed values which are not moved elsewhere, and retire10//! displaced owned fields before replacing them. Read guards deliberately have11//! no consuming conversion. Raw `Codec` use is trusted storage infrastructure.1213mod map;14pub mod ownership;15mod sequence;16mod shared;17mod shared_cell;18mod string;19pub mod transform;2021pub use cdk_rsx_collections_derive::OwnedRecord;22pub use cdk_rsx_engine_hylix::{Codec, HylixValue};23pub use map::{Iter as MapIter, Key, Map, Range as MapRange, Set};24pub use sequence::{Queue, Vec};25pub use shared::{Blob, Cell, Shared, SharedSnapshot};26pub use shared_cell::{SharedCell, SharedCellIdentity, SharedCellSnapshot};27pub use string::String;2829use cdk_rsx_engine_hylix::{Block, ContextLoan, Result, with_storage, with_storage_read};30use core::{31 marker::PhantomData,32 ops::{Deref, DerefMut},33};3435fn must<T>(result: Result<T>) -> T {36 result.unwrap_or_else(|error| std::panic::panic_any(error))37}3839/// Release an actual removed persistent owner in the active write scope.40/// Passing a borrowed decoded snapshot instead violates its ownership contract.41pub fn retire<T: HylixValue>(value: T) {42 must(with_storage(|storage| value.retire(storage)));43}4445/// Replace one owned field of a mutable record and retire its displaced owner.46pub fn replace<T: HylixValue>(slot: &mut T, value: T) {47 retire(core::mem::replace(slot, value));48}4950/// A decoded, borrowed snapshot. The value resides in transient memory.51pub struct Read<'a, T> {52 value: T,53 _loan: Option<shared::CellLoan>,54 borrowed: PhantomData<&'a T>,55}56impl<T> Deref for Read<'_, T> {57 type Target = T;58 fn deref(&self) -> &T {59 &self.value60 }61}62impl<T: core::fmt::Debug> core::fmt::Debug for Read<'_, T> {63 fn fmt(&self, out: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {64 self.value.fmt(out)65 }66}67impl<'a, T> Read<'a, T> {68 pub(super) fn new(value: T) -> Self {69 Self {70 value,71 _loan: None,72 borrowed: PhantomData,73 }74 }75 fn with_loan(value: T, loan: shared::CellLoan) -> Self {76 Self {77 value,78 _loan: Some(loan),79 borrowed: PhantomData,80 }81 }82}8384/// A decoded mutable record, written back to its original slot on drop.85/// Replaced nested owning fields require explicit retirement or transfer.86pub struct Edit<'a, T: Codec> {87 value: T,88 block: Block,89 at: u64,90 _loan: Option<shared::CellLoan>,91 scope: Option<ContextLoan>,92 borrowed: PhantomData<&'a mut T>,93}94impl<T: Codec> Deref for Edit<'_, T> {95 type Target = T;96 fn deref(&self) -> &T {97 &self.value98 }99}100impl<T: Codec> DerefMut for Edit<'_, T> {101 fn deref_mut(&mut self) -> &mut T {102 &mut self.value103 }104}105impl<'a, T: Codec> Edit<'a, T> {106 pub(super) fn new(value: T, block: Block, at: u64) -> Self {107 must(with_storage(|_| Ok(())));108 Self {109 value,110 block,111 at,112 _loan: None,113 scope: Some(must(ContextLoan::new())),114 borrowed: PhantomData,115 }116 }117 fn with_loan(value: T, block: Block, at: u64, loan: shared::CellLoan) -> Self {118 Self {119 value,120 block,121 at,122 _loan: Some(loan),123 scope: None,124 borrowed: PhantomData,125 }126 }127}128impl<T: Codec> Drop for Edit<'_, T> {129 fn drop(&mut self) {130 if let Some(scope) = &self.scope {131 if let Err(error) = scope.check() {132 if std::thread::panicking() {133 return;134 }135 must::<()>(Err(error));136 }137 }138 if let Some(loan) = &self._loan {139 if let Err(error) = loan.check() {140 if std::thread::panicking() {141 return;142 }143 must::<()>(Err(error));144 }145 }146 must(cdk_rsx_engine_hylix::with_encoded(&self.value, |bytes| {147 with_storage(|storage| storage.write(self.block, self.at, bytes))148 }));149 }150}151152fn read_value<T: Codec>(block: Block, at: u64) -> T {153 must(with_storage_read(|storage| {154 cdk_rsx_engine_hylix::read_value(storage, block, at)155 }))156}