@@ -116,7 +116,7 @@ impl Callable {
116
116
let callable = Self :: from_sync_fn ( & callable_name, function) ;
117
117
118
118
#[ cfg( not( feature = "experimental-threads" ) ) ]
119
- let callable = Self :: from_local_fn ( & callable_name, function) ;
119
+ let callable = Self :: from_fn ( & callable_name, function) ;
120
120
121
121
callable
122
122
}
@@ -154,7 +154,9 @@ impl Callable {
154
154
///
155
155
/// This constructor only allows the callable to be invoked from the same thread as creating it. If you need to invoke it from any thread,
156
156
/// use [`from_sync_fn`][Self::from_sync_fn] instead (requires crate feature `experimental-threads`; only enable if really needed).
157
- pub fn from_local_fn < R , F , S > ( name : S , rust_function : F ) -> Self
157
+ ///
158
+ /// You can also couple the callable to the lifetime of an object, see [`from_linked_fn()`][Self::from_linked_fn].
159
+ pub fn from_fn < R , F , S > ( name : S , rust_function : F ) -> Self
158
160
where
159
161
R : ToGodot ,
160
162
F : ' static + FnMut ( & [ & Variant ] ) -> R ,
@@ -177,7 +179,7 @@ impl Callable {
177
179
/// Such a callable will be automatically invalidated by Godot when a linked object is freed.
178
180
/// Prefer using [`Gd::linked_callable()`] instead.
179
181
///
180
- /// If you need a callable which can live indefinitely use [`Callable::from_local_fn ()`].
182
+ /// If you need a callable which can live indefinitely, use [`Callable::from_fn ()`].
181
183
pub fn from_linked_fn < R , F , T , S > ( name : S , linked_object : & Gd < T > , rust_function : F ) -> Self
182
184
where
183
185
R : ToGodot ,
@@ -195,12 +197,29 @@ impl Callable {
195
197
} )
196
198
}
197
199
200
+ /// This constructor is being phased out in favor of [`from_fn()`][Self::from_fn], but kept through v0.4 for smoother migration.
201
+ ///
202
+ /// `from_fn()` accepts any `R: ToGodot` return type directly instead of requiring `Result<Variant, ()>`.
203
+ #[ deprecated = "Migrate to `from_fn()`, which returns `R: ToGodot` directly." ]
204
+ pub fn from_local_fn < F , S > ( name : S , mut rust_function : F ) -> Self
205
+ where
206
+ F : ' static + FnMut ( & [ & Variant ] ) -> Result < Variant , ( ) > ,
207
+ S : meta:: AsArg < GString > ,
208
+ {
209
+ meta:: arg_into_owned!( name) ;
210
+
211
+ Self :: from_fn ( & name, move |args| {
212
+ // Ignore errors.
213
+ rust_function ( args) . unwrap_or_else ( |( ) | Variant :: nil ( ) )
214
+ } )
215
+ }
216
+
198
217
/// Create callable from **single-threaded** Rust function or closure that can only be called once.
199
218
///
200
219
/// `name` is used for the string representation of the closure, which helps debugging.
201
220
///
202
221
/// After the first invocation, subsequent calls will panic with a message indicating the callable has already been consumed. This is
203
- /// useful for deferred operations that should only execute once. For repeated execution, use [`from_local_fn ()][Self::from_local_fn ].
222
+ /// useful for deferred operations that should only execute once. For repeated execution, use [`from_fn ()][Self::from_fn ].
204
223
pub ( crate ) fn from_once_fn < R , F , S > ( name : S , rust_function : F ) -> Self
205
224
where
206
225
R : ToGodot ,
@@ -210,7 +229,7 @@ impl Callable {
210
229
meta:: arg_into_owned!( name) ;
211
230
212
231
let mut rust_fn_once = Some ( rust_function) ;
213
- Self :: from_local_fn ( & name, move |args| {
232
+ Self :: from_fn ( & name, move |args| {
214
233
let rust_fn_once = rust_fn_once
215
234
. take ( )
216
235
. expect ( "callable created with from_once_fn() has already been consumed" ) ;
@@ -252,9 +271,9 @@ impl Callable {
252
271
/// `name` is used for the string representation of the closure, which helps debugging.
253
272
///
254
273
/// This constructor requires `Send` + `Sync` bound and allows the callable to be invoked from any thread. If you guarantee that you invoke
255
- /// it from the same thread as creating it, use [`from_local_fn `][Self::from_local_fn ] instead.
274
+ /// it from the same thread as creating it, use [`from_fn `][Self::from_fn ] instead.
256
275
///
257
- /// Callables created through multiple `from_local_fn ` or `from_sync_fn()` calls are never equal, even if they refer to the same function.
276
+ /// Callables created through multiple `from_fn ` or `from_sync_fn()` calls are never equal, even if they refer to the same function.
258
277
/// If you want to use equality, either clone an existing `Callable` instance, or define your own `PartialEq` impl with
259
278
/// [`Callable::from_custom`].
260
279
///
@@ -680,7 +699,7 @@ mod custom_callable {
680
699
// NOTE: this panic is currently not propagated to the caller, but results in an error message and Nil return.
681
700
// See comments in itest callable_call() for details.
682
701
panic ! (
683
- "Callable '{}' created with from_local_fn () must be called from the same thread it was created in.\n \
702
+ "Callable '{}' created with from_fn () must be called from the same thread it was created in.\n \
684
703
If you need to call it from any thread, use from_sync_fn() instead (requires `experimental-threads` feature).",
685
704
w. name
686
705
) ;
0 commit comments