cc_template: Canonicalize template instantiations by removing whitespaces.
Whitespace shouldn't matter in Rust, right? This change makes the cc_template logic immune to formatting changes in upcoming stringify macros, e.g., rust/rust-lang@4cfdbd328b7171b2328d11b950b1af0978d6b1ef.
PiperOrigin-RevId: 591248605
Change-Id: Ifca20fc6240c79ebb2bb0e0a989a6f7de6b1e50f
diff --git a/rs_bindings_from_cc/collect_instantiations.rs b/rs_bindings_from_cc/collect_instantiations.rs
index ff8a721..8e3d685 100644
--- a/rs_bindings_from_cc/collect_instantiations.rs
+++ b/rs_bindings_from_cc/collect_instantiations.rs
@@ -81,8 +81,7 @@
// TODO(lukasza, hlopko): More explicitly ensure that the same canonicalization
// (e.g. TokenStream->String transformation) is used here and in
// `cc_template/cc_template_impl.rs`.
- let instantiation_name = m.tokens.to_string();
-
+ let instantiation_name = m.tokens.to_string().replace(' ', "");
results.insert(instantiation_name);
}
}
@@ -138,7 +137,7 @@
let result =
write_file_and_collect_instantiations(quote! { cc_template!(MyTemplate<int>) })
.unwrap();
- assert_eq!(result, vec!["MyTemplate < int >".to_string()]);
+ assert_eq!(result, vec!["MyTemplate<int>".to_string()]);
}
#[test]
@@ -146,7 +145,7 @@
let result =
write_file_and_collect_instantiations(quote! { cc_template![MyTemplate<int>] })
.unwrap();
- assert_eq!(result, vec!["MyTemplate < int >".to_string()]);
+ assert_eq!(result, vec!["MyTemplate<int>".to_string()]);
}
#[test]
@@ -154,7 +153,7 @@
let result =
write_file_and_collect_instantiations(quote! { cc_template!{MyTemplate<int>} })
.unwrap();
- assert_eq!(result, vec!["MyTemplate < int >".to_string()]);
+ assert_eq!(result, vec!["MyTemplate<int>".to_string()]);
}
#[test]
@@ -168,9 +167,9 @@
assert_eq!(
result,
vec![
- "MyTemplate < int >".to_string(),
- "MyTemplate < long >".to_string(),
- "MyTemplate < short >".to_string(),
+ "MyTemplate<int>".to_string(),
+ "MyTemplate<long>".to_string(),
+ "MyTemplate<short>".to_string(),
]
);
}
@@ -187,9 +186,9 @@
assert_eq!(
result,
vec![
- "MyTemplate < 42 >".to_string(),
- "std :: unique_ptr < absl :: Time >".to_string(),
- "std :: vector < Foo >".to_string(),
+ "MyTemplate<42>".to_string(),
+ "std::unique_ptr<absl::Time>".to_string(),
+ "std::vector<Foo>".to_string(),
]
);
}
@@ -203,7 +202,7 @@
}
})
.unwrap();
- assert_eq!(result, vec!["std :: vector < Foo >".to_string(),]);
+ assert_eq!(result, vec!["std::vector<Foo>".to_string(),]);
}
fn collect_instantiations_from_json(json: &str) -> String {
@@ -221,7 +220,7 @@
);
assert_eq!(
collect_instantiations_from_json(&format!("[\"{}\"]", filename.display())),
- "[\"std :: vector < bool >\",\"std :: vector < int >\"]"
+ "[\"std::vector<bool>\",\"std::vector<int>\"]"
);
}
}
diff --git a/rs_bindings_from_cc/collect_instantiations_test.cc b/rs_bindings_from_cc/collect_instantiations_test.cc
index f1a2262..b869362 100644
--- a/rs_bindings_from_cc/collect_instantiations_test.cc
+++ b/rs_bindings_from_cc/collect_instantiations_test.cc
@@ -29,7 +29,7 @@
std::string path =
WriteFileForCurrentTest("a.rs", "cc_template!(std::vector<bool>);");
EXPECT_THAT(CollectInstantiations({std::move(path)}),
- IsOkAndHolds(ElementsAre(StrEq("std :: vector < bool >"))));
+ IsOkAndHolds(ElementsAre(StrEq("std::vector<bool>"))));
}
} // namespace
diff --git a/support/cc_template/cc_template_impl.rs b/support/cc_template/cc_template_impl.rs
index b3bf72c..80cf897 100644
--- a/support/cc_template/cc_template_impl.rs
+++ b/support/cc_template/cc_template_impl.rs
@@ -51,7 +51,7 @@
// TODO(lukasza, hlopko): More explicitly ensure that the same canonicalization
// (e.g. TokenStream->String transformation) is used here and in
// `rs_bindings_from_cc/collect_instantiations.rs`.
- let instantiation_name = input.to_string();
+ let instantiation_name = input.to_string().replace(' ', "");
match instantiations.get(&instantiation_name) {
Some(concrete_struct_name) => {
@@ -139,9 +139,9 @@
#[test]
fn test_successful_expansion() {
let expanded = get_instantiation_struct_name(
- quote!{ std::vector<bool> },
+ quote! { std::vector<bool> },
hashmap! {
- quote!{ std::vector<bool> }.to_string() => "__std_vector__bool__".to_string(),
+ quote!{ std::vector<bool> }.to_string().replace(' ', "") => "__std_vector__bool__".to_string(),
},
)
.unwrap();
diff --git a/support/cc_template/test/__cc_template_instantiations.json b/support/cc_template/test/__cc_template_instantiations.json
index 0e8435c..34ea80b 100644
--- a/support/cc_template/test/__cc_template_instantiations.json
+++ b/support/cc_template/test/__cc_template_instantiations.json
@@ -1,3 +1,3 @@
{
- "my_namespace :: MyTemplate < MyArg >": "__CcTemplateInst_my_namespace_my_template_my_arg"
+ "my_namespace::MyTemplate<MyArg>": "__CcTemplateInst_my_namespace_my_template_my_arg"
}