dtolnay/cxx

Support moving into an rvalue reference argument

Aperta

#561 aperta il 11 dic 2020

 (0 commenti) (0 reazioni) (0 assegnatari)Rust (253 fork)batch import
help wanted

Metriche repository

Star
 (4472 stelle)
Metriche merge PR
 (Merge medio 8m) (3 PR mergiate in 30 g)

Descrizione

Consider this code:

// src/lib.rs

#[cxx::bridge]
mod ffi {
    unsafe extern "C++" {
        include!("example/include/example.h");
        type Thing;
        fn f(thing: UniquePtr<Thing>);
    }
}
#pragma once
#include <memory>

struct Thing {};

void f(std::unique_ptr<Thing> &&);

This would currently fail to build, with:

example-760540e262f31cef/out/cxxbridge/sources/example/src/main.rs.cc: In function ‘void cxxbridge1$f(Thing*)’:
example-760540e262f31cef/out/cxxbridge/sources/example/src/main.rs.cc:34:46: error: invalid conversion from ‘void (*)(std::unique_ptr<Thing>&&)’ to ‘void (*)(std::unique_ptr<Thing>)’ [-fpermissive]
   34 |   void (*f$)(::std::unique_ptr<::Thing>) = ::f;
      |                                            ~~^
      |                                              |
      |                                              void (*)(std::unique_ptr<Thing>&&

because the cxx C++ code generator is expecting a signature void f(std::unique_ptr<Thing>) whereas the actual signature is void f(std::unique_ptr<Thing> &&).

It would be good to support this better and interoperate between a Rust by-value argument and a C++ rvalue reference argument.

Maybe like:

extern "C++" {
    fn f(thing: #[move] UniquePtr<Thing>);
}

or this bastardization of C++ rvalue syntax:

extern "C++" {
    fn f(&&thing: UniquePtr<Thing>);
}

Guida contributor