add utils::BincodeVecWriter
This commit is contained in:
parent
c326fc59d3
commit
ddd728cd5d
@ -1,14 +1,57 @@
|
|||||||
pub trait ErrorToString {
|
pub trait ErrorToString {
|
||||||
type Output;
|
type Output;
|
||||||
fn str_err(self) -> std::result::Result<Self::Output, String>;
|
fn str_err(self) -> std::result::Result<Self::Output, String>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, E> ErrorToString for std::result::Result<T, E>
|
impl<T, E> ErrorToString for std::result::Result<T, E>
|
||||||
where
|
where
|
||||||
E: std::error::Error,
|
E: std::error::Error,
|
||||||
{
|
{
|
||||||
type Output = T;
|
type Output = T;
|
||||||
fn str_err(self) -> std::result::Result<Self::Output, String> {
|
fn str_err(self) -> std::result::Result<Self::Output, String> {
|
||||||
self.map_err(|e| e.to_string())
|
self.map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "sync", feature = "async"))]
|
||||||
|
mod bincode_utils {
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
|
use bincode::enc::write::Writer;
|
||||||
|
use bincode::error::EncodeError;
|
||||||
|
|
||||||
|
/// struct that allows [`Vec<u8>`] to implement [bincode::enc::write::Writer] trait
|
||||||
|
pub struct BincodeVecWriter {
|
||||||
|
vec: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BincodeVecWriter {
|
||||||
|
pub fn new(vec: Vec<u8>) -> BincodeVecWriter {
|
||||||
|
BincodeVecWriter { vec }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for BincodeVecWriter {
|
||||||
|
type Target = Vec<u8>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.vec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for BincodeVecWriter {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.vec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Writer for BincodeVecWriter {
|
||||||
|
fn write(&mut self, bytes: &[u8]) -> Result<(), EncodeError> {
|
||||||
|
self.vec.extend_from_slice(bytes);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(feature = "sync", feature = "async"))]
|
||||||
|
pub use bincode_utils::BincodeVecWriter;
|
||||||
|
Loading…
Reference in New Issue
Block a user