Skip to main content

plasma_prp/core/
keyed_object.rs

1//! hsKeyedObject — base class for all objects in the Plasma key registry.
2//!
3//! Every persistent object in Plasma inherits from hsKeyedObject, which provides
4//! a plKey (identity handle) and Read/Write that serializes the self-key.
5//!
6//! C++ ref: pnKeyedObject/hsKeyedObject.h/.cpp
7
8use std::io::Read;
9
10use anyhow::Result;
11
12use super::key::Key;
13use super::uoid::{Uoid, read_key_uoid};
14
15/// Trait for types that have a key identity in the Plasma object system.
16///
17/// Corresponds to hsKeyedObject in C++.
18pub trait KeyedObject {
19    /// Get this object's key.
20    fn key(&self) -> &Key;
21
22    /// Get a mutable reference to this object's key.
23    fn key_mut(&mut self) -> &mut Key;
24
25    /// Set this object's key.
26    fn set_key(&mut self, key: Key);
27}
28
29/// Read the hsKeyedObject portion of a stream — just the self-key reference.
30/// Returns the Uoid of the self-key.
31pub fn read_keyed_object(reader: &mut impl Read) -> Result<Option<Uoid>> {
32    read_key_uoid(reader)
33}
34
35/// Common base data for keyed objects — stores the key.
36#[derive(Debug, Clone, Default)]
37pub struct KeyedObjectData {
38    pub key: Key,
39}
40
41impl KeyedObjectData {
42    pub fn new() -> Self {
43        Self { key: Key::null() }
44    }
45
46    pub fn with_key(key: Key) -> Self {
47        Self { key }
48    }
49}
50
51impl KeyedObject for KeyedObjectData {
52    fn key(&self) -> &Key {
53        &self.key
54    }
55
56    fn key_mut(&mut self) -> &mut Key {
57        &mut self.key
58    }
59
60    fn set_key(&mut self, key: Key) {
61        self.key = key;
62    }
63}