1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-15 22:37:28 +02:00
comprehensive-rust/third_party/cxx/book/snippets.rs
Adrian Taylor 7f469fb2c7
Add Chromium section (#1479)
This is a contribution of a Chromium section for Comprehensive Rust.

---------

Co-authored-by: Nicole L <dlegare.1001@gmail.com>
Co-authored-by: Martin Geisler <martin@geisler.net>
2023-11-27 18:21:19 +00:00

143 lines
2.9 KiB
Rust

//! This file contains various code snippets taken from the CXX book and
//! tutorial. Some have been modified to fit the course better.
// ANCHOR: rust_bridge
#[cxx::bridge]
mod ffi {
extern "Rust" {
type MyType; // Opaque type
fn foo(&self); // Method on `MyType`
fn bar() -> Box<MyType>; // Free function
}
}
struct MyType(i32);
impl MyType {
fn foo(&self) {
println!("{}", self.0);
}
}
fn bar() -> Box<MyType> {
Box::new(MyType(123))
}
// ANCHOR_END: rust_bridge
// ANCHOR: cpp_bridge
#[cxx::bridge]
mod ffi {
extern "C++" {
include!("demo/include/blobstore.h");
type BlobstoreClient;
fn new_blobstore_client() -> UniquePtr<BlobstoreClient>;
fn put(&self, parts: &mut MultiBuf) -> u64;
}
unsafe extern "C++" {
fn f(); // safe to call
}
}
// ANCHOR_END: cpp_bridge
// ANCHOR: shared_types
#[cxx::bridge]
mod ffi {
#[derive(Clone, Debug, Hash)]
struct PlayingCard {
suit: Suit,
value: u8, // A=1, J=11, Q=12, K=13
}
enum Suit {
Clubs,
Diamonds,
Hearts,
Spades,
}
}
// ANCHOR_END: shared_types
// ANCHOR: shared_enums_bridge
#[cxx::bridge]
mod ffi {
enum Suit {
Clubs,
Diamonds,
Hearts,
Spades,
}
}
// ANCHOR_END: shared_enums_bridge
// ANCHOR: shared_enums_rust
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct Suit {
pub repr: u8,
}
#[allow(non_upper_case_globals)]
impl Suit {
pub const Clubs: Self = Suit { repr: 0 };
pub const Diamonds: Self = Suit { repr: 1 };
pub const Hearts: Self = Suit { repr: 2 };
pub const Spades: Self = Suit { repr: 3 };
}
// ANCHOR_END: shared_enums_rust
// ANCHOR: rust_result
#[cxx::bridge]
mod ffi {
extern "Rust" {
fn fallible(depth: usize) -> Result<String>;
}
}
fn fallible(depth: usize) -> anyhow::Result<String> {
if depth == 0 {
return Err(anyhow::Error::msg("fallible1 requires depth > 0"));
}
Ok("Success!".into())
}
// ANCHOR_END: rust_result
// ANCHOR: cpp_exception
#[cxx::bridge]
mod ffi {
unsafe extern "C++" {
include!("example/include/example.h");
fn fallible(depth: usize) -> Result<String>;
}
}
fn main() {
if let Err(err) = ffi::fallible(99) {
eprintln!("Error: {}", err);
process::exit(1);
}
}
// ANCHOR_END: cpp_exception
// ANCHOR: cxx_overview
#[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>;
}
}
// Definitions of Rust types and functions go here
// ANCHOR_END: cxx_overview