Skip to main content

encode_to_writer

Function encode_to_writer 

Source
pub fn encode_to_writer<W: Write>(value: &DxValue, writer: &mut W) -> Result<()>
Expand description

Encode to a writer with default config

Writes the encoded DX machine format directly to any Write implementor. This is more efficient than encode() when writing to files or network streams, as it avoids an intermediate buffer.

§Example

use serializer::{encode_to_writer, DxValue, DxObject};
use std::io::Cursor;

let mut obj = DxObject::new();
obj.insert("name".to_string(), DxValue::String("Test".to_string()));

let mut buffer = Vec::new();
encode_to_writer(&DxValue::Object(obj), &mut buffer).unwrap();

§Errors

Returns a DxError in the following cases:

  • DxError::Io - Failed to write to the output stream. This can occur when writing to files (disk full, permission denied), network streams (connection closed), or any other I/O operation that fails.