pub trait Format: Send + Sync {
// Required method
fn write_record(
&self,
record: &Record<'_>,
writer: &mut dyn Write,
) -> Result;
// Provided method
fn write_record_generic<W: Write>(
&self,
record: &Record<'_>,
writer: &mut W,
) -> Result
where Self: Sized { ... }
}Expand description
A record-to-text serializer.
Implementors write exactly one logical record per call, including
any trailing newline: the built-in sinks add no separators between
records. Formatters write to core::fmt::Write (not
std::io::Write) so they work under no_std and can format into a
String or a fixed buffer.
Only Format::write_record is required. Override
Format::write_record_generic as well if your format is on a hot
path and you want the built-in sinks to call it without dynamic
dispatch.
§Examples
use core::fmt;
use log_io::format::Format;
use log_io::{Level, Metadata, Record};
struct Minimal;
impl Format for Minimal {
fn write_record(&self, record: &Record<'_>, w: &mut dyn fmt::Write) -> fmt::Result {
writeln!(w, "{} {}", record.metadata.level, record.message)
}
}
let record = Record::new(Metadata::new(Level::Info, "app"), "ready", &[]);
let mut out = String::new();
Minimal.write_record(&record, &mut out)?;
assert_eq!(out, "info ready\n");
// The trait is object-safe, so formats can be picked at run time.
let chosen: Box<dyn Format> = Box::new(Minimal);
out.clear();
chosen.write_record(&record, &mut out)?;
assert_eq!(out, "info ready\n");Required Methods§
Sourcefn write_record(&self, record: &Record<'_>, writer: &mut dyn Write) -> Result
fn write_record(&self, record: &Record<'_>, writer: &mut dyn Write) -> Result
Write record to writer.
§Errors
Returns core::fmt::Error if the writer rejects a write (or
if a lazily formatted value’s Display / Debug impl fails).
Provided Methods§
Sourcefn write_record_generic<W: Write>(
&self,
record: &Record<'_>,
writer: &mut W,
) -> Resultwhere
Self: Sized,
fn write_record_generic<W: Write>(
&self,
record: &Record<'_>,
writer: &mut W,
) -> Resultwhere
Self: Sized,
Statically dispatched variant of Format::write_record.
The built-in sinks call this method. The default implementation
forwards to write_record; formats that care about the cost of
dynamic dispatch override it with a generic implementation.
§Errors
Same as Format::write_record.
Trait Implementations§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".