Skip to content

Commit

Permalink
Merge pull request #11 from RivenSkaye/master
Browse files Browse the repository at this point in the history
fixed docs.rs build issue
  • Loading branch information
sonodima authored Oct 1, 2024
2 parents b0d542e + b9d35af commit 04deefe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "microseh"
version = "1.0.3"
version = "1.0.4"
authors = ["sonodima"]
edition = "2021"

Expand All @@ -20,3 +20,9 @@ cc = "1.0"
[features]
default = ["std"]
std = []

[package.metadata.docs.rs]
default-target = "x86_64-pc-windows-msvc"
rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]
targets = ["i686-pc-windows-msvc", "x86_64-pc-windows-msvc"]
6 changes: 6 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#[cfg(not(docsrs))]
extern crate cc;

#[cfg(not(docsrs))]
fn main() {
if std::env::var("HOST").unwrap().contains("gnu") { return; }
cc::Build::new().file("src/stub.c").compile("sehstub");
}

#[cfg(docsrs)]
fn main() {}
24 changes: 16 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ type HandledProc = unsafe extern "system" fn(*mut c_void);
const MS_CATCHED: u32 = 0x1;
const MS_DISABLED: u32 = 0x2;

#[cfg(not(docsrs))]
extern "C" {
#[link_name = "HandlerStub"]
fn handler_stub(proc: HandledProc, closure: *mut c_void, exception: *mut Exception) -> u32;
}

#[cfg(docsrs)]
unsafe fn handler_stub(
_proc: HandledProc,
_closure: *mut c_void,
_exception: *mut Exception,
) -> u32 {
MS_DISABLED
}

unsafe extern "system" fn handled_proc<F>(closure: *mut c_void)
where
F: FnMut(),
Expand All @@ -41,7 +51,7 @@ where
///
/// * `Ok(())` - If the closure executed without throwing any exceptions.
/// * `Err(Exception)` - If an exception occurred during the execution of the closure.
///
///
/// # Examples
///
/// ```
Expand All @@ -53,18 +63,18 @@ where
/// println!("an exception occurred: {:?}", e);
/// }
/// ```
///
///
/// # Caveats
///
///
/// If an exception occours within the closure, resources that require cleanup via\
/// the `Drop` trait, may not be properly released.
///
///
/// As a rule of thumb, it's recommended not to define resources that implement\
/// the `Drop` trait inside the closure. Instead, allocate and manage these resources\
/// outside the closure, ensuring proper cleanup even if an exception occurs.
///
///
/// # Panics
///
///
/// If exception handling is disabled in the build, which occurs when the library is\
/// not built on Windows with Microsoft Visual C++.
pub fn try_seh<F>(mut closure: F) -> Result<(), Exception>
Expand All @@ -89,7 +99,6 @@ mod tests {

const INVALID_PTR: *mut i32 = core::mem::align_of::<i32>() as _;


#[test]
#[cfg(feature = "std")]
fn all_good() {
Expand All @@ -110,7 +119,6 @@ mod tests {
assert_eq!(ex.unwrap_err().code(), ExceptionCode::AccessViolation);
}


#[test]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn access_violation_asm() {
Expand Down

0 comments on commit 04deefe

Please sign in to comment.