Squashed 'third_party/autocxx/' changes from 629e8fa53..c35090b75
c35090b75 Merge pull request #1131 from google/rev-0.22.3
94f20d716 Revise to 0.22.3.
b4776fdd5 Merge pull request #1070 from google/reference-wrapper
25f08f567 Better encapsulate TypeConversionPolicy. No functional changes.
b389afdeb Add reference wrapper safety policy.
cd169853b Merge pull request #1126 from google/issue-1125
92f48fa30 Merge pull request #1123 from google/segfault-detection
ca60bacca Add comment
724a4971d Add test for issue 1125.
d8a9a8ca7 Detect segfaults in reduction
e147efc7c Merge pull request #1122 from google/rev-0.22.2
dfa9b99a4 Revise to 0.22.2.
4cb1da848 Merge pull request #1120 from chbaker0/main
79afb97d9 Replace lingering tempdir usages with tempfile
f945331a3 Merge pull request #1118 from google/fix-test-fixed-num
5a8b28751 Merge pull request #1117 from google/docs-tweaks
b5486faa1 Merge pull request #1109 from bsilver8192/bindgen-skip-rustfmt
f62c17273 Fix faulty test_fixed_num test.
ad954fa72 Minor doc updates.
eaa1f8737 Leave bindgen enabled when logging is
bdff5db56 Merge pull request #1110 from bsilver8192/subclass-std
922f98be4 Merge pull request #1111 from bsilver8192/subclass-unsafe
bfbcc6b94 Merge pull request #1114 from google/rev-0.22.1
4f68a2e59 Merge pull request #1112 from bsilver8192/subclass-upcast-uniqueptr
993c5705b Revise to 0.22.1.
7bf667bbf Add a function to upcast UniquePtr
733d751a2 Fix clippy and docs
8023cee43 Fix and test subclass without `safety!(unsafe)`
4ae4d47e4 Fix and test subclasses with C++ std in scope
c50b1ee7e Tell bindgen to skip rustfmt
f9b24b90e Merge pull request #1107 from google/reject-anon-namespace-typedefs
070c9755d Merge pull request #1093 from google/always-output-rs
8eb71c5e7 Merge pull request #1095 from google/issue-1094
c86f1ce7e Reject forward declared nested types.
c118dba64 Merge branch 'main' into reject-anon-namespace-typedefs
243079997 Merge pull request #1108 from google/reject-type-param-typedefs
f803c3ba5 Reject type params - fixes #1098
f3381ba52 Reject typedefs to anon namespaces.
669d932a7 Merge pull request #1106 from google/lotsa-failing-tests
f0e8487fe Marking tests as ignored.
524c2bbfc Add tests for multiple issues.
67e16ac2a Merge branch 'main' of github.com:google/autocxx into always-output-rs
5f62daf3f Merge pull request #1104 from google/roll-cxx
43ee55ca2 Further upgrade from 1.0.67 to 1.0.68
e29e3c899 Merge pull request #1100 from bsilver8192/extern_cpp_type-namespace
d2c8edef4 Merge pull request #1101 from google/fix-1081
094dbd957 Roll cxx minimal version.
94c39f35b Merge pull request #1102 from google/fix-book-build
8764f1218 Alter mdbook-mermaid installation.
85543656f Test for issue 1081
b170df056 Fix and test extern_cpp_type with type in a C++ namespace
e4b56dd49 Fix gen tests.
5457e615d Fix #1092.
git-subtree-dir: third_party/autocxx
git-subtree-split: c35090b754619531b4eebdf4d8b583db72349943
Signed-off-by: Brian Silverman <bsilver16384@gmail.com>
Change-Id: Ia34285bc1c30f7e3c71fa9e7b677a58902648843
diff --git a/examples/reference-wrappers/Cargo.toml b/examples/reference-wrappers/Cargo.toml
new file mode 100644
index 0000000..cb85e80
--- /dev/null
+++ b/examples/reference-wrappers/Cargo.toml
@@ -0,0 +1,21 @@
+# Copyright 2022 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+# option. This file may not be copied, modified, or distributed
+# except according to those terms.
+
+[package]
+name = "autocxx-reference-wrapper-example"
+version = "0.22.1"
+authors = ["Adrian Taylor <adetaylor@chromium.org>"]
+edition = "2021"
+
+[dependencies]
+cxx = "1.0.68"
+autocxx = { path = "../..", version="0.22.3" }
+
+[build-dependencies]
+autocxx-build = { path = "../../gen/build", version="0.22.3" }
+miette = { version="4.3", features=["fancy"]}
diff --git a/examples/reference-wrappers/build.rs b/examples/reference-wrappers/build.rs
new file mode 100644
index 0000000..64c573d
--- /dev/null
+++ b/examples/reference-wrappers/build.rs
@@ -0,0 +1,18 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() -> miette::Result<()> {
+ let path = std::path::PathBuf::from("src");
+ let mut b = autocxx_build::Builder::new("src/main.rs", &[&path]).build()?;
+ b.flag_if_supported("-std=c++14")
+ .file("src/input.cc").compile("autocxx-reference-wrapper-example");
+
+ println!("cargo:rerun-if-changed=src/main.rs");
+ println!("cargo:rerun-if-changed=src/input.h");
+ Ok(())
+}
diff --git a/examples/reference-wrappers/src/input.cc b/examples/reference-wrappers/src/input.cc
new file mode 100644
index 0000000..afb7eb2
--- /dev/null
+++ b/examples/reference-wrappers/src/input.cc
@@ -0,0 +1,11 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#include "input.h"
+
+Goat the_goat;
diff --git a/examples/reference-wrappers/src/input.h b/examples/reference-wrappers/src/input.h
new file mode 100644
index 0000000..5e3c6e9
--- /dev/null
+++ b/examples/reference-wrappers/src/input.h
@@ -0,0 +1,42 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#pragma once
+
+#include <cstdint>
+#include <sstream>
+#include <stdint.h>
+#include <string>
+
+class Goat {
+public:
+ Goat() : horns(0) {}
+ void add_a_horn();
+ std::string describe() const;
+private:
+ uint32_t horns;
+};
+
+
+inline void Goat::add_a_horn() { horns++; }
+inline std::string Goat::describe() const {
+ std::ostringstream oss;
+ std::string plural = horns == 1 ? "" : "s";
+ oss << "This goat has " << horns << " horn" << plural << ".";
+ return oss.str();
+}
+
+class Field {
+public:
+ const Goat& get_goat() const {
+ return the_goat;
+ }
+
+private:
+ Goat the_goat;
+};
diff --git a/examples/reference-wrappers/src/main.rs b/examples/reference-wrappers/src/main.rs
new file mode 100644
index 0000000..6bea2ff
--- /dev/null
+++ b/examples/reference-wrappers/src/main.rs
@@ -0,0 +1,67 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// This example serves to demonstrate the experimental C++
+// reference wrappers. They exist because C++ references are not
+// the same as Rust references: C++ references may alias, whereas
+// Rust references may not.
+//
+// Standard autocxx behavior therefore introduces unsoundness when
+// C++ references are encountered and treated like Rust references.
+// (cxx has this soundness problem for Trivial types; autocxx
+// makes it worse in that the same problem applies even for
+// opaque types, because we make them sized such that we can allocate
+// them on the stack).
+//
+// Reference wrappers solve that problem because internally, they're
+// just pointers. On the other hand, they're awkward to use,
+// especially in the absence of the Rust "arbitrary self types"
+// feature.
+
+use autocxx::prelude::*;
+
+include_cpp! {
+ #include "input.h"
+ // This next line enables C++ reference wrappers
+ safety!(unsafe_references_wrapped)
+ generate!("Goat")
+ generate!("Field")
+}
+
+fn main() {
+ // Create a cxx::UniquePtr as normal for a Field object.
+ let field = ffi::Field::new().within_unique_ptr();
+ // We assume at this point that C++ has had no opportunity
+ // to retain any reference to the Field. That's not strictly
+ // true, due to RVO, but under all reasonable circumstances
+ // Rust currently has exclusive ownership of the Field we've
+ // been given.
+ // Therefore, at this point in the program, it's still
+ // OK to take Rust references to this Field.
+ let _field_rust_ref = field.as_ref();
+ // However, as soon as we want to pass a reference to the field
+ // back to C++, we have to ensure we have no Rust references
+ // in existence. So: we imprison the object in a "CppPin":
+ let field = ffi::cpp_pin_uniqueptr(field);
+ // We can no longer take Rust references to the field...
+ // let _field_rust_ref = field.as_ref();
+ // However, we can take C++ references. And use such references
+ // to call methods...
+ let another_goat = field.as_cpp_ref().get_goat();
+ // The 'get_goat' method in C++ returns a reference, so this is
+ // another CppRef, not a Rust reference.
+ assert_eq!(
+ another_goat
+ .describe() // returns a UniquePtr<CxxString>, there
+ // are no Rust or C++ references involved at this point.
+ .as_ref()
+ .unwrap()
+ .to_string_lossy(),
+ "This goat has 0 horns."
+ );
+}