Added documentation for the src/featyres module

This commit is contained in:
Victor Koenders 2021-10-14 18:01:41 +02:00
parent bb98b8fcbd
commit 7b85fc168e
2 changed files with 12 additions and 4 deletions

View File

@ -13,16 +13,18 @@ impl enc::write::Writer for VecWriter {
}
}
/// Encode the given value into a `Vec<u8>`.
pub fn encode_to_vec<E: enc::Encodeable>(val: E) -> Result<Vec<u8>, error::EncodeError> {
encode_to_vec_with_config(val, config::Default)
}
/// Encode the given value into a `Vec<u8>` with the given `Config`. See the [config] module for more information.
pub fn encode_to_vec_with_config<E: enc::Encodeable, C: Config>(
val: E,
_config: C,
config: C,
) -> Result<Vec<u8>, error::EncodeError> {
let writer = VecWriter::default();
let mut encoder = enc::Encoder::<_, C>::new(writer);
let mut encoder = enc::Encoder::<_, C>::new(writer, config);
val.encode(&mut encoder)?;
Ok(encoder.into_writer().inner)
}

View File

@ -5,10 +5,14 @@ use crate::{
error::{DecodeError, EncodeError},
};
/// Decode type `D` from the given reader. The reader can be any type that implements `std::io::Read`, e.g. `std::fs::File`.
pub fn decode_from<D: Decodable, R: std::io::Read>(src: &mut R) -> Result<D, DecodeError> {
decode_from_with_config(src, config::Default)
}
/// Decode type `D` from the given reader with the given `Config`. The reader can be any type that implements `std::io::Read`, e.g. `std::fs::File`.
///
/// See the [config] module for more information about config options.
pub fn decode_from_with_config<D: Decodable, C: Config, R: std::io::Read>(
src: &mut R,
_config: C,
@ -27,6 +31,7 @@ impl<'storage, R: std::io::Read> Reader<'storage> for R {
}
}
/// Encode the given value into any type that implements `std::io::Write`, e.g. `std::fs::File`.
pub fn encode_into_write<E: Encodeable, W: std::io::Write>(
val: E,
dst: &mut W,
@ -34,16 +39,17 @@ pub fn encode_into_write<E: Encodeable, W: std::io::Write>(
encode_into_write_with_config(val, dst, config::Default)
}
/// Encode the given value into any type that implements `std::io::Write`, e.g. `std::fs::File`, with the given `Config`. See the [config] module for more information.
pub fn encode_into_write_with_config<E: Encodeable, C: Config, W: std::io::Write>(
val: E,
dst: &mut W,
_config: C,
config: C,
) -> Result<usize, EncodeError> {
let writer = IoWriter {
writer: dst,
bytes_written: 0,
};
let mut encoder = Encoder::<_, C>::new(writer);
let mut encoder = Encoder::<_, C>::new(writer, config);
val.encode(&mut encoder)?;
Ok(encoder.into_writer().bytes_written)
}