dtolnay/cxx

Support moving into an rvalue reference argument

Ouverte

#561 ouverte le 11 déc. 2020

 (0 commentaire) (0 réaction) (0 personne assignée)Rust (253 forks)batch import
help wanted

Métriques du dépôt

Stars
 (4 472 étoiles)
Métriques de merge PR
 (Merge moyen 8m) (3 PRs mergées en 30 j)

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

Guide contributeur