riffdb-types 0.1.0

First-party RiffDB runtime crate (riffdb-types)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Closed strong ownership for shared semantic allocations.
//!
//! No raw Arc or Weak can escape. Without a closed weak handle, final Arc
//! storage retires before its semantic value and lifecycle charge. A closed
//! weak handle separately retains prior allowance for the complete residual
//! allocation until the final weak handle is destroyed.

use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::sync::{Arc, Weak};

/// First-party shared ownership; this handle grants no database authority.
#[doc(hidden)]
pub struct OwnedCapacitySharedV1<T>(Option<Arc<T>>);

impl<T> OwnedCapacitySharedV1<T> {
    pub(crate) fn from_allocation_handle(allocation: Option<Arc<T>>) -> Self {
        Self(allocation)
    }

    /// Construction must occur under the containing owner's prior admission.
    pub fn new(value: T) -> Self {
        Self(Some(Arc::new(value)))
    }

    /// Tests process-local allocation identity without exposing a raw pointer.
    pub fn same_allocation(&self, other: &Self) -> bool {
        Arc::ptr_eq(self.inner(), other.inner())
    }

    /// Creates a weak relationship without exposing either native handle.
    /// The caller must first admit the complete residual allocation and the
    /// control allocation on the actual containing owner's physical family.
    /// This operation and subsequent clones allocate no memory.
    pub fn downgrade_with_control<C>(
        &self,
        control: OwnedCapacitySharedV1<C>,
    ) -> OwnedCapacityWeakV1<T, C> {
        OwnedCapacityWeakV1 {
            weak: Some(Arc::downgrade(self.inner())),
            control,
        }
    }

    /// Erases only the borrowed downcast type; retains this same physical Arc.
    pub fn into_erased(mut self) -> OwnedCapacityErasedV1
    where
        T: std::any::Any + Send + Sync,
    {
        OwnedCapacityErasedV1(self.0.take().map(|owner| ErasedOwner::Shared(owner)))
    }

    /// Exact old strong-count observation only. This cannot mint a transfer proof;
    /// mutation/extraction still validates ownership with the checked operations.
    pub fn is_uniquely_owned(&self) -> bool {
        Arc::strong_count(self.inner()) == 1
    }

    /// Mutable access exists only while this is the actual sole strong owner.
    pub fn get_mut(&mut self) -> Option<&mut T> {
        Arc::get_mut(self.0.as_mut()?)
    }

    /// Atomically releases this owner and extracts T only for the final owner.
    /// Used by iterative immutable ancestry retirement; no failed-owner race.
    pub fn into_value_on_final_owner(mut self) -> Option<T> {
        self.0.take().and_then(Arc::into_inner)
    }

    /// Consumes the actual sole strong owner. Outstanding closed weak handles
    /// can retain the separately admitted residual allocation after extraction.
    /// An alias refusal returns the same closed allocation without modifying T.
    pub fn try_into_value(mut self) -> Result<T, Self> {
        let Some(allocation) = self.0.take() else {
            return Err(self);
        };
        match Arc::try_unwrap(allocation) {
            Ok(value) => Ok(value),
            Err(allocation) => {
                self.0 = Some(allocation);
                Err(self)
            }
        }
    }

    pub(crate) fn allocation_address(&self) -> usize {
        Arc::as_ptr(self.inner()).cast::<()>() as usize
    }

    fn inner(&self) -> &Arc<T> {
        self.0.as_ref().expect("live closed shared owner")
    }
}

impl<T> Clone for OwnedCapacitySharedV1<T> {
    fn clone(&self) -> Self {
        Self(Some(self.inner().clone()))
    }
}

impl<T> Deref for OwnedCapacitySharedV1<T> {
    type Target = T;
    fn deref(&self) -> &T {
        self.inner()
    }
}

impl<T> AsRef<T> for OwnedCapacitySharedV1<T> {
    fn as_ref(&self) -> &T {
        self
    }
}

impl<T> Drop for OwnedCapacitySharedV1<T> {
    fn drop(&mut self) {
        if let Some(allocation) = self.0.take() {
            // into_inner releases this strong owner before returning T. Any
            // closed weak backing remains charged by its own control owner.
            drop(Arc::into_inner(allocation));
        }
    }
}

