microsoft / windows-rs

Rust for Windows
https://kennykerr.ca/rust-getting-started/
Apache License 2.0
10.23k stars 476 forks source link

Using `OutRef` for generated bindings #3152

Closed lifers closed 1 month ago

lifers commented 1 month ago

Suggestion

I am trying to implement this kind of runtimeclass and was confused about how to write an out parameter from Rust.

namespace example
{
  runtimeclass Simple
  {
    Simple();
    void Repeat(UInt8 input, out IVectorView<UInt8> output);
  }
}

From #3025, I suppose using OutRef will be best. However, the generated bindings doesn't use it, and I'm not sure how to use OutRef.write() in my implementation.

...
pub trait ISimple_Impl: Sized {
    fn Repeat(
        &self,
        input: u8,
        output: &mut Option<windows::Foundation::Collections::IVectorView<u8>>,
    ) -> windows_core::Result<()>;
}
impl windows_core::RuntimeName for ISimple {
    const NAME: &'static str = "example.ISimple";
}
impl ISimple_Vtbl {
    pub const fn new<Identity: windows_core::IUnknownImpl, const OFFSET: isize>() -> ISimple_Vtbl
    where
        Identity: ISimple_Impl,
    {
        unsafe extern "system" fn Repeat<
            Identity: windows_core::IUnknownImpl,
            const OFFSET: isize,
        >(
            this: *mut core::ffi::c_void,
            input: u8,
            output: *mut *mut core::ffi::c_void,
        ) -> windows_core::HRESULT
        where
            Identity: ISimple_Impl,
        {
            let this: &Identity = &*((this as *const *const ()).offset(OFFSET) as *const Identity);
            ISimple_Impl::Repeat(this, input, core::mem::transmute_copy(&output)).into()
        }
...

Any advice would be appreciated. Thank you.

kennykerr commented 1 month ago

OutRef isn't used by windows-bindgen yet but that is something that I plan to harmonize.

kennykerr commented 1 month ago

In other words, for now you can just ignore OutRef and implement the trait as defined by windows-bindgen.