Trait Dump
pub trait Dump<C = ()>: Dumpablewhere
C: ?Sized,{
// Required method
fn dump<'d, W>(
&self,
context: &C,
parent: Option<&Cow<'_, str>>,
doc_overrides: DocOverrides<'d>,
pos: &mut usize,
output: &mut W,
) -> Result<(), Error>
where W: Write;
}Expand description
A functionality to dump the contents of a struct to the output destination.
The context is borrowed and forwarded to nested values. Use &() when no
external information is needed.
§Examples
use grib_template_helpers::{DocOverrides, Dump};
struct VariableLength {
len: u8,
seq: Vec<u8>,
}
impl grib_template_helpers::Dumpable for VariableLength {}
impl Dump for VariableLength {
fn dump<'d, W: std::io::Write>(
&self,
_context: &(),
parent: Option<&std::borrow::Cow<str>>,
doc_overrides: DocOverrides<'d>,
pos: &mut usize,
output: &mut W,
) -> Result<(), std::io::Error> {
writeln!(
output,
"variable-length array (length = {}, content = {:?})",
self.len, self.seq
)
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let var = VariableLength {
len: 3,
seq: vec![1, 2, 3],
};
let mut buf = std::io::Cursor::new(Vec::with_capacity(1024));
let mut pos = 0;
var.dump(&(), None, DocOverrides::empty(), &mut pos, &mut buf)?;
assert_eq!(
String::from_utf8_lossy(buf.get_ref()),
"variable-length array (length = 3, content = [1, 2, 3])\n"
);
Ok(())
}Required Methods§
fn dump<'d, W>(
&self,
context: &C,
parent: Option<&Cow<'_, str>>,
doc_overrides: DocOverrides<'d>,
pos: &mut usize,
output: &mut W,
) -> Result<(), Error>where
W: Write,
fn dump<'d, W>(
&self,
context: &C,
parent: Option<&Cow<'_, str>>,
doc_overrides: DocOverrides<'d>,
pos: &mut usize,
output: &mut W,
) -> Result<(), Error>where
W: Write,
Perform dumping to output.
Users can use the pos argument to output the current byte offset, and
parent to output information about nested structures.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".