Added support for String, Mutex<T> and RwLock<T>

This commit is contained in:
Victor Koenders 2021-10-16 13:19:30 +02:00
parent 33dd4f761d
commit 9cf577d9bc
5 changed files with 84 additions and 4 deletions

View File

@ -23,6 +23,13 @@ pub enum EncodeError {
/// The amount of bytes that were written before the error occured
index: usize,
},
/// The encoder tried to encode a `Mutex` or `RwLock`, but the locking failed
#[cfg(feature = "std")]
LockFailed {
/// The type name of the mutex for debugging purposes
type_name: &'static str,
},
}
/// Errors that can be encounted by decoding a type

View File

@ -5,7 +5,7 @@ use crate::{
error::{DecodeError, EncodeError},
Config,
};
use alloc::{borrow::Cow, boxed::Box, collections::*, rc::Rc, sync::Arc, vec::Vec};
use alloc::{borrow::Cow, boxed::Box, collections::*, rc::Rc, string::String, sync::Arc, vec::Vec};
#[derive(Default)]
struct VecWriter {
@ -178,6 +178,19 @@ where
}
}
impl Decodable for String {
fn decode<D: Decode>(decoder: D) -> Result<Self, DecodeError> {
let bytes = Vec::<u8>::decode(decoder)?;
String::from_utf8(bytes).map_err(|e| DecodeError::Utf8(e.utf8_error()))
}
}
impl Encodeable for String {
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
self.as_bytes().encode(encoder)
}
}
impl<T> Decodable for Box<T>
where
T: Decodable,

View File

@ -4,7 +4,10 @@ use crate::{
enc::{write::Writer, Encode, Encodeable, Encoder},
error::{DecodeError, EncodeError},
};
use std::ffi::{CStr, CString};
use std::{
ffi::{CStr, CString},
sync::{Mutex, RwLock},
};
/// 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> {
@ -105,3 +108,47 @@ impl Decodable for CString {
Ok(cstr.into())
}
}
impl<T> Encodeable for Mutex<T>
where
T: Encodeable,
{
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
let t = self.lock().map_err(|_| EncodeError::LockFailed {
type_name: core::any::type_name::<Mutex<T>>(),
})?;
t.encode(encoder)
}
}
impl<T> Decodable for Mutex<T>
where
T: Decodable,
{
fn decode<D: Decode>(decoder: D) -> Result<Self, DecodeError> {
let t = T::decode(decoder)?;
Ok(Mutex::new(t))
}
}
impl<T> Encodeable for RwLock<T>
where
T: Encodeable,
{
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
let t = self.read().map_err(|_| EncodeError::LockFailed {
type_name: core::any::type_name::<Mutex<T>>(),
})?;
t.encode(encoder)
}
}
impl<T> Decodable for RwLock<T>
where
T: Decodable,
{
fn decode<D: Decode>(decoder: D) -> Result<Self, DecodeError> {
let t = T::decode(decoder)?;
Ok(RwLock::new(t))
}
}

View File

@ -48,6 +48,7 @@ fn test_vec() {
#[test]
fn test_alloc_commons() {
the_same::<Vec<u32>>(vec![1, 2, 3, 4, 5]);
the_same(String::from("Hello world"));
the_same(Box::<u32>::new(5));
the_same(Box::<[u32]>::from(vec![1, 2, 3, 4, 5]));
the_same(Cow::<u32>::Owned(5));

View File

@ -2,6 +2,8 @@
mod utils;
use std::sync::{Mutex, RwLock};
use utils::the_same;
struct Foo {
@ -60,10 +62,20 @@ fn test_std_commons() {
the_same(CString::new("Hello world").unwrap());
let config = bincode::config::Default;
let cstr = CStr::from_bytes_with_nul(b"Hello world\0").unwrap();
let mut buffer = [0u8; 1024];
let cstr = CStr::from_bytes_with_nul(b"Hello world\0").unwrap();
let len = bincode::encode_into_slice_with_config(cstr, &mut buffer, config).unwrap();
let decoded: &CStr = bincode::decode_with_config(&mut buffer[..len], config).unwrap();
assert_eq!(cstr, decoded);
let mutex = Mutex::new("Hello world".to_string());
let len = bincode::encode_into_slice_with_config(&mutex, &mut buffer, config).unwrap();
let decoded: Mutex<String> = bincode::decode_with_config(&mut buffer[..len], config).unwrap();
assert_eq!(&*mutex.lock().unwrap(), &*decoded.lock().unwrap());
let rwlock = RwLock::new("Hello world".to_string());
let len = bincode::encode_into_slice_with_config(&mutex, &mut buffer, config).unwrap();
let decoded: RwLock<String> = bincode::decode_with_config(&mut buffer[..len], config).unwrap();
assert_eq!(&*rwlock.read().unwrap(), &*decoded.read().unwrap());
}