/// Closed weak ownership of an already admitted allocation. This handle grants
/// no database authority and exposes no raw Arc, Weak, address or mutable value.
/// The numerical control must not contain a back-reference to the observed T.
#[doc(hidden)]
pub struct OwnedCapacityWeakV1<T, C> {
    // Drop the actual native weak handle before the backing allowance.
    weak: Option<Weak<T>>,
    control: OwnedCapacitySharedV1<C>,
}

impl<T, C> OwnedCapacityWeakV1<T, C> {
    /// Tests only process-local allocation identity, including after T dies.
    pub fn same_allocation(&self, other: &OwnedCapacitySharedV1<T>) -> bool {
        self.weak
            .as_ref()
            .is_some_and(|weak| std::ptr::eq(weak.as_ptr(), Arc::as_ptr(other.inner())))
    }

    /// A live T still retains its original admission. This creates no new
    /// allocation, control or authority; it only retains that same strong owner.
    pub fn upgrade(&self) -> Option<OwnedCapacitySharedV1<T>> {
        self.weak
            .as_ref()?
            .upgrade()
            .map(|owner| OwnedCapacitySharedV1::from_allocation_handle(Some(owner)))
    }
}

impl<T, C> Clone for OwnedCapacityWeakV1<T, C> {
    fn clone(&self) -> Self {
        Self {
            weak: self.weak.clone(),
            control: self.control.clone(),
        }
    }
}

impl<T, C> Drop for OwnedCapacityWeakV1<T, C> {
    fn drop(&mut self) {
        // Native weak retirement precedes the closed control's final retirement.
        // The latter uses the existing into_inner protocol, not ordinary Arc Drop.
        drop(self.weak.take());
    }
}

impl<T: fmt::Debug> fmt::Debug for OwnedCapacitySharedV1<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.deref().fmt(formatter)
    }
}
impl<T: PartialEq> PartialEq for OwnedCapacitySharedV1<T> {
    fn eq(&self, other: &Self) -> bool {
        self.deref().eq(other.deref())
    }
}
impl<T: Eq> Eq for OwnedCapacitySharedV1<T> {}
impl<T: PartialOrd> PartialOrd for OwnedCapacitySharedV1<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.deref().partial_cmp(other.deref())
    }
}
impl<T: Ord> Ord for OwnedCapacitySharedV1<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.deref().cmp(other.deref())
    }
}
impl<T: Hash> Hash for OwnedCapacitySharedV1<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.deref().hash(state)
    }
}

/// Sized ownership shell for immutable text and its exact destruction control.
#[doc(hidden)]
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct OwnedCapacityTextV1(OwnedCapacitySharedV1<TextValue>);
#[derive(Eq, Hash, Ord, PartialEq, PartialOrd)]
struct TextValue {
    text: Box<str>,
    lifecycle: crate::OwnedCapacityLifecycleV1,
}
crate::inspect_owned_capacity_struct_v1!(TextValue {
    owned: [text],
    inline: [],
    lifecycle: lifecycle
});
impl OwnedCapacityTextV1 {
    /// Pure retained-storage quote for this concrete closed text representation.
    /// Includes its Arc shell, boxed UTF-8, lifecycle control and the inspector's
    /// shared attachment bookkeeping. Construction scratch is quoted by callers.
    pub fn retained_allocation_upper_bound(
        text_bytes: usize,
    ) -> Result<u64, crate::OwnedCapacityInspectionErrorV1> {
        use crate::owned_capacity::{SHARED_ALLOCATION_BOOKKEEPING_BYTES, allocation_upper_bound};
        use std::mem::{align_of, size_of};

        let inline = size_of::<TextValue>()
            .checked_add(2 * size_of::<usize>())
            .ok_or(crate::OwnedCapacityInspectionErrorV1)?;
        let shell = allocation_upper_bound(inline, align_of::<TextValue>())?;
        let text = allocation_upper_bound(text_bytes, align_of::<u8>())?;
        let lifecycle = crate::OwnedCapacityLifecycleV1::control_allocation_upper_bound()?;
        SHARED_ALLOCATION_BOOKKEEPING_BYTES
            .checked_add(shell)
            .and_then(|bytes| bytes.checked_add(text))
            .and_then(|bytes| bytes.checked_add(lifecycle))
            .ok_or(crate::OwnedCapacityInspectionErrorV1)
    }

