Skip to content

Commit 265658d

Browse files
authored
Merge pull request #1069 from godot-rust/qol/remove-paste
Remove paste, simplify plugin macros
2 parents da23b37 + fb0d61c commit 265658d

File tree

12 files changed

+36
-62
lines changed

12 files changed

+36
-62
lines changed

godot-core/src/builtin/collections/packed_array.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ macro_rules! impl_packed_array {
5858
$($trait_impls:tt)*
5959
},
6060
) => {
61-
// TODO expand type names in doc comments (use e.g. `paste` crate)
6261
#[doc = concat!("Implements Godot's `", stringify!($PackedArray), "` type,")]
6362
#[doc = concat!("which is a space-efficient array of `", stringify!($Element), "`s.")]
6463
///

godot-core/src/private.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub(crate) fn iterate_plugins(mut visitor: impl FnMut(&ClassPlugin)) {
135135
#[cfg(feature = "codegen-full")] // Remove if used in other scenarios.
136136
pub(crate) fn find_inherent_impl(class_name: crate::meta::ClassName) -> Option<InherentImpl> {
137137
// We do this manually instead of using `iterate_plugins()` because we want to break as soon as we find a match.
138-
let plugins = __godot_rust_plugin___GODOT_PLUGIN_REGISTRY.lock().unwrap();
138+
let plugins = __GODOT_PLUGIN_REGISTRY.lock().unwrap();
139139

140140
plugins.iter().find_map(|elem| {
141141
if elem.class_name == class_name {

godot-ffi/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ api-4-3 = ["godot-bindings/api-4-3"]
3333
api-4-4 = ["godot-bindings/api-4-4"]
3434
# ]]
3535

36-
# TODO: get rid of paste and gensym, they are trivially implementable with proc-macros. Update cargo-deny.
37-
# Especially gensym which pulls in entire syn for 10 LoC. See https://github.com/regiontog/gensym/blob/master/src/lib.rs.
36+
# TODO: get rid of gensym, which is implementable with proc-macros. Update cargo-deny.
37+
# gensym pulls in entire syn for 10 LoC. See https://github.com/regiontog/gensym/blob/master/src/lib.rs.
3838
# Might however require godot-ffi to depend on godot-macro, which is not ideal...
3939
[dependencies]
40-
paste = "1"
4140

4241
[target.'cfg(target_os = "linux")'.dependencies]
4342
libc = "0.2.153"

godot-ffi/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ mod plugins;
6161
mod string_cache;
6262
mod toolbox;
6363

64-
// See https://github.com/dtolnay/paste/issues/69#issuecomment-962418430
65-
// and https://users.rust-lang.org/t/proc-macros-using-third-party-crate/42465/4
66-
#[doc(hidden)]
67-
pub use paste;
68-
6964
#[doc(hidden)]
7065
#[cfg(target_family = "wasm")]
7166
pub use gensym::gensym;

godot-ffi/src/plugins.rs

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,53 +17,44 @@
1717
#[macro_export]
1818
macro_rules! plugin_registry {
1919
($vis:vis $registry:ident: $Type:ty) => {
20-
$crate::paste::paste! {
21-
#[used]
22-
#[allow(non_upper_case_globals)]
23-
#[doc(hidden)]
24-
$vis static [< __godot_rust_plugin_ $registry >]:
25-
std::sync::Mutex<Vec<$Type>> = std::sync::Mutex::new(Vec::new());
26-
}
20+
#[used]
21+
#[allow(non_upper_case_globals)]
22+
#[doc(hidden)]
23+
$vis static $registry: std::sync::Mutex<Vec<$Type>>
24+
= std::sync::Mutex::new(Vec::new());
2725
};
2826
}
2927

3028
#[doc(hidden)]
3129
#[macro_export]
32-
#[allow(clippy::deprecated_cfg_attr)]
33-
#[cfg_attr(rustfmt, rustfmt::skip)]
34-
// ^ skip: paste's [< >] syntax chokes fmt
30+
// Following rustfmt::skip is no longer needed, but there are 2 workarounds good to know, thus preserved.
31+
// #[allow(clippy::deprecated_cfg_attr)]
32+
// #[cfg_attr(rustfmt, rustfmt::skip)]
3533
// cfg_attr: workaround for https://github.com/rust-lang/rust/pull/52234#issuecomment-976702997
3634
macro_rules! plugin_execute_pre_main_wasm {
3735
($gensym:ident,) => {
3836
// Rust presently requires that statics with a custom `#[link_section]` must be a simple
39-
// list of bytes on the wasm target (with no extra levels of indirection such as references).
37+
// list of bytes on the Wasm target (with no extra levels of indirection such as references).
4038
//
41-
// As such, instead we export a fn with a random name of predictable format to be used
42-
// by the embedder.
43-
$crate::paste::paste! {
44-
#[no_mangle]
45-
extern "C" fn [< rust_gdext_registrant_ $gensym >] () {
46-
__init();
47-
}
39+
// As such, instead we export a function with a random name of predictable format to be used by the embedder.
40+
#[no_mangle]
41+
extern "C" fn $gensym() {
42+
__init();
4843
}
4944
};
5045
}
5146

5247
/// Executes a block of code before main, by utilising platform specific linker instructions.
5348
#[doc(hidden)]
5449
#[macro_export]
55-
#[allow(clippy::deprecated_cfg_attr)]
56-
#[cfg_attr(rustfmt, rustfmt::skip)]
57-
// ^ skip: paste's [< >] syntax chokes fmt
58-
// cfg_attr: workaround for https://github.com/rust-lang/rust/pull/52234#issuecomment-976702997
5950
macro_rules! plugin_execute_pre_main {
6051
($body:expr) => {
6152
const _: () = {
6253
#[allow(non_upper_case_globals)]
6354
#[used]
6455
// Windows:
6556
#[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
66-
// MacOS + iOS:
57+
// macOS + iOS:
6758
#[cfg_attr(target_os = "ios", link_section = "__DATA,__mod_init_func")]
6859
#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
6960
// Linux, Android, BSD:
@@ -88,43 +79,33 @@ macro_rules! plugin_execute_pre_main {
8879
};
8980
}
9081

91-
/// register a plugin by executing code pre-main that adds the plugin to the plugin registry
82+
/// Register a plugin by executing code pre-main that adds the plugin to the registry.
9283
#[doc(hidden)]
9384
#[macro_export]
94-
#[allow(clippy::deprecated_cfg_attr)]
95-
#[cfg_attr(rustfmt, rustfmt::skip)]
96-
// ^ skip: paste's [< >] syntax chokes fmt
97-
// cfg_attr: workaround for https://github.com/rust-lang/rust/pull/52234#issuecomment-976702997
9885
macro_rules! plugin_add_inner {
99-
($registry:ident; $plugin:expr; $( $path_tt:tt )* ) => {
86+
($registry:path; $plugin:expr) => {
10087
$crate::plugin_execute_pre_main!({
101-
let mut guard = $crate::paste::paste!( $( $path_tt )* [< __godot_rust_plugin_ $registry >] )
102-
.lock()
103-
.unwrap();
88+
let mut guard = $registry.lock().unwrap();
89+
10490
guard.push($plugin);
10591
});
10692
};
10793
}
10894

109-
/// Register a plugin to a registry
95+
/// Register a plugin to a registry.
11096
#[doc(hidden)]
11197
#[macro_export]
11298
macro_rules! plugin_add {
113-
( $registry:ident; $plugin:expr ) => {
114-
$crate::plugin_add_inner!($registry; $plugin; );
115-
};
116-
117-
( $registry:ident in $path:path; $plugin:expr ) => {
118-
$crate::plugin_add_inner!($registry; $plugin; $path ::);
99+
( $registry:path; $plugin:expr ) => {
100+
$crate::plugin_add_inner!($registry; $plugin);
119101
};
120102
}
121103

122-
/// Iterate over all plugins in unspecified order
123104
#[doc(hidden)]
124105
#[macro_export]
125106
macro_rules! plugin_foreach_inner {
126107
( $registry:ident; $closure:expr; $( $path_tt:tt )* ) => {
127-
let guard = $crate::paste::paste!( $( $path_tt )* [< __godot_rust_plugin_ $registry >] )
108+
let guard = $( $path_tt )* $registry
128109
.lock()
129110
.unwrap();
130111

@@ -135,7 +116,7 @@ macro_rules! plugin_foreach_inner {
135116
};
136117
}
137118

138-
/// Register a plugin to a registry
119+
/// Iterate over all plugins in unspecified order.
139120
#[doc(hidden)]
140121
#[macro_export]
141122
macro_rules! plugin_foreach {

godot-macros/src/bench.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn attribute_bench(input_decl: venial::Item) -> ParseResult<TokenStream> {
5050
}
5151
}
5252

53-
::godot::sys::plugin_add!(__GODOT_BENCH in crate::framework; crate::framework::RustBenchmark {
53+
::godot::sys::plugin_add!(crate::framework::__GODOT_BENCH; crate::framework::RustBenchmark {
5454
name: #bench_name_str,
5555
file: std::file!(),
5656
line: std::line!(),

godot-macros/src/class/data_models/inherent_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub fn transform_inherent_impl(
172172
};
173173

174174
let class_registration = quote! {
175-
::godot::sys::plugin_add!(__GODOT_PLUGIN_REGISTRY in #prv; #prv::ClassPlugin::new::<#class_name>(
175+
::godot::sys::plugin_add!(#prv::__GODOT_PLUGIN_REGISTRY; #prv::ClassPlugin::new::<#class_name>(
176176
#prv::PluginItem::InherentImpl(#prv::InherentImpl::new::<#class_name>(#docs))
177177
));
178178
};

godot-macros/src/class/data_models/interface_trait_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ pub fn transform_trait_impl(original_impl: venial::Impl) -> ParseResult<TokenStr
354354
}
355355
}
356356

357-
::godot::sys::plugin_add!(__GODOT_PLUGIN_REGISTRY in #prv; #prv::ClassPlugin::new::<#class_name>(
357+
::godot::sys::plugin_add!(#prv::__GODOT_PLUGIN_REGISTRY; #prv::ClassPlugin::new::<#class_name>(
358358
#prv::PluginItem::ITraitImpl(#item_constructor)
359359
));
360360
};

godot-macros/src/class/derive_godot_class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub fn derive_godot_class(item: venial::Item) -> ParseResult<TokenStream> {
178178
#( #deprecations )*
179179
#( #errors )*
180180

181-
::godot::sys::plugin_add!(__GODOT_PLUGIN_REGISTRY in #prv; #prv::ClassPlugin::new::<#class_name>(
181+
::godot::sys::plugin_add!(#prv::__GODOT_PLUGIN_REGISTRY; #prv::ClassPlugin::new::<#class_name>(
182182
#prv::PluginItem::Struct(
183183
#prv::Struct::new::<#class_name>(#docs)#(.#modifiers())*
184184
)

godot-macros/src/class/godot_dyn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn attribute_godot_dyn(input_decl: venial::Item) -> ParseResult<TokenStream>
6666
}
6767
}
6868

69-
::godot::sys::plugin_add!(__GODOT_PLUGIN_REGISTRY in #prv; #prv::ClassPlugin::new::<#class_path>(
69+
::godot::sys::plugin_add!(#prv::__GODOT_PLUGIN_REGISTRY; #prv::ClassPlugin::new::<#class_path>(
7070
#prv::PluginItem::DynTraitImpl(#prv::DynTraitImpl::new::<#class_path, dyn #trait_path #assoc_type_constraints>()))
7171
);
7272

godot-macros/src/itest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn attribute_itest(input_item: venial::Item) -> ParseResult<TokenStream> {
6464
#body
6565
}
6666

67-
::godot::sys::plugin_add!(__GODOT_ITEST in crate::framework; crate::framework::RustTestCase {
67+
::godot::sys::plugin_add!(crate::framework::__GODOT_ITEST; crate::framework::RustTestCase {
6868
name: #test_name_str,
6969
skipped: #skipped,
7070
focused: #focused,

itest/rust/src/register_tests/constant_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ impl godot::obj::cap::ImplementsGodotApi for HasOtherConstants {
166166

167167
// TODO once this is done via proc-macro, see if `register-docs` is still used in register_docs_test.rs. Otherwise, remove feature from Cargo.toml.
168168
godot::sys::plugin_add!(
169-
__GODOT_PLUGIN_REGISTRY in ::godot::private;
170-
::godot::private::ClassPlugin::new::<HasOtherConstants>(
171-
::godot::private::PluginItem::InherentImpl(
172-
::godot::private::InherentImpl::new::<HasOtherConstants>(
169+
godot::private::__GODOT_PLUGIN_REGISTRY;
170+
godot::private::ClassPlugin::new::<HasOtherConstants>(
171+
godot::private::PluginItem::InherentImpl(
172+
godot::private::InherentImpl::new::<HasOtherConstants>(
173173
#[cfg(feature = "register-docs")]
174174
Default::default()
175175
)

0 commit comments

Comments
 (0)