blob: 141b80e03b468bdb51a4a2561c5afac95691958c [file] [log] [blame]
Brian Silverman4e662aa2022-05-11 23:10:19 -07001// Copyright 2020 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
9use autocxx::prelude::*;
10
11include_cpp! {
12 // C++ headers we want to include.
13 #include "s2/r2rect.h"
14 #include "extras.h"
15 // Safety policy. We are marking that this whole C++ inclusion is unsafe
16 // which means the functions themselves do not need to be marked
17 // as unsafe. Other policies are possible.
18 safety!(unsafe)
19 // What types and functions we want to generate
20 generate!("R1Interval")
21 generate!("R2Rect")
22 generate!("describe_point")
23}
24
25// Everything that we care about is inlined, so we don't have to do
26// anything fancy to build or link any external code.
27fn main() {
28 // Create a couple of R1Intervals using their pre-existing C++
29 // constructors. Actually these will be cxx::UniquePtr<R1Interval>s.
30 let i1 = ffi::R1Interval::new(1.0f64, 2.0f64).within_unique_ptr();
31 let i2 = ffi::R1Interval::new(5.0f64, 6.0f64).within_unique_ptr();
32 // Create a rect, passing references to the intervals.
33 // Note this is 'new1' because R2Rect has multiple
34 // overloaded constructors. 'cargo expand', `cargo doc`
35 // or a rust-analyzer IDE is useful here.
36 let r = ffi::R2Rect::new1(&i1, &i2).within_unique_ptr();
37 // Call a method on one of these objects. As it happens,
38 // this returns a
39 // UniquePtr< ... opaque object representing a point ...>.
40 let center = r.GetCenter();
41 // As the object is too complex for autocxx to understand,
42 // we can't do much with it except to send it into other
43 // C++ APIs. We'll make our own which describes the point.
44 // This will return a std::string, which autocxx will
45 // convert to a UniquePtr<CxxString>. We can convert that
46 // back to a Rust string and print it, so long as we
47 // take care to decide how to deal with non-UTF8
48 // characters (hence the unwrap).
49 println!(
50 "Center of rectangle is {}",
51 ffi::describe_point(center).to_str().unwrap()
52 );
53}