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
impl RefWriter
Sourcepub fn try_write_rc_ref<T: ?Sized>(
&mut self,
writer: &mut Writer<'_>,
rc: &Rc<T>,
) -> bool
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 torc- The Rc to check for reference tracking
§Returns
trueif a reference was writtenfalseif this is the first occurrence of the object
Sourcepub fn try_write_arc_ref<T: ?Sized>(
&mut self,
writer: &mut Writer<'_>,
arc: &Arc<T>,
) -> bool
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 toarc- The Arc to check for reference tracking
§Returns
trueif a reference was writtenfalseif this is the first occurrence of the object
Sourcepub fn reserve_ref_id(&mut self) -> u32
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