Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Error and Result<()> to be the same size as HRESULT #3126

Merged
merged 17 commits into from
Jun 26, 2024
12 changes: 12 additions & 0 deletions .github/workflows/msrv-windows-result.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ jobs:
run: rustup update --no-self-update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- name: Check
run: cargo check -p windows-result --all-features
- name: Check Default Features
run: cargo check -p windows-result
- name: Check Slim Errors
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$env:RUSTFLAGS = '--cfg=windows_slim_errors'

# This will show the size of Error, which lets us confirm that RUSTFLAGS was set.
cargo test -p windows-result --lib -- --nocapture --test-threads=1

cargo check -p windows-result
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ exclude = [
[workspace.lints.rust]
rust_2018_idioms = { level = "warn", priority = -1 }
missing_docs = "warn"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(windows_raw_dylib, windows_debugger_visualizer)'] }
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(windows_raw_dylib, windows_debugger_visualizer, windows_slim_errors)'] }
14 changes: 11 additions & 3 deletions crates/libs/result/.natvis
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="windows_result::error::Error">
<Expand>
<ExpandedItem>code</ExpandedItem>
<Item Name="[info]">info</Item>
<ExpandedItem>(HRESULT)code.__0.__0,hr</ExpandedItem>
<Item Name="[code]">(HRESULT)code.__0.__0</Item>
<Item Name="[info]">info.ptr</Item>
</Expand>
</Type>

<Type Name="windows_result::hresult::HRESULT">
<DisplayString>{(HRESULT)__0}</DisplayString>
</Type>

<Type Name="windows_result::error::error_info::ErrorInfo">
<DisplayString>ErrorInfo</DisplayString>
<Expand>
<Item Name="[object]">*(void**)&amp;ptr</Item>
</Expand>
</Type>
</AutoVisualizer>
3 changes: 3 additions & 0 deletions crates/libs/result/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ workspace = true
default-target = "x86_64-pc-windows-msvc"
targets = []

[dependencies]
static_assertions = "1.0"

[dependencies.windows-targets]
version = "0.52.5"
path = "../targets"
50 changes: 50 additions & 0 deletions crates/libs/result/src/bstr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use super::*;

#[repr(transparent)]
pub struct BasicString(*const u16);

impl BasicString {
pub fn is_empty(&self) -> bool {
self.len() == 0
}

pub fn len(&self) -> usize {
if self.0.is_null() {
0
} else {
unsafe { SysStringLen(self.0) as usize }
}
}

pub fn as_wide(&self) -> &[u16] {
let len = self.len();
if len != 0 {
unsafe { core::slice::from_raw_parts(self.as_ptr(), len) }
} else {
&[]
}
}

pub fn as_ptr(&self) -> *const u16 {
if !self.is_empty() {
self.0
} else {
const EMPTY: [u16; 1] = [0];
EMPTY.as_ptr()
}
}
}

impl Default for BasicString {
fn default() -> Self {
Self(core::ptr::null_mut())
}
}

impl Drop for BasicString {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe { SysFreeString(self.0) }
}
}
}
Loading