Added support for Path and PathBuf
This commit is contained in:
parent
acbd385649
commit
26d7683719
|
|
@ -15,6 +15,9 @@ pub enum EncodeError {
|
||||||
type_name: &'static str,
|
type_name: &'static str,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/// An uncommon error occured, see the inner text for more information
|
||||||
|
Other(&'static str),
|
||||||
|
|
||||||
/// The targetted writer encountered an `std::io::Error`
|
/// The targetted writer encountered an `std::io::Error`
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
Io {
|
Io {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ use crate::{
|
||||||
use core::time::Duration;
|
use core::time::Duration;
|
||||||
use std::{
|
use std::{
|
||||||
ffi::{CStr, CString},
|
ffi::{CStr, CString},
|
||||||
|
path::{Path, PathBuf},
|
||||||
sync::{Mutex, RwLock},
|
sync::{Mutex, RwLock},
|
||||||
time::SystemTime,
|
time::SystemTime,
|
||||||
};
|
};
|
||||||
|
|
@ -173,3 +174,32 @@ impl Decodable for SystemTime {
|
||||||
Ok(SystemTime::UNIX_EPOCH + duration)
|
Ok(SystemTime::UNIX_EPOCH + duration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Encodeable for &'_ Path {
|
||||||
|
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||||
|
match self.to_str() {
|
||||||
|
Some(str) => str.encode(encoder),
|
||||||
|
None => Err(EncodeError::Other("Path contains invalid UTF-8 characters")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> BorrowDecodable<'de> for &'de Path {
|
||||||
|
fn borrow_decode<D: BorrowDecode<'de>>(decoder: D) -> Result<Self, DecodeError> {
|
||||||
|
let str = <&'de str>::borrow_decode(decoder)?;
|
||||||
|
Ok(Path::new(str))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encodeable for PathBuf {
|
||||||
|
fn encode<E: Encode>(&self, encoder: E) -> Result<(), EncodeError> {
|
||||||
|
self.as_path().encode(encoder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decodable for PathBuf {
|
||||||
|
fn decode<D: Decode>(decoder: D) -> Result<Self, DecodeError> {
|
||||||
|
let string = std::string::String::decode(decoder)?;
|
||||||
|
Ok(string.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
13
tests/std.rs
13
tests/std.rs
|
|
@ -59,23 +59,34 @@ fn test_std_file() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_std_commons() {
|
fn test_std_commons() {
|
||||||
use std::ffi::{CStr, CString};
|
use std::ffi::{CStr, CString};
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
the_same(CString::new("Hello world").unwrap());
|
the_same(CString::new("Hello world").unwrap());
|
||||||
|
the_same(PathBuf::from("C:/Program Files/Foo"));
|
||||||
|
|
||||||
let config = bincode::config::Default;
|
let config = bincode::config::Default;
|
||||||
let mut buffer = [0u8; 1024];
|
let mut buffer = [0u8; 1024];
|
||||||
|
|
||||||
|
// &CStr
|
||||||
let cstr = CStr::from_bytes_with_nul(b"Hello world\0").unwrap();
|
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 len = bincode::encode_into_slice_with_config(cstr, &mut buffer, config).unwrap();
|
||||||
let decoded: &CStr = bincode::decode_with_config(&mut buffer[..len], config).unwrap();
|
let decoded: &CStr = bincode::decode_with_config(&mut buffer[..len], config).unwrap();
|
||||||
assert_eq!(cstr, decoded);
|
assert_eq!(cstr, decoded);
|
||||||
|
|
||||||
|
// Mutex<T>
|
||||||
let mutex = Mutex::new("Hello world".to_string());
|
let mutex = Mutex::new("Hello world".to_string());
|
||||||
let len = bincode::encode_into_slice_with_config(&mutex, &mut buffer, config).unwrap();
|
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();
|
let decoded: Mutex<String> = bincode::decode_with_config(&mut buffer[..len], config).unwrap();
|
||||||
assert_eq!(&*mutex.lock().unwrap(), &*decoded.lock().unwrap());
|
assert_eq!(&*mutex.lock().unwrap(), &*decoded.lock().unwrap());
|
||||||
|
|
||||||
|
// RwLock<T>
|
||||||
let rwlock = RwLock::new("Hello world".to_string());
|
let rwlock = RwLock::new("Hello world".to_string());
|
||||||
let len = bincode::encode_into_slice_with_config(&mutex, &mut buffer, config).unwrap();
|
let len = bincode::encode_into_slice_with_config(&rwlock, &mut buffer, config).unwrap();
|
||||||
let decoded: RwLock<String> = bincode::decode_with_config(&mut buffer[..len], config).unwrap();
|
let decoded: RwLock<String> = bincode::decode_with_config(&mut buffer[..len], config).unwrap();
|
||||||
assert_eq!(&*rwlock.read().unwrap(), &*decoded.read().unwrap());
|
assert_eq!(&*rwlock.read().unwrap(), &*decoded.read().unwrap());
|
||||||
|
|
||||||
|
// Path
|
||||||
|
let path = Path::new("C:/Program Files/Foo");
|
||||||
|
let len = bincode::encode_into_slice_with_config(path, &mut buffer, config).unwrap();
|
||||||
|
let decoded: &Path = bincode::decode_with_config(&mut buffer[..len], config).unwrap();
|
||||||
|
assert_eq!(path, decoded);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue