Skip to content

Commit

Permalink
fix: use local Error type instead of failure
Browse files Browse the repository at this point in the history
  • Loading branch information
darfink committed May 4, 2018
1 parent 9e7f222 commit ecb9d87
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ lazy_static = "1.0"
libc = "0.2"
matches = "0.1.6"
mmap-fixed = "0.1"
region = "0.1"
region = "0.3"
slice-pool = "0.3.4"
tap = "0.2.1"

Expand Down
4 changes: 2 additions & 2 deletions src/alloc/proximity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ProximityAllocator {
.iter_mut()
.filter_map(|pool| is_pool_in_range(pool).and_option_from(|| pool.allocate(size)))
.next()
.ok_or(Error::OutOfMemory.into())
.ok_or(Error::OutOfMemory)
}

/// Allocates a new pool close to `origin`.
Expand All @@ -95,7 +95,7 @@ impl ProximityAllocator {
Err(error) => Some(Err(error)),
})
.next()
.unwrap_or(Err(Error::OutOfMemory.into()))
.unwrap_or(Err(Error::OutOfMemory))
}

/// Tries to allocate fixed memory at the specified address.
Expand Down
6 changes: 3 additions & 3 deletions src/alloc/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Iterator for RegionIter {

/// Returns the closest free region for the current address.
fn next(&mut self) -> Option<Self::Item> {
let page_size = region::page::page_size();
let page_size = region::page::size();

while self.current > 0 && self.range.contains_(self.current) {
match region::query(self.current as *const _) {
Expand All @@ -54,9 +54,9 @@ impl Iterator for RegionIter {
},
Err(error) => {
// Check whether the region is free, otherwise return the error
let result = Some(match error.downcast().expect("downcasting region error") {
let result = Some(match error {
region::Error::Free => Ok(self.current as *const _),
inner @ _ => Err(Error::RegionFailure(inner).into()),
inner => Err(Error::RegionFailure(inner)),
});

// Adjust the offset for repeated calls.
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod tests {
funcs::external_loop as *const (),
funcs::ret10 as *const ())
}.unwrap_err();
assert_matches!(error.downcast(), Ok(Error::UnsupportedInstruction));
assert_matches!(error, Error::UnsupportedInstruction);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86/patcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Patcher {
let patch_size = jump_rel32_size + jump_rel08_size;
Ok(slice::from_raw_parts_mut(hot_patch as *mut u8, patch_size))
} else {
Err(Error::NoPatchArea.into())
Err(Error::NoPatchArea)
}
} else {
// The range is from the start of the function to the end of the jump
Expand Down
2 changes: 1 addition & 1 deletion src/arch/x86/trampoline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl Builder {
Ok(Box::new(instruction.as_slice().to_vec()))
} else if instruction.is_loop() {
// Loops (e.g 'loopnz', 'jecxz') to the outside are not supported
Err(Error::UnsupportedInstruction.into())
Err(Error::UnsupportedInstruction)
} else if instruction.is_unconditional_jump() {
// If the function is not in a branch, and it unconditionally jumps
// a distance larger than the prolog, it's the same as if it terminates.
Expand Down
10 changes: 8 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Error types and utilities.
use {failure, region};
use region;

/// The result of a detour operation.
pub type Result<T> = ::std::result::Result<T, failure::Error>;
pub type Result<T> = ::std::result::Result<T, Error>;

#[derive(Fail, Debug)]
pub enum Error {
Expand Down Expand Up @@ -32,3 +32,9 @@ pub enum Error {
#[fail(display = "{}", _0)]
RegionFailure(#[cause] region::Error),
}

impl From<region::Error> for Error {
fn from(error: region::Error) -> Self {
Error::RegionFailure(error)
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,6 @@ mod tests {
}

let err = unsafe { RawDetour::new(add as *const (), add as *const ()).unwrap_err() };
assert_matches!(err.downcast(), Ok(Error::SameAddress));
assert_matches!(err, Error::SameAddress);
}
}

0 comments on commit ecb9d87

Please sign in to comment.