Skip to content

Commit

Permalink
Deserialize Box<Path> through PathBuf::into_boxed_path
Browse files Browse the repository at this point in the history
Including Rc<Path> et al.

Fixes #1633
  • Loading branch information
heftig committed Oct 22, 2019
1 parent 42990d8 commit b8772a1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion serde/build.rs
Expand Up @@ -29,8 +29,9 @@ fn main() {
println!("cargo:rustc-cfg=core_reverse");
}

// CString::into_boxed_c_str stabilized in Rust 1.20:
// CString::into_boxed_c_str and PathBuf::into_boxed_path stabilized in Rust 1.20:
// https://doc.rust-lang.org/std/ffi/struct.CString.html#method.into_boxed_c_str
// https://doc.rust-lang.org/std/path/struct.PathBuf.html#method.into_boxed_path
if minor >= 20 {
println!("cargo:rustc-cfg=de_boxed_c_str");
}
Expand Down
3 changes: 3 additions & 0 deletions serde/src/de/impls.rs
Expand Up @@ -1610,6 +1610,9 @@ impl<'de> Deserialize<'de> for PathBuf {
}
}

#[cfg(all(feature = "std", de_boxed_c_str))]
forwarded_impl!((), Box<Path>, PathBuf::into_boxed_path);

////////////////////////////////////////////////////////////////////////////////

// If this were outside of the serde crate, it would just use:
Expand Down
14 changes: 14 additions & 0 deletions test_suite/tests/test_de.rs
Expand Up @@ -907,6 +907,20 @@ declare_tests! {
Token::ByteBuf(b"/usr/local/lib"),
],
}
test_boxed_path {
PathBuf::from("/usr/local/lib").into_boxed_path() => &[
Token::Str("/usr/local/lib"),
],
PathBuf::from("/usr/local/lib").into_boxed_path() => &[
Token::String("/usr/local/lib"),
],
PathBuf::from("/usr/local/lib").into_boxed_path() => &[
Token::Bytes(b"/usr/local/lib"),
],
PathBuf::from("/usr/local/lib").into_boxed_path() => &[
Token::ByteBuf(b"/usr/local/lib"),
],
}
test_cstring {
CString::new("abc").unwrap() => &[
Token::Bytes(b"abc"),
Expand Down

0 comments on commit b8772a1

Please sign in to comment.