Add Deserialize impl for CStr

Fixes #810
This commit is contained in:
Jon Gjengset
2017-03-08 23:13:28 -05:00
parent a4ee9bd045
commit 0c2fed49e2
3 changed files with 45 additions and 1 deletions
+32
View File
@@ -8,6 +8,9 @@ use std::time::Duration;
use std::default::Default;
use std::ffi::CString;
#[cfg(feature = "unstable")]
use std::ffi::CStr;
extern crate serde;
use serde::Deserialize;
@@ -886,6 +889,13 @@ declare_tests! {
}
}
#[cfg(feature = "unstable")]
#[test]
fn test_cstr() {
assert_de_tokens::<Box<CStr>>(&CString::new("abc").unwrap().into_boxed_c_str(),
&[Token::Bytes(b"abc")]);
}
#[cfg(feature = "unstable")]
#[test]
fn test_net_ipaddr() {
@@ -895,6 +905,28 @@ fn test_net_ipaddr() {
);
}
#[cfg(feature = "unstable")]
#[test]
fn test_cstr_internal_null() {
assert_de_tokens_error::<Box<CStr>>(
&[
Token::Bytes(b"a\0c"),
],
Error::Message("nul byte found in provided data at position: 1".into())
);
}
#[cfg(feature = "unstable")]
#[test]
fn test_cstr_internal_null_end() {
assert_de_tokens_error::<Box<CStr>>(
&[
Token::Bytes(b"ac\0"),
],
Error::Message("nul byte found in provided data at position: 2".into())
);
}
declare_error_tests! {
test_unknown_field<StructDenyUnknown> {
&[