diff --git a/benches/context_new.rs b/benches/context_new.rs index c5e7e8f..28a2f11 100644 --- a/benches/context_new.rs +++ b/benches/context_new.rs @@ -17,13 +17,13 @@ fn context_new(b: &mut test::Bencher) { let mut ctx = Context::new(stack, move || { let ctx_ptr = ctx_slot; loop { - (*ctx_ptr).swap() + Context::swap(ctx_ptr, ctx_ptr); } }); ctx_slot = &mut ctx; - ctx.swap(); + Context::swap(ctx_slot, ctx_slot); }) } diff --git a/benches/swap.rs b/benches/swap.rs index 0b8c2ca..cd6d84d 100644 --- a/benches/swap.rs +++ b/benches/swap.rs @@ -17,14 +17,15 @@ fn swap(b: &mut test::Bencher) { let mut ctx = Context::new(stack, move || { let ctx_ptr = ctx_slot; loop { - (*ctx_ptr).swap() + Context::swap(ctx_ptr, ctx_ptr); } }); - ctx_slot = &mut ctx; + let ctx_ptr = &mut ctx; + ctx_slot = ctx_ptr; - ctx.swap(); + Context::swap(ctx_ptr, ctx_ptr); - b.iter(|| ctx.swap()); + b.iter(|| Context::swap(ctx_ptr, ctx_ptr)); } } diff --git a/examples/basic.rs b/examples/basic.rs index 92116c8..8cc3515 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -14,12 +14,12 @@ fn main() { let mut ctx = Context::new(stack, move || { println!("it's alive!"); - (*ctx_slot).swap(); + Context::swap(ctx_slot, ctx_slot); panic!("Do not come back!") }); ctx_slot = &mut ctx; - (*ctx_slot).swap(); + Context::swap(ctx_slot, ctx_slot); } } diff --git a/src/context.rs b/src/context.rs index 942852c..188f0dd 100644 --- a/src/context.rs +++ b/src/context.rs @@ -43,21 +43,17 @@ impl<'a, Stack> Context<'a, Stack> where Stack: stack::Stack { } } - /// Switch to the context, saving the current thread of execution there. - #[inline(always)] - pub unsafe fn swap(&mut self) { - Context::swap2(self, self) - } - - /// Switch to in_ctx, saving the current thread of execution to out_ctx. - #[inline(always)] - pub unsafe fn swap2<'b>(out_ctx: *mut Context<'a, Stack>, in_ctx: *const Context<'b, Stack>) { - Registers::swap2(&mut (*out_ctx).regs, &(*in_ctx).regs) - } - /// Unwrap the context, returning the stack it contained. #[inline] pub unsafe fn unwrap(self) -> Stack { self.stack } } + +impl<'i, InStack> Context<'i, InStack> where InStack: stack::Stack { + /// Switch to in_ctx, saving the current thread of execution to out_ctx. + #[inline(always)] + pub unsafe fn swap<'o, OutStack: stack::Stack>(out_ctx: *mut Context<'o, OutStack>, in_ctx: *const Context<'i, InStack>) { + Registers::swap2(&mut (*out_ctx).regs, &(*in_ctx).regs) + } +}