Skip to content

Commit

Permalink
📝 add conext check for done (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Sep 2, 2019
1 parent 52b6356 commit 33c51a5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/yield_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::any::Any;

use crate::gen_impl::Generator;
use crate::reg_context::RegContext;
use crate::rt::{Context, ContextStack, Error};
use crate::rt::{is_generator, Context, ContextStack, Error};

/// it's a special return instruction that yield nothing
/// but only terminate the generator safely
Expand All @@ -18,8 +18,11 @@ macro_rules! done {
}

/// don't use it directly, use done!() macro instead
/// would panic if use in none generator context
#[doc(hidden)]
#[inline]
pub fn done<T>() -> T {
assert!(is_generator(), "done is only possible in a generator");
// set the done bit for this special return
ContextStack::current().top()._ref = 0xf;
unsafe { std::mem::MaybeUninit::uninit().assume_init() }
Expand Down
6 changes: 6 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,9 @@ fn test_re_init() {
assert_eq!(g.next(), Some(5));
assert_eq!(g.is_done(), true);
}

#[test]
#[should_panic]
fn done_in_normal() {
done!();
}

0 comments on commit 33c51a5

Please sign in to comment.