language = "C++" # The "\u0065" in the header is simply the letter 'e'. This trick is to prevent # our tools from marking this file as generated file. header = """/* * Copyright (c) Facebook, Inc. and its affiliates. * * This software may be used and distributed according to the terms of the * GNU General Public License version 2. * * This file is generated with cbindgen. Please run `./tools/cbindgen.sh` to * update this file. * * @gen\u0065rated <> * */ // The generated functions are exported from this Rust library // @dep=:backingstore #pragma once #include #include #include extern "C" void rust_cfallible_free_error(char *ptr); // MSVC toolchain dislikes having template in `extern "C"` functions. So we will // have to use void pointer here. Cbindgen does not support generating code like // this since it's kinda a special case so we manually generate this struct. struct RustCFallibleBase { void *value; char *error; }; // Some Rust functions will have the return type `RustCFallibleBase`, and we // have this convenient struct to help C++ code to consume the returned // struct. This is the only way to use the returned `RustCFallibleBase` from // Rust, and the user must provide a `Deleter` to correctly free the pointer // returned from Rust. template > class RustCFallible { private: std::unique_ptr> ptr_; char* error_; public: RustCFallible(RustCFallibleBase&& base, Deleter deleter) : ptr_(reinterpret_cast(base.value), deleter), error_(base.error) {} bool isError() const { return error_ != nullptr; } char* getError() { return error_; } T* get() { return ptr_.get(); } std::unique_ptr unwrap() { return std::move(ptr_); } ~RustCFallible() { if (error_ != nullptr) { rust_cfallible_free_error(error_); } unwrap(); } }; """ [export] prefix= "Rust" exclude = ["CFallible"] include = ["Tree", "TreeEntry", "TreeEntryType"] [export.rename] "CFallible" = "CFallibleBase" [export.body] "CBytes" = """ folly::ByteRange asByteRange() const { return folly::ByteRange(ptr, len); } operator folly::ByteRange() const { return asByteRange(); } """