From b8211689fa32f174df80d7c91643d68c8ea23679 Mon Sep 17 00:00:00 2001 From: edef Date: Thu, 16 Apr 2015 04:14:18 -0400 Subject: [PATCH] return a Result from StackSource::get_stack with an associated Error fix #7 --- benches/swap.rs | 2 +- examples/basic.rs | 2 +- src/os.rs | 29 +++++++++++++---------------- src/stack.rs | 4 +++- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/benches/swap.rs b/benches/swap.rs index 257720e..b6385de 100644 --- a/benches/swap.rs +++ b/benches/swap.rs @@ -8,7 +8,7 @@ static mut ctx_slot: *mut Context<'static, lwkt::os::Stack> = 0 as *mut Context< #[bench] fn swap(b: &mut test::Bencher) { unsafe { - let stack = lwkt::os::StackSource::get_stack(4 << 20); + let stack = lwkt::os::StackSource::get_stack(4 << 20).unwrap(); let mut ctx = Context::new(stack, move || { let ctx_ptr = ctx_slot; diff --git a/examples/basic.rs b/examples/basic.rs index c116195..f61f3d2 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -7,7 +7,7 @@ static mut ctx_slot: *mut Context<'static, lwkt::os::Stack> = 0 as *mut Context< fn main() { unsafe { - let stack = lwkt::os::StackSource::get_stack(4 << 20); + let stack = lwkt::os::StackSource::get_stack(4 << 20).unwrap(); let mut ctx = Context::new(stack, move || { println!("it's alive!"); diff --git a/src/os.rs b/src/os.rs index a2c416c..88e96ec 100644 --- a/src/os.rs +++ b/src/os.rs @@ -17,7 +17,9 @@ pub struct StackSource; impl stack::StackSource for StackSource { type Output = Stack; - fn get_stack(size: usize) -> Stack { + type Error = IoError; + + fn get_stack(size: usize) -> Result { Stack::new(size) } } @@ -37,7 +39,7 @@ impl stack::Stack for Stack { } impl Stack { - fn new(size: usize) -> Stack { + fn new(size: usize) -> Result { let page_size = sys::page_size(); // round the page size up, @@ -45,25 +47,20 @@ impl Stack { let len = (size + page_size - 1) & !(page_size - 1); let stack = unsafe { - let ptr = match sys::map_stack(size) { - None => { - panic!("mmap for stack of size {} failed: {}", - len, IoError::last_os_error()) - } - Some(ptr) => ptr - }; + let ptr = try!(match sys::map_stack(size) { + None => Err(IoError::last_os_error()), + Some(ptr) => Ok(ptr) + }); Stack { ptr: ptr as *mut u8, len: len } }; - unsafe { - if !sys::protect_stack(stack.ptr) { - panic!("mprotect for guard page of stack {:p} failed: {}", - stack.ptr, IoError::last_os_error()); - } - } + try!(unsafe { + if sys::protect_stack(stack.ptr) { Ok(()) } + else { Err(IoError::last_os_error()) } + }); - stack + Ok(stack) } } diff --git a/src/stack.rs b/src/stack.rs index 59c92a8..3c721df 100644 --- a/src/stack.rs +++ b/src/stack.rs @@ -1,4 +1,5 @@ //! Traits for stacks. +use core::prelude::*; /// A trait for objects that hold ownership of a stack. pub trait Stack { @@ -15,5 +16,6 @@ pub trait Stack { /// A trait for objects that provide stacks of arbitrary size. pub trait StackSource { type Output: Stack; - fn get_stack(size: usize) -> Self::Output; + type Error = (); + fn get_stack(size: usize) -> Result; }