dtolnay/cxx

Support moving into an rvalue reference argument

Aberta

#561 aberto em 11 de dez. de 2020

 (0 comentário) (0 reação) (0 responsável)Rust (253 forks)batch import
help wanted

Métricas do repositório

Stars
 (4.472 estrelas)
Métricas de merge de PR
 (Mesclagem média 8m) (3 fundiu PRs em 30d)

Description

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>);
}

Guia do colaborador