Skip to main content

RefReader

Struct RefReader 

Source
pub struct RefReader { /* private fields */ }
Expand description

Reference reader for resolving shared references during deserialization.

RefReader maintains a vector of previously deserialized objects that can be referenced by ID. When a reference is encountered during deserialization, the RefReader can return the previously deserialized object instead of deserializing it again.

§Examples

use fory_core::resolver::ref_resolver::RefReader;
use std::rc::Rc;

let mut ref_reader = RefReader::new();
let rc = Rc::new(42);

// Store an object for later reference
let ref_id = ref_reader.store_rc_ref(rc.clone());

// Retrieve the object by reference ID
let retrieved = ref_reader.get_rc_ref::<i32>(ref_id).unwrap();
assert!(Rc::ptr_eq(&rc, &retrieved));

Implementations§

Source§

impl RefReader

Source

pub fn new() -> Self

Creates a new RefReader instance.

Source

pub fn reserve_ref_id(&mut self) -> u32

Reserve a reference ID slot without storing anything yet.

Returns the reserved reference ID that will be used when storing the object later.

Source

pub fn store_rc_ref_at<T: 'static + ?Sized>(&mut self, ref_id: u32, rc: Rc<T>)

Store an Rc<T> at a previously reserved reference ID.

§Arguments
  • ref_id - The reference ID that was reserved
  • rc - The Rc to store
Source

pub fn store_rc_ref<T: 'static + ?Sized>(&mut self, rc: Rc<T>) -> u32

Store an Rc<T> for later reference resolution during deserialization.

§Arguments
  • rc - The Rc to store for later reference
§Returns

The reference ID that can be used to retrieve this object later

Source

pub fn store_arc_ref_at<T: 'static + ?Sized>( &mut self, ref_id: u32, arc: Arc<T>, )

Store an Arc<T> at a previously reserved reference ID.

§Arguments
  • ref_id - The reference ID that was reserved
  • arc - The Arc to store
Source

pub fn store_arc_ref<T: 'static + ?Sized>(&mut self, arc: Arc<T>) -> u32

Store an Arc<T> for later reference resolution during deserialization.

§Arguments
  • arc - The Arc to store for later reference
§Returns

The reference ID that can be used to retrieve this object later

Source

pub fn get_rc_ref<T: 'static + ?Sized>(&self, ref_id: u32) -> Option<Rc<T>>

Get an Rc<T> by reference ID during deserialization.

§Arguments
  • ref_id - The reference ID returned by store_rc_ref
§Returns
  • Some(Rc<T>) if the reference ID is valid and the type matches
  • None if the reference ID is invalid or the type doesn’t match
Source

pub fn get_arc_ref<T: 'static + ?Sized>(&self, ref_id: u32) -> Option<Arc<T>>

Get an Arc<T> by reference ID during deserialization.

§Arguments
  • ref_id - The reference ID returned by store_arc_ref
§Returns
  • Some(Arc<T>) if the reference ID is valid and the type matches
  • None if the reference ID is invalid or the type doesn’t match
Source

pub fn add_callback(&mut self, callback: Box<dyn FnOnce(&RefReader)>)

Add a callback to be executed when weak references are resolved.

§Arguments
  • callback - A closure that takes a reference to the RefReader
Source

pub fn read_ref_flag(&self, reader: &mut Reader<'_>) -> Result<RefFlag, Error>

Read a reference flag and determine what action to take.

§Arguments
  • reader - The reader to read the reference flag from
§Returns

The RefFlag indicating what type of reference this is

§Errors

Errors if an invalid reference flag value is encountered

Source

pub fn read_ref_id(&self, reader: &mut Reader<'_>) -> Result<u32, Error>

Read a reference ID from the reader.

§Arguments
  • reader - The reader to read the reference ID from
§Returns

The reference ID as a u32

Source

pub fn resolve_callbacks(&mut self)

Execute all pending callbacks to resolve weak pointer references.

This should be called after deserialization completes to update any weak pointers that referenced objects which were not yet available during deserialization.

Source

pub fn reset(&mut self)

Clear all stored references and callbacks.

This is useful for reusing the RefReader for multiple deserialization operations.

Trait Implementations§

Source§

impl Default for RefReader

Source§

fn default() -> RefReader

Returns the “default value” for a type. Read more
Source§

impl Send for RefReader

Source§

impl Sync for RefReader

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.