網(wǎng)站備案 新聞審批號百度seo點擊排名優(yōu)化
std::unique_ptr 的 Rust 綁定稱為 UniquePtr。有關(guān) Rust API 的文檔,請參見鏈接。
限制:
目前僅支持 std::unique_ptr<T, std::default_delete>。未來可能會支持自定義刪除器。
UniquePtr 不支持 T 為不透明的 Rust 類型。對于在語言邊界傳遞不透明 Rust 類型的所有權(quán),應使用 Box(C++ 中的 rust::Box)。
示例:
UniquePtr 通常用于將不透明的 C++ 對象返回給 Rust。此用例在 blobstore 教程中有所體現(xiàn)。
// src/main.rs#[cxx::bridge]
mod ffi {unsafe extern "C++" {include!("example/include/blobstore.h");type BlobstoreClient;fn new_blobstore_client() -> UniquePtr<BlobstoreClient>;// ...}
}fn main() {let client = ffi::new_blobstore_client();// ...
}
// include/blobstore.h#pragma once
#include <memory>class BlobstoreClient;std::unique_ptr<BlobstoreClient> new_blobstore_client();// src/blobstore.cc#include "example/include/blobstore.h"std::unique_ptr<BlobstoreClient> new_blobstore_client() {return std::make_unique<BlobstoreClient>();
}