์˜ˆ์ œ

CXX์—์„œ๋Š” ์ „์ฒด C++/Rust ๊ฒฝ๊ณ„๊ฐ€ .rs ์†Œ์Šค ์ฝ”๋“œ ๋‚ด์˜ cxx::bridge โ€™modulesโ€™์— ์„ ์–ธ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

#[cxx::bridge]
mod ffi {
    extern "Rust" {
        type MultiBuf;

        fn next_chunk(buf: &mut MultiBuf) -> &[u8];
    }

    unsafe extern "C++" {
        include!("example/include/blobstore.h");

        type BlobstoreClient;

        fn new_blobstore_client() -> UniquePtr<BlobstoreClient>;
        fn put(self: &BlobstoreClient, buf: &mut MultiBuf) -> Result<u64>;
    }
}

// Rust ์œ ํ˜• ๋ฐ ํ•จ์ˆ˜์˜ ์ •์˜๋Š” ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

์ฐธ๊ณ :

  • Although this looks like a regular Rust mod, the #[cxx::bridge] procedural macro does complex things to it. The generated code is quite a bit more sophisticated - though this does still result in a mod called ffi in your code.
  • Rust์—์„œ C++โ€™์˜ std::unique_ptr ๊ธฐ๋ณธ ์ง€์›
  • Native support for Rust slices in C++
  • C++์—์„œ Rust ํ˜ธ์ถœ ๋ฐ Rust ์œ ํ˜•(์ƒ๋‹จ)
  • Rust์—์„œ C++ ํ˜ธ์ถœ ๋ฐ C++ ์œ ํ˜•(ํ•˜๋‹จ)

์ผ๋ฐ˜์ ์ธ ์˜คํ•ด: C++ ํ—ค๋”๊ฐ€ Rust์—์„œ ํŒŒ์‹ฑ๋˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ ์˜คํ•ด์˜ ์†Œ์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ—ค๋”๋Š” Rust์—์„œ ํ•ด์„๋˜์ง€ ์•Š์œผ๋ฉฐ C++ ์ปดํŒŒ์ผ๋Ÿฌ์˜ ์ด์ ์„ ์œ„ํ•ด ์ƒ์„ฑ๋œ C++ ์ฝ”๋“œ์— ๋‹จ์ˆœํžˆ #include๋ฉ๋‹ˆ๋‹ค.