    pub fn new(text: String) -> Self {
        Self(OwnedCapacitySharedV1::new(TextValue {
            text: text.into_boxed_str(),
            lifecycle: Default::default(),
        }))
    }
}
impl Deref for OwnedCapacityTextV1 {
    type Target = str;
    fn deref(&self) -> &str {
        &self.0.text
    }
}
impl AsRef<str> for OwnedCapacityTextV1 {
    fn as_ref(&self) -> &str {
        self
    }
}
impl fmt::Debug for OwnedCapacityTextV1 {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.deref(), formatter)
    }
}
impl fmt::Display for OwnedCapacityTextV1 {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self.deref(), formatter)
    }
}
impl crate::InspectOwnedCapacityV1 for OwnedCapacityTextV1 {
    fn inspect_owned_capacity(
        &self,
        inspection: &mut crate::OwnedCapacityInspectionV1,
    ) -> Result<(), crate::OwnedCapacityInspectionErrorV1> {
        inspection.shared_closed(&self.0)
    }
}

/// Sized ownership shell for one immutable slice and its destruction control.
#[doc(hidden)]
pub struct OwnedCapacitySliceV1<T>(OwnedCapacitySharedV1<SliceValue<T>>);
struct SliceValue<T> {
    values: Box<[T]>,
    lifecycle: crate::OwnedCapacityLifecycleV1,
}
impl<T: crate::InspectOwnedCapacityV1> crate::InspectOwnedCapacityV1 for SliceValue<T> {
    fn allocation_lifecycle(&self) -> Option<&crate::OwnedCapacityLifecycleV1> {
        Some(&self.lifecycle)
    }
    fn inspect_owned_capacity(
        &self,
        inspection: &mut crate::OwnedCapacityInspectionV1,
    ) -> Result<(), crate::OwnedCapacityInspectionErrorV1> {
        let Self { values, lifecycle } = self;
        inspection.visit(values)?;
        inspection.visit(lifecycle)
    }
}
impl<T> OwnedCapacitySliceV1<T> {
    pub fn same_allocation(&self, other: &Self) -> bool {
        self.0.same_allocation(&other.0)
    }
    pub fn new(values: Vec<T>) -> Self {
        Self(OwnedCapacitySharedV1::new(SliceValue {
            values: values.into_boxed_slice(),
            lifecycle: Default::default(),
        }))
    }
}
impl<T> Clone for OwnedCapacitySliceV1<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}
impl<T> Deref for OwnedCapacitySliceV1<T> {
    type Target = [T];
    fn deref(&self) -> &[T] {
        &self.0.values
    }
}
impl<T> AsRef<[T]> for OwnedCapacitySliceV1<T> {
    fn as_ref(&self) -> &[T] {
        self
    }
}
impl<T: fmt::Debug> fmt::Debug for OwnedCapacitySliceV1<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.deref().fmt(formatter)
    }
}
impl<T: PartialEq> PartialEq for OwnedCapacitySliceV1<T> {
    fn eq(&self, other: &Self) -> bool {
        self.deref().eq(other.deref())
    }
}
impl<T: Eq> Eq for OwnedCapacitySliceV1<T> {}
impl<T: PartialOrd> PartialOrd for OwnedCapacitySliceV1<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.deref().partial_cmp(other.deref())
    }
}
impl<T: Ord> Ord for OwnedCapacitySliceV1<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.deref().cmp(other.deref())
    }
}
impl<T: Hash> Hash for OwnedCapacitySliceV1<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.deref().hash(state)
    }
}
impl<T: crate::InspectOwnedCapacityV1> crate::InspectOwnedCapacityV1 for OwnedCapacitySliceV1<T> {
    fn inspect_owned_capacity(
        &self,
        inspection: &mut crate::OwnedCapacityInspectionV1,
    ) -> Result<(), crate::OwnedCapacityInspectionErrorV1> {
        inspection.shared_closed(&self.0)
    }
}

impl<T> From<Vec<T>> for OwnedCapacitySliceV1<T> {
    fn from(values: Vec<T>) -> Self {
        Self::new(values)
    }
}
impl<T> From<Box<[T]>> for OwnedCapacitySliceV1<T> {
    fn from(values: Box<[T]>) -> Self {
        Self::new(values.into_vec())
    }
}
impl<T, const N: usize> From<[T; N]> for OwnedCapacitySliceV1<T> {
    fn from(values: [T; N]) -> Self {
        Self::new(Vec::from(values))
    }
}
impl<T: Clone> From<&[T]> for OwnedCapacitySliceV1<T> {
    fn from(values: &[T]) -> Self {
        Self::new(values.to_vec())
    }
}

