Skip to main content

to_string_tracking_shared

Function to_string_tracking_shared 

Source
pub fn to_string_tracking_shared<T>(value: &T) -> Result<String>
where T: ?Sized + Serialize,
Available on crate feature std only.
Expand description

Serialize a Rust type to a YAML string with automatic anchor/alias emission for shared Rc and Arc pointers wrapped in RcAnchor / ArcAnchor.

During this call, every RcAnchor / ArcAnchor whose pointer is seen for the first time emits a YAML anchor (&idNNN); every subsequent sighting of the same pointer emits an alias (*idNNN). This preserves true DAG structure in the emitted document — Rc::clone siblings become alias references instead of duplicated subtrees.

Pointer identity is tracked via a thread-local scratchpad that is installed for the duration of the call and cleared on return. Plain to_string behaviour is unaffected.

§Errors

Returns an error if the type cannot be serialized to YAML.

§Examples

use noyalib::{to_string_tracking_shared, RcAnchor};
use std::rc::Rc;

let shared: RcAnchor<String> = RcAnchor::from("hello".to_string());
let doc = vec![shared.clone(), shared.clone(), shared];
let yaml = to_string_tracking_shared(&doc).unwrap();
assert!(yaml.contains("&id001"));
assert!(yaml.contains("*id001"));