You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
windows-bindgen add a way to convert from an Interface to Option<Interface> #1339
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.
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>]
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.
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
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.
Activity
fowenix commentedon Nov 19, 2021
Doing
&buffer.clone().into()works fine for turning&ID3D11Bufferinto&Option<ID3D11Buffer>kennykerr commentedon Nov 19, 2021
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.
kennykerr commentedon Feb 23, 2022
Sorry for the delay, #1562 adds support for Win32 array parameters.
chyyran commentedon Feb 28, 2022
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 ofTtoOption<T>::Some, or transmute&[T]to&[Option<T>]kennykerr commentedon Feb 28, 2022
Reopening to address this.
kennykerr commentedon Mar 8, 2022
This function is generated as follows:
This is in line with the metadata:
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/win32metadatachyyran commentedon Mar 8, 2022
What if an
IntoParam<Option<T>>implementation is generated for everyTthat 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 asIntoParamwould 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.kennykerr commentedon Mar 9, 2022
Ah that's a good point, thanks for the reminder! Yes, I'll experiment with using
IntoParamfor input arrays.aconverse commentedon Jan 10, 2025
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
&ID3D11Bufferand wants to callVSSetConstantBuffers().Can we get an AsRef to take us from
&ID3D11Bufferto a&Option<ID3D11Buffer>? (Any more genericallythis transformation for any COM object).
Right now the options seem to be:
transmutefrom application codeID3D11Bufferchurning ref countsSee also: #2930 (comment)
zopsicle commentedon Jan 10, 2025
If VSSetConstantBuffers took Option<Ref<ID3D11Buffer>> instead, neither clone nor transmute would be necessary. That would be ideal IMO.
kennykerr commentedon Jan 10, 2025
Yes, I added
Refto deal with such cases. Just need to apply it more broadly.[-]Add a way to convert from an `Interface` to `Option<Interface>`[/-][+]`windows-bindgen` add a way to convert from an `Interface` to `Option<Interface>`[/+]kennykerr commentedon Aug 6, 2026
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 usingRefand 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.