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

Translate (In-only?) MemorySize(BytesParamIndex = N) void pointers to &[u8]? #3229

Closed
MarijnS95 opened this issue Aug 27, 2024 · 3 comments
Closed
Labels
question Further information is requested

Comments

@MarijnS95
Copy link
Contributor

MarijnS95 commented Aug 27, 2024

Suggestion

In Windows.Win32.Graphics.Direct3D.Fxc there's this function in the metadata:

[DllImport("D3DCOMPILER_47.dll", ExactSpelling = true, PreserveSig = false)]
[Documentation("https://learn.microsoft.com/windows/win32/api/d3dcompiler/nf-d3dcompiler-d3dcompile")]
public unsafe static extern HRESULT D3DCompile(
    [In][Const][MemorySize(BytesParamIndex = 1)] void* pSrcData,
    [In] UIntPtr SrcDataSize,
    ...
);

In windows-rs that currently takes a raw pointer and a slice:

pub unsafe fn D3DCompile<P0, P1, P2, P3>(
    psrcdata: *const c_void,
    srcdatasize: usize,
    ...
);

Would it be nice to pass a sized slice borrow of u8 here instead?

#2106 is perhaps related, but seems to mostly discuss preallocated out slices that would have to use MaybeUninit<u8> to allow the caller to skip initialization.

@kennykerr
Copy link
Collaborator

There were some false positives that made this impractical last I checked, but I can check again to see what impact this has.

@kennykerr kennykerr added question Further information is requested and removed enhancement New feature or request labels Sep 3, 2024
@kennykerr
Copy link
Collaborator

An example is a function like DwmGetWindowAttribute and its pvAttribute parameter that really isn't meant to be treated as a byte array/slice. This parameter is correctly defined as void* since it is polymorphic.

Functions that use MemorySize should use a byte parameter type. This signals that they are meant to be treated as byte slices rather than opaque or polymorphic pointer types.

I would suggest that windows-bindgen is right to be conservative here and that if you would like to pursue this further that you consider improvements to the Win32 metadata for functions that use void when they should be using byte. This would then translate to a &[u8] or &mut [u8] parameter in Rust.

@kennykerr
Copy link
Collaborator

Ideally, the MemorySize attribute would be removed from non-byte parameters like DwmGetWindowAttribute. I can't imagine they are useful in any language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants