@@ -338,7 +338,11 @@ macro_rules! inner_spawn {
338
338
( $self: expr, $invocation: tt) => {
339
339
{
340
340
let Self { name, attr, f } = $self;
341
- let name = name. unwrap_or_else( || "<rust>" . into( ) ) ;
341
+ let name = if let Some ( name) = name {
342
+ name
343
+ } else {
344
+ impl_details:: fiber_name_from_caller( )
345
+ } ;
342
346
Ok ( Fyber :: $invocation( name, f, attr. as_ref( ) ) ?. spawn( ) )
343
347
}
344
348
} ;
@@ -355,6 +359,7 @@ where
355
359
/// to the new fiber immediately.
356
360
///
357
361
/// See the [`start`] free function for more details.
362
+ #[ track_caller]
358
363
pub fn start ( self ) -> Result < C :: JoinHandle > {
359
364
inner_spawn ! ( self , immediate)
360
365
}
@@ -370,6 +375,7 @@ where
370
375
/// In the future we are planning to add a correct implementation.
371
376
///
372
377
/// See the [`defer`] free function for more details.
378
+ #[ track_caller]
373
379
pub fn defer ( self ) -> Result < C :: JoinHandle > {
374
380
inner_spawn ! ( self , deferred)
375
381
}
@@ -498,8 +504,11 @@ where
498
504
Self { callee }
499
505
}
500
506
507
+ #[ track_caller]
501
508
pub fn spawn ( self ) -> Result < C :: JoinHandle > {
502
509
let Self { callee } = self ;
510
+ let name = impl_details:: fiber_name_from_caller ( ) ;
511
+
503
512
let fiber_ref = unsafe {
504
513
let l = ffi:: luaT_state ( ) ;
505
514
lua:: lua_getglobal ( l, c_ptr ! ( "require" ) ) ;
@@ -520,6 +529,17 @@ where
520
529
impl_details:: guarded_pcall ( l, 2 , 0 )
521
530
. map_err ( |e| panic ! ( "{}" , e) )
522
531
. unwrap ( ) ;
532
+
533
+ lua:: lua_getfield ( l, -1 , c_ptr ! ( "name" ) ) ;
534
+ lua:: lua_pushvalue ( l, -2 ) ;
535
+ lua:: lua_pushlstring ( l, name. as_ptr ( ) as _ , name. len ( ) ) ;
536
+ impl_details:: guarded_pcall ( l, 2 , 0 )
537
+ . map_err ( |e| {
538
+ // Pop the fiber module and fiber instance from the stack
539
+ lua:: lua_pop ( l, 2 ) ;
540
+ e
541
+ } ) ?;
542
+
523
543
let fiber_ref = lua:: luaL_ref ( l, lua:: LUA_REGISTRYINDEX ) ;
524
544
// pop the fiber module from the stack
525
545
lua:: lua_pop ( l, 1 ) ;
@@ -752,6 +772,12 @@ mod impl_details {
752
772
0
753
773
}
754
774
}
775
+
776
+ #[ track_caller]
777
+ pub ( super ) fn fiber_name_from_caller ( ) -> String {
778
+ let loc = std:: panic:: Location :: caller ( ) ;
779
+ format ! ( "<rust:{}:{}:{}>" , loc. file( ) , loc. line( ) , loc. column( ) )
780
+ }
755
781
}
756
782
757
783
////////////////////////////////////////////////////////////////////////////////
@@ -1170,6 +1196,7 @@ impl TrampolineArgs for VaList {
1170
1196
/// This will create a fiber using default parameters of [`Builder`], if you
1171
1197
/// want to specify the stack size or the name of the thread, use this API
1172
1198
/// instead.
1199
+ #[ track_caller]
1173
1200
pub fn start < ' f , F , T > ( f : F ) -> JoinHandle < ' f , T >
1174
1201
where
1175
1202
F : FnOnce ( ) -> T ,
@@ -1188,6 +1215,7 @@ where
1188
1215
/// should always be used instead of the latter.
1189
1216
///
1190
1217
/// For more details see: [`start`]
1218
+ #[ track_caller]
1191
1219
pub fn start_proc < ' f , F > ( f : F ) -> UnitJoinHandle < ' f >
1192
1220
where
1193
1221
F : FnOnce ( ) ,
@@ -1207,6 +1235,7 @@ where
1207
1235
///
1208
1236
/// The new fiber can be joined by calling [`LuaJoinHandle::join`] method on
1209
1237
/// it's join handle.
1238
+ #[ track_caller]
1210
1239
pub fn defer < ' f , F , T > ( f : F ) -> LuaJoinHandle < ' f , T >
1211
1240
where
1212
1241
F : FnOnce ( ) -> T ,
@@ -1226,6 +1255,7 @@ where
1226
1255
/// it's join handle.
1227
1256
///
1228
1257
/// This is an optimized version [`defer`]`<F, ()>`.
1258
+ #[ track_caller]
1229
1259
pub fn defer_proc < ' f , F > ( f : F ) -> LuaUnitJoinHandle < ' f >
1230
1260
where
1231
1261
F : FnOnce ( ) ,
0 commit comments