From 458ed98e606e400f87916581ffa178b9a899beca Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 2 Jun 2021 22:29:01 +0200 Subject: [PATCH 1/2] Add no_std support --- Cargo.toml | 1 + extern/exception.m | 3 ++- src/lib.rs | 17 ++++++++++++----- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7bb99c7..e7c6e4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Steven Sheldon"] description = "Rust interface for Objective-C's throw and try/catch statements." keywords = ["objective-c", "osx", "ios"] +categories = ["development-tools::ffi", "no-std"] repository = "http://github.com/SSheldon/rust-objc-exception" documentation = "http://ssheldon.github.io/rust-objc/objc_exception/" license = "MIT" diff --git a/extern/exception.m b/extern/exception.m index 700439e..255a9ce 100644 --- a/extern/exception.m +++ b/extern/exception.m @@ -5,7 +5,8 @@ void RustObjCExceptionThrow(id exception) { @throw exception; } -int RustObjCExceptionTryCatch(void (*try)(void *), void *context, id *error) { +// We return `unsigned char`, since it is guaranteed to be an `u8` on all platforms +unsigned char RustObjCExceptionTryCatch(void (*try)(void *), void *context, id *error) { @try { try(context); if (error) { diff --git a/src/lib.rs b/src/lib.rs index d381f60..1942738 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,13 @@ //! Rust interface for Objective-C's `@throw` and `@try`/`@catch` statements. -use std::mem; -use std::os::raw::{c_int, c_void}; -use std::ptr; +#![no_std] + +#[cfg(test)] +extern crate alloc; + +use core::ffi::c_void; +use core::mem; +use core::ptr; #[link(name = "objc", kind = "dylib")] extern { } @@ -10,7 +15,7 @@ extern { } extern { fn RustObjCExceptionThrow(exception: *mut c_void); fn RustObjCExceptionTryCatch(try: extern fn(*mut c_void), - context: *mut c_void, error: *mut *mut c_void) -> c_int; + context: *mut c_void, error: *mut *mut c_void) -> u8; // std::os::raw::c_uchar } /// An opaque type representing any Objective-C object thrown as an exception. @@ -74,7 +79,9 @@ pub unsafe fn try(closure: F) -> Result #[cfg(test)] mod tests { - use std::ptr; + use alloc::string::ToString; + use core::ptr; + use super::{throw, try}; #[test] From fcfe0de99a7d0224ee6f1ef378ca44e903c31c16 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 2 Jun 2021 22:28:26 +0200 Subject: [PATCH 2/2] Bump edition to 2018 --- Cargo.toml | 1 + src/lib.rs | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e7c6e4b..599fdf3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "objc_exception" version = "0.1.2" authors = ["Steven Sheldon"] +edition = "2018" description = "Rust interface for Objective-C's throw and try/catch statements." keywords = ["objective-c", "osx", "ios"] diff --git a/src/lib.rs b/src/lib.rs index 1942738..32a2c13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ extern { } extern { fn RustObjCExceptionThrow(exception: *mut c_void); - fn RustObjCExceptionTryCatch(try: extern fn(*mut c_void), + fn RustObjCExceptionTryCatch(r#try: extern fn(*mut c_void), context: *mut c_void, error: *mut *mut c_void) -> u8; // std::os::raw::c_uchar } @@ -64,7 +64,7 @@ unsafe fn try_no_ret(closure: F) -> Result<(), *mut Exception> /// /// Unsafe because this encourages unwinding through the closure from /// Objective-C, which is not safe. -pub unsafe fn try(closure: F) -> Result +pub unsafe fn r#try(closure: F) -> Result where F: FnOnce() -> R { let mut value = None; let result = { @@ -82,13 +82,13 @@ mod tests { use alloc::string::ToString; use core::ptr; - use super::{throw, try}; + use super::{r#try, throw}; #[test] fn test_try() { unsafe { let s = "Hello".to_string(); - let result = try(move || { + let result = r#try(move || { if s.len() > 0 { throw(ptr::null_mut()); } @@ -97,7 +97,7 @@ mod tests { assert!(result.unwrap_err() == ptr::null_mut()); let mut s = "Hello".to_string(); - let result = try(move || { + let result = r#try(move || { s.push_str(", World!"); s });