pub struct ConFrameWriter<W: Write> { /* private fields */ }Expand description
A writer that can serialize and write ConFrame objects to any output stream.
This struct encapsulates a writer (like a file) and provides a high-level API
for writing simulation frames in the .con format.
§Example
let mut writer = ConFrameWriter::from_path("output.con").unwrap();
writer.extend(frames.iter()).unwrap();Implementations§
Source§impl<W: Write> ConFrameWriter<W>
impl<W: Write> ConFrameWriter<W>
Sourcepub fn new(writer: W) -> Self
pub fn new(writer: W) -> Self
Creates a new ConFrameWriter that wraps a given writer.
§Arguments
writer- Any type that implementsstd::io::Write, e.g., aFile.
Sourcepub fn with_precision(writer: W, precision: usize) -> Self
pub fn with_precision(writer: W, precision: usize) -> Self
Creates a new ConFrameWriter with a custom floating-point precision.
§Arguments
writer- Any type that implementsstd::io::Write.precision- Number of decimal places for floating-point output.
Sourcepub fn with_float_format(writer: W, float_format: FloatFormat) -> Self
pub fn with_float_format(writer: W, float_format: FloatFormat) -> Self
Creates a writer with an explicit numeric text representation.
FloatFormat::RoundTrip retains small forces and coordinates exactly.
Metadata floats use the JSON serializer independently of this setting.
Sourcepub fn parallel_atoms(self, n: usize) -> Self
pub fn parallel_atoms(self, n: usize) -> Self
Format a wide coordinate block on n workers.
n < 2 leaves the serial loop. The bytes match that loop: each
worker formats a contiguous run of atoms and the runs are joined
in file order.
Sourcepub fn map_rows(self, on: bool) -> Self
pub fn map_rows(self, on: bool) -> Self
Copy formatted rows into a shared mapping of stream_file.
The wide path does this on its own when a spare physical core is free. Tests turn it on for short frames so an odd atom count still takes the mapping, including on a runner with no spare core.
Sourcepub fn mapped_file(self, file: File) -> Self
pub fn mapped_file(self, file: File) -> Self
File the mapping copies into. Open it read/write; map_rows selects
the mapping even when Self::parallel_atoms would otherwise pwrite.
Sourcepub fn canonical(self, on: bool) -> Self
pub fn canonical(self, on: bool) -> Self
Opt-in canonical serialization: BTree-ordered metadata keys in JSON,
fixed section order. Use for corpus
materialization and content-stable hashes; not required for on-disk fidelity
of spans preserved from next_with_raw_span.
Sourcepub fn set_canonical(&mut self, on: bool)
pub fn set_canonical(&mut self, on: bool)
Set or clear canonical mode on an existing writer (C ABI / FFI).
Sourcepub fn is_canonical(&self) -> bool
pub fn is_canonical(&self) -> bool
Whether canonical serialization is enabled.
Sourcepub fn write_frame_binding(
&mut self,
frame: &mut ConFrame,
) -> Result<(), WriteError>
pub fn write_frame_binding( &mut self, frame: &mut ConFrame, ) -> Result<(), WriteError>
Writes frame with the binding metadata stamp and the original atoms.
The stamp is installed for the duration of the write and then put back. Atoms are not rebuilt.
Sourcepub fn write_frame_bound(&mut self, frame: &ConFrame) -> Result<(), WriteError>
pub fn write_frame_bound(&mut self, frame: &ConFrame) -> Result<(), WriteError>
Self::write_frame_binding without a mutable frame.
The header the file emits is the binding stamp. frame’s atoms and
its stored header are left as the caller passed them, so a wide frame
is not cloned before its rows are formatted.
Sourcepub fn write_frame(&mut self, frame: &ConFrame) -> Result<(), WriteError>
pub fn write_frame(&mut self, frame: &ConFrame) -> Result<(), WriteError>
Writes a single ConFrame to the output stream.
Source§impl ConFrameWriter<File>
impl ConFrameWriter<File>
Sourcepub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self>
Creates a new ConFrameWriter that writes to a file at the given path.
This is a convenience function that creates the file and wraps it.
Sourcepub fn from_path_with_precision<P: AsRef<Path>>(
path: P,
precision: usize,
) -> Result<Self>
pub fn from_path_with_precision<P: AsRef<Path>>( path: P, precision: usize, ) -> Result<Self>
Creates a new ConFrameWriter that writes to a file with a custom precision.
Sourcepub fn from_path_append<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn from_path_append<P: AsRef<Path>>(path: P) -> Result<Self>
Opens an uncompressed file for appending.
The file is created when it does not exist. A non-empty file that does not end in a newline receives one, so the next frame header starts on its own line. A file this writer produced already ends in a newline, and appending the remaining frames reproduces one whole-file write. Gzip and zstd paths are refused: a compressed member cannot be extended.
write_frame fills a one-frame scratch buffer and a std::io::BufWriter.
Self::flush pushes that buffer to the file before the writer is dropped.
use readcon_core::iterators::read_all_frames;
use readcon_core::types::ConFrameBuilder;
use readcon_core::writer::ConFrameWriter;
let path = std::env::temp_dir().join(format!(
"readcon-append-doc-{}",
std::process::id()
));
let mut builder = ConFrameBuilder::new([10.0, 10.0, 10.0], [90.0, 90.0, 90.0]);
builder.add_atom("H", 0.0, 0.0, 0.0, [false; 3], 1, 1.0);
let frame = builder.build().unwrap();
{
let mut writer = ConFrameWriter::from_path(&path).unwrap();
writer.write_frame(&frame).unwrap();
writer.flush().unwrap();
}
{
let mut writer = ConFrameWriter::from_path_append(&path).unwrap();
writer.write_frame(&frame).unwrap();
writer.flush().unwrap();
}
let frames = read_all_frames(&path).unwrap();
assert_eq!(frames.len(), 2);
let _ = std::fs::remove_file(&path);