Skip to main content

RefWriter

Struct RefWriter 

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

Reference writer for tracking shared references during serialization.

RefWriter maintains a mapping from object pointer addresses to reference IDs, allowing the serialization system to detect when the same object is encountered multiple times and write a reference instead of serializing the object again. This enables proper handling of shared references and circular references.

§Examples

use fory_core::buffer::Writer;
use fory_core::resolver::ref_resolver::RefWriter;
use std::rc::Rc;

let mut ref_writer = RefWriter::new();
let mut buffer = vec![];
let mut writer = Writer::from_buffer(&mut buffer);
let rc = Rc::new(42);

// First encounter - returns false, indicating object should be serialized
assert!(!ref_writer.try_write_rc_ref(&mut writer, &rc));

// Second encounter - returns true, indicating reference was written
let rc2 = rc.clone();
assert!(ref_writer.try_write_rc_ref(&mut writer, &rc2));

Implementations§

Source§

impl RefWriter

Source

pub fn new() -> Self

Creates a new RefWriter instance.

Source

pub fn try_write_rc_ref<T: ?Sized>( &mut self, writer: &mut Writer<'_>, rc: &Rc<T>, ) -> bool

Attempt to write a reference for an Rc<T>.

Returns true if a reference was written (indicating this object has been seen before), false if this is the first occurrence and the object should be serialized normally.

§Arguments
  • writer - The writer to write reference information to
  • rc - The Rc to check for reference tracking
§Returns
  • true if a reference was written
  • false if this is the first occurrence of the object
Source

pub fn try_write_arc_ref<T: ?Sized>( &mut self, writer: &mut Writer<'_>, arc: &Arc<T>, ) -> bool

Attempt to write a reference for an Arc<T>.

Returns true if a reference was written (indicating this object has been seen before), false if this is the first occurrence and the object should be serialized normally.

§Arguments
  • writer - The writer to write reference information to
  • arc - The Arc to check for reference tracking
§Returns
  • true if a reference was written
  • false if this is the first occurrence of the object
Source

pub fn reserve_ref_id(&mut self) -> u32

Reserve a reference ID slot without storing anything.

This is used for xlang compatibility where ALL objects (including struct values, not just Rc/Arc) participate in reference tracking.

§Returns

The reserved reference ID

Source

pub fn reset(&mut self)

Clear all stored references.

This is useful for reusing the RefWriter for multiple serialization operations.

Trait Implementations§

Source§

impl Default for RefWriter

Source§

fn default() -> RefWriter

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

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.