Skip to content

windows-bindgen add a way to convert from an Interface to Option<Interface> #1339

Closed as not planned

Description

Some APIs like VSSetConstantBuffers take an *const Option<ID3D11Buffer>. Given a &ID3D11Buffer I'd like to be able to construct a &Option<ID3D11Buffer> to pass to a function like this.

Activity

  1. fowenix commented on Nov 19, 2021

    @fowenix

    Doing &buffer.clone().into() works fine for turning &ID3D11Buffer into &Option<ID3D11Buffer>

  2. kennykerr commented on Nov 19, 2021

    @kennykerr
    Collaborator

    This API is difficult to use because it really expects an array but the Rust bindings aren't yet adapted to accept a slice.

    This is a duplicate of #479 that I hope to get to soon.

  3. kennykerr commented on Feb 23, 2022

    @kennykerr
    Collaborator

    Sorry for the delay, #1562 adds support for Win32 array parameters.

  4. chyyran commented on Feb 28, 2022

    @chyyran

    Unfortunately #1562 does not solve the core issue. Even for functions that take slices, they expect slices of &[Option<T>], which makes it difficult when you have a &[T]. The .clone().into() trick works well if you have a slice of size 1, but for slices without known size, we either have to map over the slices of T to Option<T>::Some, or transmute &[T] to &[Option<T>]

  5. kennykerr commented on Feb 28, 2022

    @kennykerr
    Collaborator

    Reopening to address this.

  6. kennykerr commented on Mar 8, 2022

    @kennykerr
    Collaborator

    This function is generated as follows:

    pub unsafe fn VSSetConstantBuffers(&self, startslot: u32, ppconstantbuffers: &[::core::option::Option<ID3D11Buffer>]) {
        (::windows::core::Interface::vtable(self).VSSetConstantBuffers)(::core::mem::transmute_copy(self), ::core::mem::transmute(startslot), ppconstantbuffers.len() as _, ::core::mem::transmute(::windows::core::as_ptr_or_null(ppconstantbuffers)))
    }

    This is in line with the metadata:

    unsafe void VSSetConstantBuffers([In] uint StartSlot, [In] uint NumBuffers, [Optional][In][NativeArrayInfo(CountParamIndex = 1)] ID3D11Buffer* ppConstantBuffers);

    Unfortunately, there's nothing in the metadata that says that the interface pointers in the array may not be null, hence the Option. Such a (non-null) option could be explored here: https://github.com/microsoft/win32metadata

  7. chyyran commented on Mar 8, 2022

    @chyyran

    What if an IntoParam<Option<T>> implementation is generated for every T that is exposed by a function as [In][Optional]? The issue here is that it's difficult to pass &[T] into functions that accept &[Option<T>], and likewise for the non-slice variants without transmute. This is a sound-ish transmute in the context of COM and COM-lite APIs but only because Rust guarantees so (kind of) for #[repr(transparent)], and because the input to the transmute is non-null, contract wise it is still wildly unsafe (if the wrapped COM ptr is null, the transmute is UB). Expressing it as IntoParam would codifying this relation in the function contract, but I'm not familiar enough to know if this is feasible codegen wise.

    This wouldn't work for [Out] params for obvious reasons but for [In] params it would eliminate the need to use transmute here.

  8. kennykerr commented on Mar 9, 2022

    @kennykerr
    Collaborator

    Ah that's a good point, thanks for the reminder! Yes, I'll experiment with using IntoParam for input arrays.

  9. aconverse commented on Jan 10, 2025

    @aconverse

    I know this issue is very old. I can open a new issue if that is preferred.

    But this was closed with a change (#2233) that had to be reverted and never re-opened.

    Going back to the original use case: User has &ID3D11Buffer and wants to call VSSetConstantBuffers().

    Can we get an AsRef to take us from &ID3D11Buffer to a &Option<ID3D11Buffer>? (Any more generically
    this transformation for any COM object).

    Right now the options seem to be:

    • Call transmute from application code
    • Clone the ID3D11Buffer churning ref counts

    See also: #2930 (comment)

  10. zopsicle commented on Jan 10, 2025

    @zopsicle

    If VSSetConstantBuffers took Option<Ref<ID3D11Buffer>> instead, neither clone nor transmute would be necessary. That would be ideal IMO.

  11. kennykerr commented on Jan 10, 2025

    @kennykerr
    Collaborator

    Yes, I added Ref to deal with such cases. Just need to apply it more broadly.

  12. changed the title [-]Add a way to convert from an `Interface` to `Option<Interface>`[/-] [+]`windows-bindgen` add a way to convert from an `Interface` to `Option<Interface>`[/+] on Jul 7, 2026
  13. kennykerr commented on Aug 6, 2026

    @kennykerr
    Collaborator

    The issue is valid, but solving it broadly adds disproportionate complexity and hidden behavior. The existing &[Option<T>] projection is the most practical low-level representation that I can see. I experimented with using Ref and param slices but it becomes overly complex with diminishing returns. Your best bet is to wrap these low-level API calls in safe and idiomatic wrapper libraries like windows-canvas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions