Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no_std support and migrate to 2018 edition #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
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"]
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"
Expand Down
3 changes: 2 additions & 1 deletion extern/exception.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 17 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
//! 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 { }

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;
fn RustObjCExceptionTryCatch(r#try: extern fn(*mut c_void),
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.
Expand Down Expand Up @@ -59,7 +64,7 @@ unsafe fn try_no_ret<F>(closure: F) -> Result<(), *mut Exception>
///
/// Unsafe because this encourages unwinding through the closure from
/// Objective-C, which is not safe.
pub unsafe fn try<F, R>(closure: F) -> Result<R, *mut Exception>
pub unsafe fn r#try<F, R>(closure: F) -> Result<R, *mut Exception>
where F: FnOnce() -> R {
let mut value = None;
let result = {
Expand All @@ -74,14 +79,16 @@ pub unsafe fn try<F, R>(closure: F) -> Result<R, *mut Exception>

#[cfg(test)]
mod tests {
use std::ptr;
use super::{throw, try};
use alloc::string::ToString;
use core::ptr;

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());
}
Expand All @@ -90,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
});
Expand Down