Use the existing CString error message

This commit is contained in:
David Tolnay 2017-03-05 13:59:18 -08:00
parent 2f988aa5e6
commit cc06f070d1
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 4 additions and 5 deletions

View File

@ -305,9 +305,8 @@ impl Deserialize for CString {
fn deserialize<D>(deserializer: D) -> Result<CString, D::Error>
where D: Deserializer
{
let v: Vec<u8> = try!(ByteBuf::deserialize(deserializer)).into();
CString::new(v)
.map_err(|e| Error::custom(format!("unexpected NULL at byte {}", e.nul_position())))
let bytes = try!(ByteBuf::deserialize(deserializer));
CString::new(bytes).map_err(Error::custom)
}
}

View File

@ -1005,12 +1005,12 @@ declare_error_tests! {
&[
Token::Bytes(b"a\0c"),
],
Error::Message("unexpected NULL at byte 1".into()),
Error::Message("nul byte found in provided data at position: 1".into()),
}
test_cstring_internal_null_end<CString> {
&[
Token::Bytes(b"ac\0"),
],
Error::Message("unexpected NULL at byte 2".into()),
Error::Message("nul byte found in provided data at position: 2".into()),
}
}