// Only these private vtables may consume an erased Arc. Each concrete method
// retires its strong handle before its value and charge. Outstanding closed
// weak backing retains its separate control; it grants no erased access.
trait SharedErasure: Send + Sync {
    fn as_any(&self) -> &(dyn std::any::Any + Send + Sync);
    fn retire(self: Arc<Self>);
}
impl<T: std::any::Any + Send + Sync> SharedErasure for T {
    fn as_any(&self) -> &(dyn std::any::Any + Send + Sync) {
        self
    }
    fn retire(self: Arc<Self>) {
        drop(Arc::into_inner(self));
    }
}
trait AllocationErasure: Send + Sync {
    fn as_any(&self) -> &(dyn std::any::Any + Send + Sync);
    fn retire(self: Arc<Self>);
}
impl<T: std::any::Any + Send + Sync> AllocationErasure
    for crate::exact_provider_budget_v1::ProviderAllocation<T>
{
    fn as_any(&self) -> &(dyn std::any::Any + Send + Sync) {
        &**self
    }
    fn retire(self: Arc<Self>) {
        drop(Arc::into_inner(self));
    }
}
enum ErasedOwner {
    Shared(Arc<dyn SharedErasure>),
    Allocation(Arc<dyn AllocationErasure>),
}
/// A closed erased owner with borrow-only downcasts and concrete retirement.
#[doc(hidden)]
pub struct OwnedCapacityErasedV1(Option<ErasedOwner>);
impl OwnedCapacityErasedV1 {
    pub(crate) fn from_allocation<T: std::any::Any + Send + Sync>(
        allocation: Option<Arc<crate::exact_provider_budget_v1::ProviderAllocation<T>>>,
    ) -> Self {
        Self(allocation.map(|owner| ErasedOwner::Allocation(owner)))
    }
    /// Borrows semantic T; a provider allocation's numerical shell stays private.
    pub fn downcast_ref<T: std::any::Any>(&self) -> Option<&T> {
        match self.0.as_ref()? {
            ErasedOwner::Shared(owner) => owner.as_ref().as_any().downcast_ref(),
            ErasedOwner::Allocation(owner) => owner.as_ref().as_any().downcast_ref(),
        }
    }
}
impl Clone for OwnedCapacityErasedV1 {
    fn clone(&self) -> Self {
        Self(self.0.as_ref().map(|owner| match owner {
            ErasedOwner::Shared(owner) => ErasedOwner::Shared(owner.clone()),
            ErasedOwner::Allocation(owner) => ErasedOwner::Allocation(owner.clone()),
        }))
    }
}
impl Drop for OwnedCapacityErasedV1 {
    fn drop(&mut self) {
        match self.0.take() {
            Some(ErasedOwner::Shared(owner)) => owner.retire(),
            Some(ErasedOwner::Allocation(owner)) => owner.retire(),
            None => {}
        }
    }
}
impl fmt::Debug for OwnedCapacityErasedV1 {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("OwnedCapacityErasedV1([REDACTED])")
    }
}
// External crates can only provide the two concrete closed handles below.
mod erased_seal {
    pub trait Sealed {}
}
/// Closed zero-allocation type erasure for admitted first-party owners.
#[doc(hidden)]
pub trait IntoOwnedCapacityErasedV1: erased_seal::Sealed {
    fn into_owned_capacity_erased(self) -> OwnedCapacityErasedV1;
}
impl<T: std::any::Any + Send + Sync> erased_seal::Sealed for OwnedCapacitySharedV1<T> {}
impl<T: std::any::Any + Send + Sync> IntoOwnedCapacityErasedV1 for OwnedCapacitySharedV1<T> {
    fn into_owned_capacity_erased(self) -> OwnedCapacityErasedV1 {
        self.into_erased()
    }
}
impl<T: std::any::Any + Send + Sync> erased_seal::Sealed
    for crate::exact_provider_budget_v1::ProviderAllocationOwner<T>
{
}
impl<T: std::any::Any + Send + Sync> IntoOwnedCapacityErasedV1
    for crate::exact_provider_budget_v1::ProviderAllocationOwner<T>
{
    fn into_owned_capacity_erased(self) -> OwnedCapacityErasedV1 {
        self.into_erased()
    }
}

#[cfg(test)]
#[path = "owned_capacity_weak_tests.rs"]
mod weak_tests;