@@ -48,19 +48,88 @@ impl<'a, A, T> Generator<'a, A, T> {
48
48
std:: mem:: forget ( g) ;
49
49
ret
50
50
}
51
- }
52
51
53
- impl < ' a , A , T > std:: ops:: Deref for Generator < ' a , A , T > {
54
- type Target = GeneratorImpl < ' a , A , T > ;
52
+ /// prefetch the generator into cache
53
+ #[ inline]
54
+ pub fn prefetch ( & self ) {
55
+ self . gen . prefetch ( ) ;
56
+ }
57
+
58
+ /// init a heap based generator with scoped closure
59
+ pub fn scoped_init < F : FnOnce ( Scope < ' a , A , T > ) -> T + ' a > ( & mut self , f : F )
60
+ where
61
+ T : ' a ,
62
+ A : ' a ,
63
+ {
64
+ self . gen . scoped_init ( f) ;
65
+ }
55
66
56
- fn deref ( & self ) -> & GeneratorImpl < ' a , A , T > {
57
- & * self . gen
67
+ /// init a heap based generator
68
+ // it's can be used to re-init a 'done' generator before it's get dropped
69
+ pub fn init_code < F : FnOnce ( ) -> T + ' a > ( & mut self , f : F )
70
+ where
71
+ T : ' a ,
72
+ {
73
+ self . gen . init_code ( f) ;
58
74
}
59
- }
60
75
61
- impl < ' a , A , T > std:: ops:: DerefMut for Generator < ' a , A , T > {
62
- fn deref_mut ( & mut self ) -> & mut GeneratorImpl < ' a , A , T > {
63
- & mut * self . gen
76
+ /// prepare the para that passed into generator before send
77
+ #[ inline]
78
+ pub fn set_para ( & mut self , para : A ) {
79
+ self . gen . set_para ( para) ;
80
+ }
81
+
82
+ /// set the generator local data
83
+ #[ inline]
84
+ pub fn set_local_data ( & mut self , data : * mut u8 ) {
85
+ self . gen . set_local_data ( data) ;
86
+ }
87
+
88
+ /// get the generator local data
89
+ #[ inline]
90
+ pub fn get_local_data ( & self ) -> * mut u8 {
91
+ self . gen . get_local_data ( )
92
+ }
93
+
94
+ /// get the generator panic data
95
+ #[ inline]
96
+ pub fn get_panic_data ( & mut self ) -> Option < Box < dyn Any + Send > > {
97
+ self . gen . get_panic_data ( )
98
+ }
99
+
100
+ /// resume the generator without touch the para
101
+ /// you should call `set_para` before this method
102
+ #[ inline]
103
+ pub fn resume ( & mut self ) -> Option < T > {
104
+ self . gen . resume ( )
105
+ }
106
+
107
+ /// `raw_send`
108
+ #[ inline]
109
+ pub fn raw_send ( & mut self , para : Option < A > ) -> Option < T > {
110
+ self . gen . raw_send ( para)
111
+ }
112
+
113
+ /// send interface
114
+ pub fn send ( & mut self , para : A ) -> T {
115
+ self . gen . send ( para)
116
+ }
117
+
118
+ /// cancel the generator
119
+ /// this will trigger a Cancel panic, it's unsafe in that you must care about the resource
120
+ pub unsafe fn cancel ( & mut self ) {
121
+ self . gen . cancel ( )
122
+ }
123
+
124
+ /// is finished
125
+ #[ inline]
126
+ pub fn is_done ( & self ) -> bool {
127
+ self . gen . is_done ( )
128
+ }
129
+
130
+ /// get stack total size and used size in word
131
+ pub fn stack_usage ( & self ) -> ( usize , usize ) {
132
+ self . gen . stack_usage ( )
64
133
}
65
134
}
66
135
@@ -145,7 +214,7 @@ impl<A: Any> Gn<A> {
145
214
146
215
/// `GeneratorImpl`
147
216
#[ repr( C ) ]
148
- pub struct GeneratorImpl < ' a , A , T > {
217
+ struct GeneratorImpl < ' a , A , T > {
149
218
// run time context
150
219
context : Context ,
151
220
// stack
@@ -201,7 +270,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
201
270
}
202
271
203
272
/// init a heap based generator with scoped closure
204
- pub fn scoped_init < F : FnOnce ( Scope < ' a , A , T > ) -> T + ' a > ( & mut self , f : F )
273
+ fn scoped_init < F : FnOnce ( Scope < ' a , A , T > ) -> T + ' a > ( & mut self , f : F )
205
274
where
206
275
T : ' a ,
207
276
A : ' a ,
@@ -213,7 +282,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
213
282
214
283
/// init a heap based generator
215
284
// it's can be used to re-init a 'done' generator before it's get dropped
216
- pub fn init_code < F : FnOnce ( ) -> T + ' a > ( & mut self , f : F )
285
+ fn init_code < F : FnOnce ( ) -> T + ' a > ( & mut self , f : F )
217
286
where
218
287
T : ' a ,
219
288
{
@@ -295,32 +364,32 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
295
364
296
365
/// prepare the para that passed into generator before send
297
366
#[ inline]
298
- pub fn set_para ( & mut self , para : A ) {
367
+ fn set_para ( & mut self , para : A ) {
299
368
self . para = Some ( para) ;
300
369
}
301
370
302
371
/// set the generator local data
303
372
#[ inline]
304
- pub fn set_local_data ( & mut self , data : * mut u8 ) {
373
+ fn set_local_data ( & mut self , data : * mut u8 ) {
305
374
self . context . local_data = data;
306
375
}
307
376
308
377
/// get the generator local data
309
378
#[ inline]
310
- pub fn get_local_data ( & self ) -> * mut u8 {
379
+ fn get_local_data ( & self ) -> * mut u8 {
311
380
self . context . local_data
312
381
}
313
382
314
383
/// get the generator panic data
315
384
#[ inline]
316
- pub fn get_panic_data ( & mut self ) -> Option < Box < dyn Any + Send > > {
385
+ fn get_panic_data ( & mut self ) -> Option < Box < dyn Any + Send > > {
317
386
self . context . err . take ( )
318
387
}
319
388
320
389
/// resume the generator without touch the para
321
390
/// you should call `set_para` before this method
322
391
#[ inline]
323
- pub fn resume ( & mut self ) -> Option < T > {
392
+ fn resume ( & mut self ) -> Option < T > {
324
393
if self . is_done ( ) {
325
394
#[ cold]
326
395
return None ;
@@ -336,7 +405,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
336
405
337
406
/// `raw_send`
338
407
#[ inline]
339
- pub fn raw_send ( & mut self , para : Option < A > ) -> Option < T > {
408
+ fn raw_send ( & mut self , para : Option < A > ) -> Option < T > {
340
409
if self . is_done ( ) {
341
410
#[ cold]
342
411
return None ;
@@ -355,7 +424,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
355
424
}
356
425
357
426
/// send interface
358
- pub fn send ( & mut self , para : A ) -> T {
427
+ fn send ( & mut self , para : A ) -> T {
359
428
let ret = self . raw_send ( Some ( para) ) ;
360
429
ret. expect ( "send got None return" )
361
430
}
@@ -375,7 +444,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
375
444
376
445
/// cancel the generator
377
446
/// this will trigger a Cancel panic, it's unsafe in that you must care about the resource
378
- pub unsafe fn cancel ( & mut self ) {
447
+ unsafe fn cancel ( & mut self ) {
379
448
if self . is_done ( ) {
380
449
return ;
381
450
}
@@ -391,12 +460,12 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
391
460
392
461
/// is finished
393
462
#[ inline]
394
- pub fn is_done ( & self ) -> bool {
463
+ fn is_done ( & self ) -> bool {
395
464
self . is_started ( ) && ( self . context . _ref & 0x3 ) != 0
396
465
}
397
466
398
467
/// get stack total size and used size in word
399
- pub fn stack_usage ( & self ) -> ( usize , usize ) {
468
+ fn stack_usage ( & self ) -> ( usize , usize ) {
400
469
( self . stack . size ( ) , self . stack . get_used_size ( ) )
401
470
}
402
471
}
0 commit comments