Skip to content

Commit a414fbe

Browse files
committed
📝 hide GeneratorImpl completely
1 parent 5fda139 commit a414fbe

File tree

1 file changed

+91
-22
lines changed

1 file changed

+91
-22
lines changed

src/gen_impl.rs

Lines changed: 91 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,88 @@ impl<'a, A, T> Generator<'a, A, T> {
4848
std::mem::forget(g);
4949
ret
5050
}
51-
}
5251

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+
}
5566

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);
5874
}
59-
}
6075

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()
64133
}
65134
}
66135

@@ -145,7 +214,7 @@ impl<A: Any> Gn<A> {
145214

146215
/// `GeneratorImpl`
147216
#[repr(C)]
148-
pub struct GeneratorImpl<'a, A, T> {
217+
struct GeneratorImpl<'a, A, T> {
149218
// run time context
150219
context: Context,
151220
// stack
@@ -201,7 +270,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
201270
}
202271

203272
/// 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)
205274
where
206275
T: 'a,
207276
A: 'a,
@@ -213,7 +282,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
213282

214283
/// init a heap based generator
215284
// 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)
217286
where
218287
T: 'a,
219288
{
@@ -295,32 +364,32 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
295364

296365
/// prepare the para that passed into generator before send
297366
#[inline]
298-
pub fn set_para(&mut self, para: A) {
367+
fn set_para(&mut self, para: A) {
299368
self.para = Some(para);
300369
}
301370

302371
/// set the generator local data
303372
#[inline]
304-
pub fn set_local_data(&mut self, data: *mut u8) {
373+
fn set_local_data(&mut self, data: *mut u8) {
305374
self.context.local_data = data;
306375
}
307376

308377
/// get the generator local data
309378
#[inline]
310-
pub fn get_local_data(&self) -> *mut u8 {
379+
fn get_local_data(&self) -> *mut u8 {
311380
self.context.local_data
312381
}
313382

314383
/// get the generator panic data
315384
#[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>> {
317386
self.context.err.take()
318387
}
319388

320389
/// resume the generator without touch the para
321390
/// you should call `set_para` before this method
322391
#[inline]
323-
pub fn resume(&mut self) -> Option<T> {
392+
fn resume(&mut self) -> Option<T> {
324393
if self.is_done() {
325394
#[cold]
326395
return None;
@@ -336,7 +405,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
336405

337406
/// `raw_send`
338407
#[inline]
339-
pub fn raw_send(&mut self, para: Option<A>) -> Option<T> {
408+
fn raw_send(&mut self, para: Option<A>) -> Option<T> {
340409
if self.is_done() {
341410
#[cold]
342411
return None;
@@ -355,7 +424,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
355424
}
356425

357426
/// send interface
358-
pub fn send(&mut self, para: A) -> T {
427+
fn send(&mut self, para: A) -> T {
359428
let ret = self.raw_send(Some(para));
360429
ret.expect("send got None return")
361430
}
@@ -375,7 +444,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
375444

376445
/// cancel the generator
377446
/// 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) {
379448
if self.is_done() {
380449
return;
381450
}
@@ -391,12 +460,12 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
391460

392461
/// is finished
393462
#[inline]
394-
pub fn is_done(&self) -> bool {
463+
fn is_done(&self) -> bool {
395464
self.is_started() && (self.context._ref & 0x3) != 0
396465
}
397466

398467
/// get stack total size and used size in word
399-
pub fn stack_usage(&self) -> (usize, usize) {
468+
fn stack_usage(&self) -> (usize, usize) {
400469
(self.stack.size(), self.stack.get_used_size())
401470
}
402471
}

0 commit comments

Comments
 (0)