pub trait WriteGro<'a, A: 'a> {
// Required methods
fn title(&self) -> String;
fn natoms(&self) -> usize;
fn atoms(&'a self) -> impl Iterator<Item = &'a A>;
fn boxvecs(&self) -> String;
fn format_atom_line(atom: &A) -> String;
// Provided methods
fn format_atom_lines_iter(&'a self) -> impl Iterator<Item = String> { ... }
fn format_atom_lines(&'a self) -> String { ... }
fn write(&'a self, writer: &mut impl Write) -> Result<()> { ... }
fn save_gro<P: AsRef<Path>>(&'a self, path: P) -> Result<()> { ... }
}Expand description
A trait for writing out a value as gro using parallel formatting.
For multi-threaded formatting, see WriteGroPar.
WriteGro implements robust default implementations for writing to generic writers
(WriteGro::write) and to files (WriteGro::save_gro).
To implement this trait for a type, WriteGro::title, WriteGro::natoms,
WriteGro::atoms, WriteGro::format_atom_line, and WriteGro::boxvecs need to be
specified. The default write implementations are based on these building blocks.
Required Methods§
Sourcefn natoms(&self) -> usize
fn natoms(&self) -> usize
Return the number of atoms in this value.
§Invariant
The returned number of atoms must be the same as the number of items yielded by
WriteGro::atoms
Sourcefn atoms(&'a self) -> impl Iterator<Item = &'a A>
fn atoms(&'a self) -> impl Iterator<Item = &'a A>
Return an iterator over &A.
This &A item represents an atom to be formatted with WriteGro::format_atom_line.
§Invariant
The returned iterator must yield exactly the number of atoms specified by
WriteGro::natoms.
Sourcefn boxvecs(&self) -> String
fn boxvecs(&self) -> String
Format this value’s box vectors for gro.
§Note
A valid box vector is either three or nine space-separated floats.
Sourcefn format_atom_line(atom: &A) -> String
fn format_atom_line(atom: &A) -> String
Format a &A as a gro atom line.
The line always has a trailing newline.
Provided Methods§
Sourcefn format_atom_lines_iter(&'a self) -> impl Iterator<Item = String>
fn format_atom_lines_iter(&'a self) -> impl Iterator<Item = String>
An iterator over Strings representing this value’s atoms as the atoms section of a gro
file.
Each line in the iterator has a trailing newline.
Sourcefn format_atom_lines(&'a self) -> String
fn format_atom_lines(&'a self) -> String
Format this value’s atoms as the atoms section of a gro file.
The last line always has a trailing newline.
Sourcefn write(&'a self, writer: &mut impl Write) -> Result<()>
fn write(&'a self, writer: &mut impl Write) -> Result<()>
Write this value as gro.
It is prudent to use this function over a buffered writer.
For a variant that uses multi-threaded formatting, see WriteGroPar::write_par.
§Examples
Structure implements WriteGro.
use eightyseven::structure::{Atom, BoxVecs, Structure};
use eightyseven::writer::WriteGro;
use glam::Vec3;
let structure = Structure {
title: "a simple structure".to_string(),
atoms: vec![
Atom {
resnum: 1,
resname: "MET".into(),
atomname: "CA".into(),
atomnum: 1,
position: Vec3::ZERO,
velocity: Vec3::ZERO,
}
],
boxvecs: BoxVecs::Short([10.0, 10.0, 10.0]),
};
let writer = std::fs::File::create("simple.gro").unwrap();
let mut writer = std::io::BufWriter::new(writer);
structure.write(&mut writer).unwrap();Sourcefn save_gro<P: AsRef<Path>>(&'a self, path: P) -> Result<()>
fn save_gro<P: AsRef<Path>>(&'a self, path: P) -> Result<()>
Write this value to a gro file at a path.
For a variant that uses multi-threaded formatting, see WriteGroPar::save_gro_par.
§Examples
Structure implements WriteGro.
use eightyseven::structure::{Atom, BoxVecs, Structure};
use eightyseven::writer::WriteGro;
use glam::Vec3;
let structure = Structure {
title: "a simple structure".to_string(),
atoms: vec![
Atom {
resnum: 1,
resname: "MET".into(),
atomname: "CA".into(),
atomnum: 1,
position: Vec3::ZERO,
velocity: Vec3::ZERO,
}
],
boxvecs: BoxVecs::Short([10.0, 10.0, 10.0]),
};
let mut writer = std::fs::File::create("simple.gro").unwrap();
structure.write(&mut writer).unwrap();Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.