Skip to content

Commit 41d2e99

Browse files
authored
Allow Error and Result<()> to be the same size as HRESULT (#3126)
1 parent f69ba3d commit 41d2e99

File tree

11 files changed

+387
-163
lines changed

11 files changed

+387
-163
lines changed

.github/workflows/msrv-windows-result.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,15 @@ jobs:
2727
run: rustup update --no-self-update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
2828
- name: Check
2929
run: cargo check -p windows-result --all-features
30+
- name: Check Default Features
31+
run: cargo check -p windows-result
32+
- name: Check Slim Errors
33+
shell: pwsh
34+
run: |
35+
$ErrorActionPreference = 'Stop'
36+
$env:RUSTFLAGS = '--cfg=windows_slim_errors'
37+
38+
# This will show the size of Error, which lets us confirm that RUSTFLAGS was set.
39+
cargo test -p windows-result --lib -- --nocapture --test-threads=1
40+
41+
cargo check -p windows-result

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ exclude = [
1616
[workspace.lints.rust]
1717
rust_2018_idioms = { level = "warn", priority = -1 }
1818
missing_docs = "warn"
19-
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(windows_raw_dylib, windows_debugger_visualizer)'] }
19+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(windows_raw_dylib, windows_debugger_visualizer, windows_slim_errors)'] }

crates/libs/result/.natvis

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
33
<Type Name="windows_result::error::Error">
44
<Expand>
5-
<ExpandedItem>code</ExpandedItem>
6-
<Item Name="[info]">info</Item>
5+
<ExpandedItem>(HRESULT)code.__0.__0,hr</ExpandedItem>
6+
<Item Name="[code]">(HRESULT)code.__0.__0</Item>
7+
<Item Name="[info]">info.ptr</Item>
78
</Expand>
89
</Type>
910

1011
<Type Name="windows_result::hresult::HRESULT">
1112
<DisplayString>{(HRESULT)__0}</DisplayString>
1213
</Type>
14+
15+
<Type Name="windows_result::error::error_info::ErrorInfo">
16+
<DisplayString>ErrorInfo</DisplayString>
17+
<Expand>
18+
<Item Name="[object]">*(void**)&amp;ptr</Item>
19+
</Expand>
20+
</Type>
1321
</AutoVisualizer>

crates/libs/result/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ workspace = true
2121
default-target = "x86_64-pc-windows-msvc"
2222
targets = []
2323

24+
[dependencies]
25+
static_assertions = "1.0"
26+
2427
[dependencies.windows-targets]
2528
version = "0.52.5"
2629
path = "../targets"

crates/libs/result/src/bstr.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use super::*;
2+
3+
#[repr(transparent)]
4+
pub struct BasicString(*const u16);
5+
6+
impl BasicString {
7+
pub fn is_empty(&self) -> bool {
8+
self.len() == 0
9+
}
10+
11+
pub fn len(&self) -> usize {
12+
if self.0.is_null() {
13+
0
14+
} else {
15+
unsafe { SysStringLen(self.0) as usize }
16+
}
17+
}
18+
19+
pub fn as_wide(&self) -> &[u16] {
20+
let len = self.len();
21+
if len != 0 {
22+
unsafe { core::slice::from_raw_parts(self.as_ptr(), len) }
23+
} else {
24+
&[]
25+
}
26+
}
27+
28+
pub fn as_ptr(&self) -> *const u16 {
29+
if !self.is_empty() {
30+
self.0
31+
} else {
32+
const EMPTY: [u16; 1] = [0];
33+
EMPTY.as_ptr()
34+
}
35+
}
36+
}
37+
38+
impl Default for BasicString {
39+
fn default() -> Self {
40+
Self(core::ptr::null_mut())
41+
}
42+
}
43+
44+
impl Drop for BasicString {
45+
fn drop(&mut self) {
46+
if !self.0.is_null() {
47+
unsafe { SysFreeString(self.0) }
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)