Skip to content

Commit

Permalink
📝 refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Apr 29, 2024
1 parent 5c123d8 commit 233fa38
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/gen_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
//!
use crate::detail::gen_init;
use crate::reg_context::RegContext;
use crate::rt::{Context, ContextStack, Error};
use crate::scope::Scope;
use crate::stack::{Func, Stack, StackBox};

use std::any::Any;
use std::fmt;
use std::marker::PhantomData;
use std::panic;
use std::thread;

use crate::reg_context::RegContext;
use crate::rt::{Context, ContextStack, Error};
use crate::scope::Scope;
use crate::stack::{Func, Stack, StackBox};

/// The default stack size for generators, in bytes.
// windows has a minimal size as 0x4a8!!!!
pub const DEFAULT_STACK_SIZE: usize = 0x1000;
Expand Down Expand Up @@ -311,7 +311,7 @@ impl<'a, A, T> GeneratorImpl<'a, A, T> {
A: 'a,
{
use std::mem::transmute;
let scope = unsafe { transmute(Scope::new(&mut self.para, &mut self.ret)) };
let scope: Scope<A, T> = unsafe { transmute(Scope::new(&mut self.para, &mut self.ret)) };
self.init_code(move || f(scope));
}

Expand Down
6 changes: 2 additions & 4 deletions src/reg_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,13 @@ mod test {
let mut cur = RegContext::empty();

fn callback() {
unsafe {
VAL = true;
}
unsafe { VAL = true };
}

let stk = Stack::new(MIN_STACK);
let ctx = RegContext::new(
init_fn,
unsafe { transmute(&cur) },
&cur as *const _ as usize,
callback as *mut usize,
&stk,
);
Expand Down
1 change: 0 additions & 1 deletion src/stack/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::io;
use std::mem;
use std::os::raw::c_void;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::usize;

use super::SysStack;

Expand Down
14 changes: 5 additions & 9 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ fn test_scoped_yield() {
#[test]
fn test_inner_ref() {
let mut g = Gn::<()>::new_scoped(|mut s| {
use std::mem;
// setup something
let mut x: u32 = 10;

Expand All @@ -201,18 +200,15 @@ fn test_inner_ref() {
// however modify this internal value is really unsafe
// but this is useful pattern for setup and teardown
// which can be put in the same place
// s.yield_(&mut x);
s.yield_(unsafe { mem::transmute(&mut x) });
unsafe {
let mut_ref: &mut u32 = std::mem::transmute(&mut x);
s.yield_unsafe(mut_ref);
};

// this was modified by the invoker
assert!(x == 5);
// teardown happened when the generator get dropped
// this is just a safe dummy ret
static mut RET: u32 = 0;
#[allow(static_mut_refs)]
unsafe {
&mut RET
}
done!()
});

// use the resource setup from generator
Expand Down

0 comments on commit 233fa38

Please sign in to comment.