Austin Schuh | 6ea9bfa | 2023-08-06 19:05:10 -0700 | [diff] [blame^] | 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
| 9 | // This example shows calls from C++ back into Rust. That's really not |
| 10 | // the main purpose of autocxx, and this support is immature. If you're |
| 11 | // primarily doing this sort of thing, look into other tools such as |
| 12 | // cbindgen or cxx. |
| 13 | |
| 14 | use autocxx::prelude::*; |
| 15 | use std::pin::Pin; |
| 16 | |
| 17 | include_cpp! { |
| 18 | #include "input.h" |
| 19 | safety!(unsafe_ffi) |
| 20 | generate!("jurassic") |
| 21 | } |
| 22 | |
| 23 | fn main() { |
| 24 | ffi::jurassic(); |
| 25 | } |
| 26 | |
| 27 | #[autocxx::extern_rust::extern_rust_type] |
| 28 | pub struct Dinosaur { |
| 29 | carnivore: bool, |
| 30 | } |
| 31 | |
| 32 | #[autocxx::extern_rust::extern_rust_function] |
| 33 | pub fn new_dinosaur(carnivore: bool) -> Box<Dinosaur> { |
| 34 | Box::new(Dinosaur { carnivore }) |
| 35 | } |
| 36 | |
| 37 | impl Dinosaur { |
| 38 | #[autocxx::extern_rust::extern_rust_function] |
| 39 | fn roar(&self) { |
| 40 | println!("Roar"); |
| 41 | } |
| 42 | |
| 43 | #[autocxx::extern_rust::extern_rust_function] |
| 44 | fn eat(self: Pin<&mut Dinosaur>, other_dinosaur: Box<Dinosaur>) { |
| 45 | assert!(self.carnivore); |
| 46 | other_dinosaur.get_eaten(); |
| 47 | println!("Nom nom"); |
| 48 | } |
| 49 | |
| 50 | fn get_eaten(&self) { |
| 51 | println!("Uh-oh"); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[autocxx::extern_rust::extern_rust_function] |
| 56 | pub fn go_extinct() { |
| 57 | println!("Boom") |
| 58 | } |