Skip to content

Commit

Permalink
Merge pull request #6 from madsmtm/no-std-exception
Browse files Browse the repository at this point in the history
More `no_std` support
  • Loading branch information
madsmtm authored Aug 31, 2021
2 parents d343048 + 63f8c5c commit 44cca2b
Show file tree
Hide file tree
Showing 33 changed files with 135 additions and 91 deletions.
6 changes: 3 additions & 3 deletions objc/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::os::raw::c_void;
use std::ptr;
use std::sync::atomic::{AtomicPtr, Ordering};
use core::ffi::c_void;
use core::ptr;
use core::sync::atomic::{AtomicPtr, Ordering};

use crate::runtime::{self, Class, Sel};

Expand Down
8 changes: 5 additions & 3 deletions objc/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ decl.register();
```
*/

use alloc::format;
use alloc::string::ToString;
use core::mem;
use core::ptr;
use std::ffi::CString;
use std::mem;
use std::ptr;

use crate::runtime::{self, Class, Imp, Object, Protocol, Sel, BOOL, NO};
use crate::{Encode, EncodeArguments, Encoding, Message};
Expand Down Expand Up @@ -95,7 +97,7 @@ fn method_type_encoding(ret: &Encoding<'_>, args: &[Encoding<'_>]) -> CString {
// First two arguments are always self and the selector
let mut types = format!("{}{}{}", ret, <*mut Object>::ENCODING, Sel::ENCODING);
for enc in args {
use std::fmt::Write;
use core::fmt::Write;
write!(&mut types, "{}", enc).unwrap();
}
CString::new(types).unwrap()
Expand Down
1 change: 1 addition & 0 deletions objc/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ encode_args_impl!(A, B, C, D, E, F, G, H, I, J, K, L);
#[cfg(test)]
mod tests {
use crate::runtime::{Class, Object, Sel};
use alloc::string::ToString;
use objc_encode::Encode;

#[test]
Expand Down
6 changes: 4 additions & 2 deletions objc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ The bindings can be used on Linux or *BSD utilizing the
[GNUstep Objective-C runtime](https://www.github.com/gnustep/libobjc2).
*/

#![crate_name = "objc"]
#![crate_type = "lib"]
#![no_std]
#![warn(missing_docs)]
#![allow(clippy::missing_safety_doc)]

extern crate alloc;
extern crate std;

pub use objc_encode::{Encode, Encoding};

pub use crate::encode::EncodeArguments;
Expand Down
4 changes: 2 additions & 2 deletions objc/src/message/apple/arm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::any::{Any, TypeId};
use std::mem;
use core::any::{Any, TypeId};
use core::mem;

use crate::runtime::Imp;

Expand Down
2 changes: 1 addition & 1 deletion objc/src/message/apple/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::any::Any;
use core::any::Any;

use super::{Message, MessageArguments, MessageError, Super};
use crate::runtime::{Class, Object, Sel};
Expand Down
4 changes: 2 additions & 2 deletions objc/src/message/apple/x86.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::any::{Any, TypeId};
use std::mem;
use core::any::{Any, TypeId};
use core::mem;

use crate::runtime::Imp;

Expand Down
2 changes: 1 addition & 1 deletion objc/src/message/apple/x86_64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem;
use core::mem;

use crate::runtime::Imp;

Expand Down
4 changes: 2 additions & 2 deletions objc/src/message/gnustep.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::any::Any;
use std::mem;
use core::any::Any;
use core::mem;

use super::{Message, MessageArguments, MessageError, Super};
use crate::runtime::{Class, Imp, Object, Sel};
Expand Down
9 changes: 5 additions & 4 deletions objc/src/message/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::any::Any;
use alloc::string::{String, ToString};
use core::any::Any;
use core::fmt;
use core::mem;
use std::error::Error;
use std::fmt;
use std::mem;

use crate::runtime::{Class, Imp, Object, Sel};
use crate::{Encode, EncodeArguments};
Expand Down Expand Up @@ -314,7 +315,7 @@ mod tests {
#[cfg(not(feature = "verify_message"))]
#[test]
fn test_send_message_nil() {
let nil: *mut Object = ::std::ptr::null_mut();
let nil: *mut Object = ::core::ptr::null_mut();
let result: usize = unsafe { msg_send![nil, hash] };
assert!(result == 0);

Expand Down
2 changes: 1 addition & 1 deletion objc/src/message/verify.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use core::fmt;

use crate::runtime::{Class, Method, Object, Sel};
use crate::{Encode, EncodeArguments, Encoding};
Expand Down
2 changes: 1 addition & 1 deletion objc/src/rc/autorelease.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::runtime::{objc_autoreleasePoolPop, objc_autoreleasePoolPush};
use std::os::raw::c_void;
use core::ffi::c_void;

// we use a struct to ensure that objc_autoreleasePoolPop during unwinding.
struct AutoReleaseHelper {
Expand Down
6 changes: 3 additions & 3 deletions objc/src/rc/strong.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;
use std::mem;
use std::ops::Deref;
use core::fmt;
use core::mem;
use core::ops::Deref;

use super::WeakPtr;
use crate::runtime::{self, Object};
Expand Down
5 changes: 3 additions & 2 deletions objc/src/rc/weak.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cell::UnsafeCell;
use std::ptr;
use alloc::boxed::Box;
use core::cell::UnsafeCell;
use core::ptr;

use super::StrongPtr;
use crate::runtime::{self, Object};
Expand Down
9 changes: 5 additions & 4 deletions objc/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
//! For more information on foreign functions, see Apple's documentation:
//! <https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html>

use core::ffi::c_void;
use core::fmt;
use core::ptr;
use core::str;
use malloc_buf::Malloc;
use std::ffi::{CStr, CString};
use std::fmt;
use std::os::raw::{c_char, c_int, c_uint, c_void};
use std::ptr;
use std::str;
use std::os::raw::{c_char, c_int, c_uint};

use crate::Encode;

Expand Down
2 changes: 1 addition & 1 deletion objc/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{Deref, DerefMut};
use core::ops::{Deref, DerefMut};
use std::os::raw::c_char;
use std::sync::Once;

Expand Down
2 changes: 1 addition & 1 deletion objc_encode/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl PartialEq<Encoding<'_>> for str {
#[cfg(test)]
mod tests {
use super::Encoding;
use std::string::ToString;
use alloc::string::ToString;

#[test]
fn test_array_display() {
Expand Down
2 changes: 1 addition & 1 deletion objc_encode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ assert_eq!(i32::ENCODING.to_string(), "i");
#![no_std]

#[cfg(test)]
extern crate std;
extern crate alloc;

mod encode;
mod encoding;
Expand Down
1 change: 1 addition & 0 deletions objc_exception/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 objc_exception/extern/exception.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// See https://clang.llvm.org/docs/AutomaticReferenceCounting.html#arc-runtime-objc-retain
id objc_retain(id value);

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
17 changes: 12 additions & 5 deletions objc_exception/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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")]
// TODO: "C-unwind"
Expand All @@ -20,7 +25,7 @@ extern "C" {
r#try: extern "C" fn(*mut c_void),
context: *mut c_void,
error: *mut *mut c_void,
) -> c_int;
) -> u8; // std::os::raw::c_uchar
}

/// An opaque type representing any Objective-C object thrown as an exception.
Expand Down Expand Up @@ -100,8 +105,10 @@ where

#[cfg(test)]
mod tests {
use alloc::string::ToString;
use core::ptr;

use super::{r#try, throw};
use std::ptr;

#[test]
fn test_try() {
Expand Down
29 changes: 17 additions & 12 deletions objc_foundation/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate quote;
extern crate syn;

use proc_macro::TokenStream;
use quote::{Tokens, ToTokens};
use quote::{ToTokens, Tokens};

#[proc_macro_derive(INSObject)]
pub fn impl_object(input: TokenStream) -> TokenStream {
Expand All @@ -22,7 +22,8 @@ pub fn impl_object(input: TokenStream) -> TokenStream {
let mut gen = Tokens::new();
quote!(
unsafe impl #impl_generics ::objc::Message for #name #ty_generics #where_clause { }
).to_tokens(&mut gen);
)
.to_tokens(&mut gen);

quote!(
impl #impl_generics INSObject for #name #ty_generics #where_clause {
Expand All @@ -36,32 +37,36 @@ pub fn impl_object(input: TokenStream) -> TokenStream {
}
}
}
).to_tokens(&mut gen);
)
.to_tokens(&mut gen);

quote!(
impl #impl_generics ::std::cmp::PartialEq for #name #ty_generics #where_clause {
impl #impl_generics ::core::cmp::PartialEq for #name #ty_generics #where_clause {
fn eq(&self, other: &Self) -> bool {
INSObject::is_equal(self, other)
}
}
).to_tokens(&mut gen);
)
.to_tokens(&mut gen);

quote!(
impl #impl_generics ::std::hash::Hash for #name #ty_generics #where_clause {
fn hash<H>(&self, state: &mut H) where H: ::std::hash::Hasher {
impl #impl_generics ::core::hash::Hash for #name #ty_generics #where_clause {
fn hash<H>(&self, state: &mut H) where H: ::core::hash::Hasher {
INSObject::hash_code(self).hash(state);
}
}
).to_tokens(&mut gen);
)
.to_tokens(&mut gen);

quote!(
impl #impl_generics ::std::fmt::Debug for #name #ty_generics #where_clause {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
impl #impl_generics ::core::fmt::Debug for #name #ty_generics #where_clause {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let s = INSObject::description(self);
::std::fmt::Display::fmt(&*s, f)
::core::fmt::Display::fmt(&*s, f)
}
}
).to_tokens(&mut gen);
)
.to_tokens(&mut gen);

// Return the generated impl
gen.parse().unwrap()
Expand Down
12 changes: 8 additions & 4 deletions objc_foundation/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::cmp::Ordering;
use std::marker::PhantomData;
use std::ops::{Index, Range};
use std::os::raw::c_void;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::ffi::c_void;
use core::marker::PhantomData;
use core::ops::{Index, Range};

use objc::runtime::{Class, Object};
use objc::{class, msg_send};
Expand Down Expand Up @@ -406,6 +407,9 @@ pub type NSMutableSharedArray<T> = NSMutableArray<T, Shared>;

#[cfg(test)]
mod tests {
use alloc::vec;
use alloc::vec::Vec;

use super::{INSArray, INSMutableArray, NSArray, NSMutableArray};
use crate::{INSObject, INSString, NSObject, NSString};
use objc_id::Id;
Expand Down
12 changes: 8 additions & 4 deletions objc_foundation/src/data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::ops::Range;
use std::os::raw::c_void;
use std::slice;
#[cfg(feature = "block")]
use alloc::vec::Vec;
use core::ffi::c_void;
use core::ops::Range;
use core::slice;

use super::{INSCopying, INSMutableCopying, INSObject, NSRange};
#[cfg(feature = "block")]
Expand Down Expand Up @@ -57,7 +59,7 @@ pub trait INSData: INSObject {
let obj: *mut Self = msg_send![obj, initWithBytesNoCopy:bytes_ptr
length:bytes.len()
deallocator:dealloc];
std::mem::forget(bytes);
core::mem::forget(bytes);
Id::from_retained_ptr(obj)
}
}
Expand Down Expand Up @@ -135,6 +137,8 @@ impl INSMutableCopying for NSMutableData {
mod tests {
use super::{INSData, INSMutableData, NSData, NSMutableData};
use crate::INSObject;
#[cfg(feature = "block")]
use alloc::vec;

#[test]
fn test_bytes() {
Expand Down
13 changes: 8 additions & 5 deletions objc_foundation/src/dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::cmp::min;
use std::marker::PhantomData;
use std::ops::Index;
use std::ptr;
use alloc::vec::Vec;
use core::cmp::min;
use core::marker::PhantomData;
use core::ops::Index;
use core::ptr;

use objc::runtime::Class;
use objc::{class, msg_send};
Expand Down Expand Up @@ -164,9 +165,11 @@ where

#[cfg(test)]
mod tests {
use alloc::vec;
use objc_id::Id;

use super::{INSDictionary, NSDictionary};
use crate::{INSArray, INSObject, INSString, NSObject, NSString};
use objc_id::Id;

fn sample_dict(key: &str) -> Id<NSDictionary<NSString, NSObject>> {
let string = NSString::from_str(key);
Expand Down
Loading

0 comments on commit 44cca2b

Please sign in to comment.