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
2 changes: 2 additions & 0 deletions .github/workflows/msrv-windows-result.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ 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
1 change: 1 addition & 0 deletions crates/libs/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ windows-interface = { path = "../interface", version = "0.57.0" }
[features]
default = ["std"]
std = []
slim-errors = ["windows-result/slim-errors"]
5 changes: 5 additions & 0 deletions crates/libs/result/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ categories = ["os::windows-apis"]
[features]
default = ["std"]
std = []
# slim-errors reduces the size of Error (and Result<()>) to that of a single HRESULT
slim-errors = []
sivadeilra marked this conversation as resolved.
Show resolved Hide resolved

[lints]
workspace = true
Expand All @@ -21,6 +23,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
Loading