pub trait FileFormat<T> {
type FormatError: Error;
// Required methods
fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError>;
fn to_writer<W: Write>(
&self,
writer: W,
value: &T,
) -> Result<(), Self::FormatError>;
// Provided methods
fn from_reader_buffered<R: Read>(
&self,
reader: R,
) -> Result<T, Self::FormatError> { ... }
fn from_buffer(&self, buf: &[u8]) -> Result<T, Self::FormatError> { ... }
fn to_writer_buffered<W: Write>(
&self,
writer: W,
value: &T,
) -> Result<(), Self::FormatError> { ... }
fn to_buffer(&self, value: &T) -> Result<Vec<u8>, Self::FormatError> { ... }
}
Expand description
A trait that describes how a file’s contents should be interpreted.
Usually, you will want to implement a simple wrapper over your file format’s
to_writer
and from_reader
functions, using your favorite serialization framework.
§Example
struct Json;
impl<T> FileFormat<T> for Json
where T: Serialize + DeserializeOwned {
type FormatError = serde_json::Error;
fn to_writer<W: Write>(&self, writer: W, value: &T) -> Result<(), Self::FormatError> {
serde_json::to_writer_pretty(writer, value).map_err(From::from)
}
fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError> {
serde_json::from_reader(reader).map_err(From::from)
}
}
Required Associated Types§
Sourcetype FormatError: Error
type FormatError: Error
The type of error to return from to_writer
and from_reader
.
Required Methods§
Sourcefn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError>
fn from_reader<R: Read>(&self, reader: R) -> Result<T, Self::FormatError>
Deserialize a value from a Read
stream.
If you are reading directly from a File
, you should consider
using from_reader_buffered
instead.
Sourcefn to_writer<W: Write>(
&self,
writer: W,
value: &T,
) -> Result<(), Self::FormatError>
fn to_writer<W: Write>( &self, writer: W, value: &T, ) -> Result<(), Self::FormatError>
Serialize a value into a Write
stream.
If you are writing directly to a File
, you should consider
using to_writer_buffered
instead.
Provided Methods§
Sourcefn from_reader_buffered<R: Read>(
&self,
reader: R,
) -> Result<T, Self::FormatError>
fn from_reader_buffered<R: Read>( &self, reader: R, ) -> Result<T, Self::FormatError>
Identical to FileFormat::from_reader
, however the provided reader is buffered with BufReader
.
You should override this function if your file format reads to a buffer internally in order to avoid double-buffering.
Sourcefn from_buffer(&self, buf: &[u8]) -> Result<T, Self::FormatError>
fn from_buffer(&self, buf: &[u8]) -> Result<T, Self::FormatError>
Deserialize a value from a byte vec.
Sourcefn to_writer_buffered<W: Write>(
&self,
writer: W,
value: &T,
) -> Result<(), Self::FormatError>
fn to_writer_buffered<W: Write>( &self, writer: W, value: &T, ) -> Result<(), Self::FormatError>
Identical to FileFormat::to_writer
, however the provided writer is buffered with BufWriter
.
You should override this function if your file format writes to a buffer internally in order to avoid double-buffering.
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.