Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial conversion to CppRef<T> everywhere #1429

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ rust-version = "1.77"
resolver = "2"

[features]
arbitrary_self_types_pointers = []
arbitrary_self_types = []

[dependencies]
autocxx-macro = { path="macro", version="0.29.0" }
Expand Down
8 changes: 5 additions & 3 deletions demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(arbitrary_self_types)]

use autocxx::prelude::*;
include_cpp! {
#include "input.h"
Expand All @@ -16,9 +18,9 @@ include_cpp! {

fn main() {
println!("Hello, world! - C++ math should say 12={}", ffi::DoMath(4));
let mut goat = ffi::Goat::new().within_box();
goat.as_mut().add_a_horn();
goat.as_mut().add_a_horn();
let goat = ffi::Goat::new().within_cpp_pin();
goat.add_a_horn();
goat.add_a_horn();
assert_eq!(
goat.describe().as_ref().unwrap().to_string_lossy(),
"This goat has 2 horns."
Expand Down
10 changes: 6 additions & 4 deletions engine/src/conversion/analysis/fun/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1541,13 +1541,15 @@ impl<'a> FnAnalyzer<'a> {
let from_type = self_ty.as_ref().unwrap();
let from_type_path = from_type.to_type_path();
let to_type = to_type.to_type_path();
let (trait_signature, ty, method_name) = match *mutable {
let (trait_signature, ty, method_name): (_, Type, _) = match *mutable {
CastMutability::ConstToConst => (
parse_quote! {
AsRef < #to_type >
autocxx::AsCppRef < #to_type >
},
Type::Path(from_type_path),
"as_ref",
parse_quote! {
autocxx::CppRef< #from_type_path >
},
"as_cpp_ref",
),
CastMutability::MutToConst => (
parse_quote! {
Expand Down
11 changes: 7 additions & 4 deletions engine/src/conversion/codegen_rs/function_wrapper_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,12 @@ impl TypeConversionPolicy {
_ => panic!("Not a pointer"),
};
let (ty, wrapper_name) = if is_mut {
(parse_quote! { autocxx::CppMutRef<'a, #ty> }, "CppMutRef")
(
parse_quote! { autocxx::CppMutLtRef<'a, #ty> },
"CppMutLtRef",
)
} else {
(parse_quote! { autocxx::CppRef<'a, #ty> }, "CppRef")
(parse_quote! { autocxx::CppLtRef<'a, #ty> }, "CppLtRef")
};
let wrapper_name = make_ident(wrapper_name);
RustParamConversion::Param {
Expand All @@ -194,9 +197,9 @@ impl TypeConversionPolicy {
_ => panic!("Not a pointer"),
};
let ty = if is_mut {
parse_quote! { &mut autocxx::CppMutRef<'a, #ty> }
parse_quote! { autocxx::CppMutRef<#ty> }
} else {
parse_quote! { &autocxx::CppRef<'a, #ty> }
parse_quote! { autocxx::CppRef<#ty> }
};
RustParamConversion::Param {
ty,
Expand Down
12 changes: 3 additions & 9 deletions engine/src/conversion/codegen_rs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod utils;
use indexmap::map::IndexMap as HashMap;
use indexmap::set::IndexSet as HashSet;

use autocxx_parser::{ExternCppType, IncludeCppConfig, RustFun, UnsafePolicy};
use autocxx_parser::{ExternCppType, IncludeCppConfig, RustFun};

use itertools::Itertools;
use proc_macro2::{Span, TokenStream};
Expand Down Expand Up @@ -121,7 +121,6 @@ fn get_string_items() -> Vec<Item> {
/// In practice, much of the "generation" involves connecting together
/// existing lumps of code within the Api structures.
pub(crate) struct RsCodeGenerator<'a> {
unsafe_policy: &'a UnsafePolicy,
include_list: &'a [String],
bindgen_mod: ItemMod,
original_name_map: CppNameMap,
Expand All @@ -133,14 +132,12 @@ impl<'a> RsCodeGenerator<'a> {
/// Generate code for a set of APIs that was discovered during parsing.
pub(crate) fn generate_rs_code(
all_apis: ApiVec<FnPhase>,
unsafe_policy: &'a UnsafePolicy,
include_list: &'a [String],
bindgen_mod: ItemMod,
config: &'a IncludeCppConfig,
header_name: Option<String>,
) -> Vec<Item> {
let c = Self {
unsafe_policy,
include_list,
bindgen_mod,
original_name_map: CppNameMap::new_from_apis(&all_apis),
Expand Down Expand Up @@ -515,11 +512,8 @@ impl<'a> RsCodeGenerator<'a> {
name, superclass, ..
} => {
let methods = associated_methods.get(&superclass);
let generate_peer_constructor = subclasses_with_a_single_trivial_constructor.contains(&name.0.name) &&
// TODO: Create an UnsafeCppPeerConstructor trait for calling an unsafe
// constructor instead? Need to create unsafe versions of everything that uses
// it too.
matches!(self.unsafe_policy, UnsafePolicy::AllFunctionsSafe);
let generate_peer_constructor =
subclasses_with_a_single_trivial_constructor.contains(&name.0.name);
self.generate_subclass(name, &superclass, methods, generate_peer_constructor)
}
Api::ExternCppType {
Expand Down
2 changes: 1 addition & 1 deletion engine/src/conversion/conversion_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn do_test(input: ItemMod) {
bc.convert(
input,
parse_callback_results,
UnsafePolicy::AllFunctionsSafe,
UnsafePolicy::ReferencesWrappedAllFunctionsSafe,
inclusions,
&CodegenOptions::default(),
"",
Expand Down
1 change: 0 additions & 1 deletion engine/src/conversion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ impl<'a> BridgeConverter<'a> {
.map_err(ConvertError::Cpp)?;
let rs = RsCodeGenerator::generate_rs_code(
analyzed_apis,
&unsafe_policy,
self.include_list,
bindgen_mod,
self.config,
Expand Down
2 changes: 1 addition & 1 deletion examples/reference-wrappers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

// Necessary to be able to call methods on reference wrappers.
// For that reason, this example only builds on nightly Rust.
#![feature(arbitrary_self_types_pointers)]
#![feature(arbitrary_self_types)]

use autocxx::prelude::*;
use std::pin::Pin;
Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ pub fn do_run_test(
let safety_policy = format_ident!("{}", safety_policy);
let unexpanded_rust = quote! {
#module_attributes
#![feature(arbitrary_self_types)]

use autocxx::prelude::*;

Expand Down
4 changes: 2 additions & 2 deletions integration-tests/tests/cpprefs_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn run_cpprefs_test(
generate_pods: &[&str],
) {
if !arbitrary_self_types_supported() {
// "unsafe_references_wrapped" requires arbitrary_self_types_pointers, which requires nightly.
// "unsafe_references_wrapped" requires arbitrary_self_types, which requires nightly.
return;
}
do_run_test(
Expand Down Expand Up @@ -127,7 +127,7 @@ fn test_return_reference_cpprefs() {
let rs = quote! {
let b = CppPin::new(ffi::Bob { a: 3, b: 4 });
let b_ref = b.as_cpp_ref();
let bob = ffi::give_bob(&b_ref);
let bob = ffi::give_bob(b_ref);
let val = unsafe { bob.as_ref() };
assert_eq!(val.b, 4);
};
Expand Down
Loading
Loading