From 9835e2ae736235810b4ea1c162ca5e65c547e770 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 04:49:50 +0200 Subject: Merging upstream version 1.71.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/camino/src/lib.rs | 24 ++++++++ vendor/camino/src/serde_impls.rs | 125 +++++++++++++++++++++++++++++++++++++++ vendor/camino/src/tests.rs | 8 +++ 3 files changed, 157 insertions(+) (limited to 'vendor/camino/src') diff --git a/vendor/camino/src/lib.rs b/vendor/camino/src/lib.rs index 44684b096..86a58ee51 100644 --- a/vendor/camino/src/lib.rs +++ b/vendor/camino/src/lib.rs @@ -484,6 +484,14 @@ impl Deref for Utf8PathBuf { } } +/// *Requires Rust 1.68 or newer.* +#[cfg(path_buf_deref_mut)] +impl std::ops::DerefMut for Utf8PathBuf { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { Utf8Path::assume_utf8_mut(&mut self.0) } + } +} + impl fmt::Debug for Utf8PathBuf { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) @@ -1457,6 +1465,11 @@ impl Utf8Path { // *const Path to a *const Utf8Path is valid. &*(path as *const Path as *const Utf8Path) } + + #[cfg(path_buf_deref_mut)] + unsafe fn assume_utf8_mut(path: &mut Path) -> &mut Utf8Path { + &mut *(path as *mut Path as *mut Utf8Path) + } } impl Clone for Box { @@ -2220,6 +2233,17 @@ impl Utf8DirEntry { pub fn into_inner(self) -> fs::DirEntry { self.inner } + + /// Returns the full path to the file that this entry represents. + /// + /// This is analogous to [`path`], but moves ownership of the path. + /// + /// [`path`]: struct.Utf8DirEntry.html#method.path + #[inline] + #[must_use = "`self` will be dropped if the result is not used"] + pub fn into_path(self) -> Utf8PathBuf { + self.path + } } impl From for Utf8PathBuf { diff --git a/vendor/camino/src/serde_impls.rs b/vendor/camino/src/serde_impls.rs index d4c4ecab9..49b6f8621 100644 --- a/vendor/camino/src/serde_impls.rs +++ b/vendor/camino/src/serde_impls.rs @@ -55,3 +55,128 @@ impl Serialize for Utf8Path { self.as_str().serialize(serializer) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::Utf8PathBuf; + use serde_bytes::ByteBuf; + + #[test] + fn valid_utf8() { + let valid_utf8 = &["", "bar", "💩"]; + for input in valid_utf8 { + let encode = Encode { + path: ByteBuf::from(*input), + }; + let encoded = bincode::serialize(&encode).expect("encoded correctly"); + + assert_valid_utf8::(input, &encoded); + assert_valid_utf8::(input, &encoded); + } + } + + fn assert_valid_utf8<'de, T: TestTrait<'de>>(input: &str, encoded: &'de [u8]) { + let output = bincode::deserialize::(encoded).expect("valid UTF-8 should be fine"); + assert_eq!( + output.path(), + input, + "for input, with {}, paths should match", + T::description() + ); + } + + #[test] + fn invalid_utf8() { + let invalid_utf8: &[(&[u8], _, _)] = &[ + (b"\xff", 0, 1), + (b"foo\xfe", 3, 1), + (b"a\xC3\xA9 \xED\xA0\xBD\xF0\x9F\x92\xA9", 4, 1), + ]; + + for (input, valid_up_to, error_len) in invalid_utf8 { + let encode = Encode { + path: ByteBuf::from(*input), + }; + let encoded = bincode::serialize(&encode).expect("encoded correctly"); + + assert_invalid_utf8::(input, &encoded, *valid_up_to, *error_len); + assert_invalid_utf8::(input, &encoded, *valid_up_to, *error_len) + } + } + + fn assert_invalid_utf8<'de, T: TestTrait<'de>>( + input: &[u8], + encoded: &'de [u8], + valid_up_to: usize, + error_len: usize, + ) { + let error = bincode::deserialize::(encoded).expect_err("invalid UTF-8 should error out"); + let utf8_error = match *error { + bincode::ErrorKind::InvalidUtf8Encoding(utf8_error) => utf8_error, + other => panic!( + "for input {:?}, with {}, expected ErrorKind::InvalidUtf8Encoding, found: {}", + input, + T::description(), + other + ), + }; + assert_eq!( + utf8_error.valid_up_to(), + valid_up_to, + "for input {:?}, with {}, valid_up_to didn't match", + input, + T::description(), + ); + assert_eq!( + utf8_error.error_len(), + Some(error_len), + "for input {:?}, with {}, error_len didn't match", + input, + T::description(), + ); + } + + #[derive(Serialize, Debug)] + struct Encode { + path: ByteBuf, + } + + trait TestTrait<'de>: Deserialize<'de> + fmt::Debug { + fn description() -> &'static str; + fn path(&self) -> &Utf8Path; + } + + #[derive(Deserialize, Debug)] + #[allow(unused)] + struct DecodeOwned { + path: Utf8PathBuf, + } + + impl<'de> TestTrait<'de> for DecodeOwned { + fn description() -> &'static str { + "DecodeOwned" + } + + fn path(&self) -> &Utf8Path { + &self.path + } + } + + #[derive(Deserialize, Debug)] + #[allow(unused)] + struct DecodeBorrowed<'a> { + #[serde(borrow)] + path: &'a Utf8Path, + } + + impl<'de> TestTrait<'de> for DecodeBorrowed<'de> { + fn description() -> &'static str { + "DecodeBorrowed" + } + + fn path(&self) -> &Utf8Path { + self.path + } + } +} diff --git a/vendor/camino/src/tests.rs b/vendor/camino/src/tests.rs index 305b76091..e84c227af 100644 --- a/vendor/camino/src/tests.rs +++ b/vendor/camino/src/tests.rs @@ -44,3 +44,11 @@ where { let _ = orig.into(); } + +#[cfg(path_buf_deref_mut)] +#[test] +fn test_deref_mut() { + // This test is mostly for miri. + let mut path_buf = Utf8PathBuf::from("foobar"); + let _: &mut Utf8Path = &mut path_buf; +} -- cgit v1.2.3