Skip to content

Commit

Permalink
📝 make cancel safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Mar 30, 2021
1 parent 997cf9c commit 949200a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
19 changes: 8 additions & 11 deletions src/gen_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,9 @@ impl<'a, A, T, const LOCAL: bool> GeneratorObj<'a, A, T, LOCAL> {
self.gen.send(para)
}

/// cancel the generator [TODO: change to safe?]
/// this will trigger a Cancel panic
/// # Safety
/// it's unsafe that cancel may have side effect when unwind stack
/// all the resources would be dropped before the normal completion
pub unsafe fn cancel(&mut self) {
/// cancel the generator
/// this will trigger a Cancel panic to unwind the stack and finish the generator
pub fn cancel(&mut self) {
self.gen.cancel()
}

Expand Down Expand Up @@ -334,7 +331,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
{
// make sure the last one is finished
if self.f.is_none() && self.context._ref == 0 {
unsafe { self.cancel() };
self.cancel();
} else {
let _ = self.f.take();
}
Expand Down Expand Up @@ -474,7 +471,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {

/// cancel the generator without any check
#[inline]
unsafe fn raw_cancel(&mut self) {
fn raw_cancel(&mut self) {
// tell the func to panic
// so that we can stop the inner func
self.context._ref = 2;
Expand All @@ -486,8 +483,8 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
}

/// cancel the generator
/// this will trigger a Cancel panic, it's unsafe in that you must care about the resource
unsafe fn cancel(&mut self) {
/// this will trigger a Cancel panic to unwind the stack
fn cancel(&mut self) {
if self.is_done() {
return;
}
Expand Down Expand Up @@ -527,7 +524,7 @@ impl<'a, A, T> Drop for GeneratorImpl<'a, A, T> {

if !self.is_done() {
trace!("generator is not done while drop");
unsafe { self.raw_cancel() }
self.raw_cancel()
}

assert!(self.is_done());
Expand Down
7 changes: 2 additions & 5 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,9 @@ fn test_cancel() {
});

loop {
// rust 1.17 can't deduce the output type!
let i: i32 = g.next().unwrap();
let i= g.next().unwrap();
if i > 10 {
unsafe {
g.cancel();
}
g.cancel();
break;
}
}
Expand Down

0 comments on commit 949200a

Please sign in to comment.