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

Functions taking a pointer to an array of pointers get confusing signatures #2105

Closed
Muon opened this issue Oct 19, 2022 · 7 comments · Fixed by #2233
Closed

Functions taking a pointer to an array of pointers get confusing signatures #2105

Muon opened this issue Oct 19, 2022 · 7 comments · Fixed by #2233
Labels
enhancement New feature or request

Comments

@Muon
Copy link

Muon commented Oct 19, 2022

I've run into an issue with ID3D11DeviceContext::OMSetRenderTargets() where the original takes a pointer to an array of const pointers to ID3D11RenderTargetView, but the Rust version takes an Option<&[Option<ID3D11RenderTargetView>]>. As best as I can tell, it should really just be taking an &[ID3D11RenderTargetView], because it certainly makes no sense to be passing in NULLs in that API. In addition, the current version complicates things by requiring you to move ID3D11RenderTargetView to pass it in. If it took an &[ID3D11RenderTargetView] instead, you could avoid the move by using std::slice::from_ref().

@kennykerr kennykerr added the enhancement New feature or request label Oct 19, 2022
@kennykerr
Copy link
Collaborator

The general problem is that the metadata doesn't indicate that the API expects non-null COM interface pointers so it assumes they can all be null, and thus uses Option. We might argue that array parameters should be non-null as any null COM interfaces pointers can simply be omitted. I'm not sure whether that logic is universally applicable, but it sounds reasonable at first glance.

@Muon
Copy link
Author

Muon commented Oct 20, 2022

I suppose there are two parts here:

  1. The metadata should probably indicate that ID3D11DeviceContext::OMSetRenderTargets() expects an array of non-null pointers.
  2. Input-only array parameters should probably be translated as &[T] instead of Option<&[T]>. (The empty slice indicates an empty array.)

@kennykerr
Copy link
Collaborator

The metadata should probably indicate that ID3D11DeviceContext::OMSetRenderTargets() expects an array of non-null pointers.

That would be great but an uphill battle that is unlikely to be comprehensive.

Input-only array parameters should probably be translated as &[T] instead of Option<&[T]>. (The empty slice indicates an empty array.)

Yes, that may be possible. I can experiment to see how effective/practical that may be.

@kennykerr
Copy link
Collaborator

This looks much like #1339.

@kennykerr
Copy link
Collaborator

IDCompositionDevice.CreateTransform3DGroup is another good example as its typically passed an array of derived interfaces.

@Muon
Copy link
Author

Muon commented Mar 12, 2023

Unfortunate that #2233 had to be reverted. Currently I have to use OMSetRenderTargets like so:

self.context.OMSetRenderTargets(Some(&[Some(self.render_target_view.clone())]), None)

In addition to it being quite ugly, the signature currently forces an unnecessary clone. It would be strictly easier for me to use without the Options getting in the way:

self.context.OMSetRenderTargets(&self.render_target_view as *mut _, std::ptr::null_mut())

or even

self.context.OMSetRenderTargets(std::slice::from_ref(&self.render_target_view).as_mut_ptr(), std::ptr::null_mut())

@kennykerr
Copy link
Collaborator

There may be further options here now that I've dramatically simplified the parameter binding machinery (#2360).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants