From 42f05aa40210b22bfdb0d7b837525998d128131a Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Mon, 18 Jul 2022 11:17:57 -0700 Subject: [PATCH] Use internal `Decode` helper in a few more places (#1918) --- .github/workflows/build.yml | 1 - .github/workflows/test.yml | 1 - crates/libs/bindgen/src/replacements/bstr.rs | 6 +----- crates/libs/windows/src/Windows/Win32/Foundation/mod.rs | 6 +----- crates/libs/windows/src/core/bindings.rs | 6 +----- crates/libs/windows/src/core/strings/hstring.rs | 6 +----- crates/libs/windows/src/core/strings/mod.rs | 5 ++++- crates/tests/core/tests/hstrings.rs | 7 +++++++ crates/tests/hstring/Cargo.toml | 8 -------- crates/tests/hstring/src/lib.rs | 1 - crates/tests/hstring/tests/display.rs | 8 -------- 11 files changed, 15 insertions(+), 40 deletions(-) delete mode 100644 crates/tests/hstring/Cargo.toml delete mode 100644 crates/tests/hstring/src/lib.rs delete mode 100644 crates/tests/hstring/tests/display.rs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f936e9e410..4208014f82 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -154,7 +154,6 @@ jobs: cargo clippy -p test_event && cargo clippy -p test_handles && cargo clippy -p test_helpers && - cargo clippy -p test_hstring && cargo clippy -p test_identity && cargo clippy -p test_implement && cargo clippy -p test_interface && diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 27c4f07de1..3b49c496d3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -138,7 +138,6 @@ jobs: cargo test --target ${{ matrix.target }} -p test_event && cargo test --target ${{ matrix.target }} -p test_handles && cargo test --target ${{ matrix.target }} -p test_helpers && - cargo test --target ${{ matrix.target }} -p test_hstring && cargo test --target ${{ matrix.target }} -p test_identity && cargo test --target ${{ matrix.target }} -p test_implement && cargo test --target ${{ matrix.target }} -p test_interface && diff --git a/crates/libs/bindgen/src/replacements/bstr.rs b/crates/libs/bindgen/src/replacements/bstr.rs index 9069e1e40e..f06131fbe9 100644 --- a/crates/libs/bindgen/src/replacements/bstr.rs +++ b/crates/libs/bindgen/src/replacements/bstr.rs @@ -87,11 +87,7 @@ pub fn gen() -> TokenStream { } impl ::core::fmt::Display for BSTR { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - use ::core::fmt::Write; - for c in ::core::char::decode_utf16(self.as_wide().iter().cloned()) { - f.write_char(c.map_err(|_| ::core::fmt::Error)?)? - } - Ok(()) + ::core::write!(f, "{}", ::windows::core::Decode(|| ::core::char::decode_utf16(self.as_wide().iter().cloned()))) } } impl ::core::fmt::Debug for BSTR { diff --git a/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs b/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs index 6fa1b5155e..84bdeac48e 100644 --- a/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs +++ b/crates/libs/windows/src/Windows/Win32/Foundation/mod.rs @@ -293,11 +293,7 @@ impl ::core::default::Default for BSTR { } impl ::core::fmt::Display for BSTR { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - use core::fmt::Write; - for c in ::core::char::decode_utf16(self.as_wide().iter().cloned()) { - f.write_char(c.map_err(|_| ::core::fmt::Error)?)? - } - Ok(()) + ::core::write!(f, "{}", ::windows::core::Decode(|| ::core::char::decode_utf16(self.as_wide().iter().cloned()))) } } impl ::core::fmt::Debug for BSTR { diff --git a/crates/libs/windows/src/core/bindings.rs b/crates/libs/windows/src/core/bindings.rs index 6832e4c188..8c1536656c 100644 --- a/crates/libs/windows/src/core/bindings.rs +++ b/crates/libs/windows/src/core/bindings.rs @@ -1227,11 +1227,7 @@ impl ::core::default::Default for BSTR { } impl ::core::fmt::Display for BSTR { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - use core::fmt::Write; - for c in ::core::char::decode_utf16(self.as_wide().iter().cloned()) { - f.write_char(c.map_err(|_| ::core::fmt::Error)?)? - } - Ok(()) + ::core::write!(f, "{}", ::windows::core::Decode(|| ::core::char::decode_utf16(self.as_wide().iter().cloned()))) } } impl ::core::fmt::Debug for BSTR { diff --git a/crates/libs/windows/src/core/strings/hstring.rs b/crates/libs/windows/src/core/strings/hstring.rs index 7cbd11c00c..4103ae54a9 100644 --- a/crates/libs/windows/src/core/strings/hstring.rs +++ b/crates/libs/windows/src/core/strings/hstring.rs @@ -134,11 +134,7 @@ unsafe impl Sync for HSTRING {} impl core::fmt::Display for HSTRING { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - use core::fmt::Write; - for c in core::char::decode_utf16(self.as_wide().iter().cloned()) { - f.write_char(c.unwrap_or(core::char::REPLACEMENT_CHARACTER))? - } - Ok(()) + write!(f, "{}", Decode(|| core::char::decode_utf16(self.as_wide().iter().cloned()))) } } diff --git a/crates/libs/windows/src/core/strings/mod.rs b/crates/libs/windows/src/core/strings/mod.rs index df4a03a20c..e3b53bb9c2 100644 --- a/crates/libs/windows/src/core/strings/mod.rs +++ b/crates/libs/windows/src/core/strings/mod.rs @@ -16,12 +16,15 @@ pub use pwstr::*; use super::*; extern "C" { + #[doc(hidden)] pub fn strlen(s: PCSTR) -> usize; + #[doc(hidden)] pub fn wcslen(s: PCWSTR) -> usize; } /// An internal helper for decoding an iterator of chars and displaying them -struct Decode(F); +#[doc(hidden)] +pub struct Decode(pub F); impl core::fmt::Display for Decode where diff --git a/crates/tests/core/tests/hstrings.rs b/crates/tests/core/tests/hstrings.rs index 33a3faa493..e6305fe4bf 100644 --- a/crates/tests/core/tests/hstrings.rs +++ b/crates/tests/core/tests/hstrings.rs @@ -36,6 +36,13 @@ fn display_format() { assert!(format!("{}", value) == "Hello world"); } +#[test] +fn display_invalid_format() { + let s = HSTRING::from_wide(&[0xD834, 0xDD1E, 0x006d, 0x0075, 0x0073, 0xDD1E, 0x0069, 0x0063, 0xD834]); + let d = format!("{}", s); + assert_eq!(d, "𝄞mus�ic�"); +} + #[test] fn debug_format() { let value = HSTRING::from("Hello world"); diff --git a/crates/tests/hstring/Cargo.toml b/crates/tests/hstring/Cargo.toml deleted file mode 100644 index 8bc30fc25a..0000000000 --- a/crates/tests/hstring/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "test_hstring" -version = "0.0.0" -authors = ["Microsoft"] -edition = "2018" - -[dependencies.windows] -path = "../../libs/windows" diff --git a/crates/tests/hstring/src/lib.rs b/crates/tests/hstring/src/lib.rs deleted file mode 100644 index 8b13789179..0000000000 --- a/crates/tests/hstring/src/lib.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/crates/tests/hstring/tests/display.rs b/crates/tests/hstring/tests/display.rs deleted file mode 100644 index 527153db9b..0000000000 --- a/crates/tests/hstring/tests/display.rs +++ /dev/null @@ -1,8 +0,0 @@ -use windows::core::*; - -#[test] -fn display() { - let s = HSTRING::from_wide(&[0xD834, 0xDD1E, 0x006d, 0x0075, 0x0073, 0xDD1E, 0x0069, 0x0063, 0xD834]); - let d = format!("{}", s); - assert_eq!(d, "𝄞mus�ic�"); -}