Skip to content

Commit 44cca2b

Browse files
authored
Merge pull request #6 from madsmtm/no-std-exception
More `no_std` support
2 parents d343048 + 63f8c5c commit 44cca2b

33 files changed

+135
-91
lines changed

objc/src/cache.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::os::raw::c_void;
2-
use std::ptr;
3-
use std::sync::atomic::{AtomicPtr, Ordering};
1+
use core::ffi::c_void;
2+
use core::ptr;
3+
use core::sync::atomic::{AtomicPtr, Ordering};
44

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

objc/src/declare.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ decl.register();
3434
```
3535
*/
3636

37+
use alloc::format;
38+
use alloc::string::ToString;
39+
use core::mem;
40+
use core::ptr;
3741
use std::ffi::CString;
38-
use std::mem;
39-
use std::ptr;
4042

4143
use crate::runtime::{self, Class, Imp, Object, Protocol, Sel, BOOL, NO};
4244
use crate::{Encode, EncodeArguments, Encoding, Message};
@@ -95,7 +97,7 @@ fn method_type_encoding(ret: &Encoding<'_>, args: &[Encoding<'_>]) -> CString {
9597
// First two arguments are always self and the selector
9698
let mut types = format!("{}{}{}", ret, <*mut Object>::ENCODING, Sel::ENCODING);
9799
for enc in args {
98-
use std::fmt::Write;
100+
use core::fmt::Write;
99101
write!(&mut types, "{}", enc).unwrap();
100102
}
101103
CString::new(types).unwrap()

objc/src/encode.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ encode_args_impl!(A, B, C, D, E, F, G, H, I, J, K, L);
5555
#[cfg(test)]
5656
mod tests {
5757
use crate::runtime::{Class, Object, Sel};
58+
use alloc::string::ToString;
5859
use objc_encode::Encode;
5960

6061
#[test]

objc/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ The bindings can be used on Linux or *BSD utilizing the
6060
[GNUstep Objective-C runtime](https://www.github.com/gnustep/libobjc2).
6161
*/
6262

63-
#![crate_name = "objc"]
64-
#![crate_type = "lib"]
63+
#![no_std]
6564
#![warn(missing_docs)]
6665
#![allow(clippy::missing_safety_doc)]
6766

67+
extern crate alloc;
68+
extern crate std;
69+
6870
pub use objc_encode::{Encode, Encoding};
6971

7072
pub use crate::encode::EncodeArguments;

objc/src/message/apple/arm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::any::{Any, TypeId};
2-
use std::mem;
1+
use core::any::{Any, TypeId};
2+
use core::mem;
33

44
use crate::runtime::Imp;
55

objc/src/message/apple/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::any::Any;
1+
use core::any::Any;
22

33
use super::{Message, MessageArguments, MessageError, Super};
44
use crate::runtime::{Class, Object, Sel};

objc/src/message/apple/x86.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::any::{Any, TypeId};
2-
use std::mem;
1+
use core::any::{Any, TypeId};
2+
use core::mem;
33

44
use crate::runtime::Imp;
55

objc/src/message/apple/x86_64.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::mem;
1+
use core::mem;
22

33
use crate::runtime::Imp;
44

objc/src/message/gnustep.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use std::any::Any;
2-
use std::mem;
1+
use core::any::Any;
2+
use core::mem;
33

44
use super::{Message, MessageArguments, MessageError, Super};
55
use crate::runtime::{Class, Imp, Object, Sel};

objc/src/message/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::any::Any;
1+
use alloc::string::{String, ToString};
2+
use core::any::Any;
3+
use core::fmt;
4+
use core::mem;
25
use std::error::Error;
3-
use std::fmt;
4-
use std::mem;
56

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

objc/src/message/verify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt;
1+
use core::fmt;
22

33
use crate::runtime::{Class, Method, Object, Sel};
44
use crate::{Encode, EncodeArguments, Encoding};

objc/src/rc/autorelease.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::runtime::{objc_autoreleasePoolPop, objc_autoreleasePoolPush};
2-
use std::os::raw::c_void;
2+
use core::ffi::c_void;
33

44
// we use a struct to ensure that objc_autoreleasePoolPop during unwinding.
55
struct AutoReleaseHelper {

objc/src/rc/strong.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::fmt;
2-
use std::mem;
3-
use std::ops::Deref;
1+
use core::fmt;
2+
use core::mem;
3+
use core::ops::Deref;
44

55
use super::WeakPtr;
66
use crate::runtime::{self, Object};

objc/src/rc/weak.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use std::cell::UnsafeCell;
2-
use std::ptr;
1+
use alloc::boxed::Box;
2+
use core::cell::UnsafeCell;
3+
use core::ptr;
34

45
use super::StrongPtr;
56
use crate::runtime::{self, Object};

objc/src/runtime.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
//! For more information on foreign functions, see Apple's documentation:
44
//! <https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ObjCRuntimeRef/index.html>
55
6+
use core::ffi::c_void;
7+
use core::fmt;
8+
use core::ptr;
9+
use core::str;
610
use malloc_buf::Malloc;
711
use std::ffi::{CStr, CString};
8-
use std::fmt;
9-
use std::os::raw::{c_char, c_int, c_uint, c_void};
10-
use std::ptr;
11-
use std::str;
12+
use std::os::raw::{c_char, c_int, c_uint};
1213

1314
use crate::Encode;
1415

objc/src/test_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ops::{Deref, DerefMut};
1+
use core::ops::{Deref, DerefMut};
22
use std::os::raw::c_char;
33
use std::sync::Once;
44

objc_encode/src/encoding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl PartialEq<Encoding<'_>> for str {
102102
#[cfg(test)]
103103
mod tests {
104104
use super::Encoding;
105-
use std::string::ToString;
105+
use alloc::string::ToString;
106106

107107
#[test]
108108
fn test_array_display() {

objc_encode/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ assert_eq!(i32::ENCODING.to_string(), "i");
4444
#![no_std]
4545

4646
#[cfg(test)]
47-
extern crate std;
47+
extern crate alloc;
4848

4949
mod encode;
5050
mod encoding;

objc_exception/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2018"
66

77
description = "Rust interface for Objective-C's throw and try/catch statements."
88
keywords = ["objective-c", "osx", "ios"]
9+
categories = ["development-tools::ffi", "no-std"]
910
repository = "http://github.com/SSheldon/rust-objc-exception"
1011
documentation = "http://ssheldon.github.io/rust-objc/objc_exception/"
1112
license = "MIT"

objc_exception/extern/exception.m

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// See https://clang.llvm.org/docs/AutomaticReferenceCounting.html#arc-runtime-objc-retain
33
id objc_retain(id value);
44

5-
int RustObjCExceptionTryCatch(void (*try)(void *), void *context, id *error) {
5+
// We return `unsigned char`, since it is guaranteed to be an `u8` on all platforms
6+
unsigned char RustObjCExceptionTryCatch(void (*try)(void *), void *context, id *error) {
67
@try {
78
try(context);
89
if (error) {

objc_exception/src/lib.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
//! Rust interface for Objective-C's `@throw` and `@try`/`@catch` statements.
22
3-
use std::mem;
4-
use std::os::raw::{c_int, c_void};
5-
use std::ptr;
3+
#![no_std]
4+
5+
#[cfg(test)]
6+
extern crate alloc;
7+
8+
use core::ffi::c_void;
9+
use core::mem;
10+
use core::ptr;
611

712
#[link(name = "objc", kind = "dylib")]
813
// TODO: "C-unwind"
@@ -20,7 +25,7 @@ extern "C" {
2025
r#try: extern "C" fn(*mut c_void),
2126
context: *mut c_void,
2227
error: *mut *mut c_void,
23-
) -> c_int;
28+
) -> u8; // std::os::raw::c_uchar
2429
}
2530

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

101106
#[cfg(test)]
102107
mod tests {
108+
use alloc::string::ToString;
109+
use core::ptr;
110+
103111
use super::{r#try, throw};
104-
use std::ptr;
105112

106113
#[test]
107114
fn test_try() {

objc_foundation/derive/src/lib.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate quote;
44
extern crate syn;
55

66
use proc_macro::TokenStream;
7-
use quote::{Tokens, ToTokens};
7+
use quote::{ToTokens, Tokens};
88

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

2728
quote!(
2829
impl #impl_generics INSObject for #name #ty_generics #where_clause {
@@ -36,32 +37,36 @@ pub fn impl_object(input: TokenStream) -> TokenStream {
3637
}
3738
}
3839
}
39-
).to_tokens(&mut gen);
40+
)
41+
.to_tokens(&mut gen);
4042

4143
quote!(
42-
impl #impl_generics ::std::cmp::PartialEq for #name #ty_generics #where_clause {
44+
impl #impl_generics ::core::cmp::PartialEq for #name #ty_generics #where_clause {
4345
fn eq(&self, other: &Self) -> bool {
4446
INSObject::is_equal(self, other)
4547
}
4648
}
47-
).to_tokens(&mut gen);
49+
)
50+
.to_tokens(&mut gen);
4851

4952
quote!(
50-
impl #impl_generics ::std::hash::Hash for #name #ty_generics #where_clause {
51-
fn hash<H>(&self, state: &mut H) where H: ::std::hash::Hasher {
53+
impl #impl_generics ::core::hash::Hash for #name #ty_generics #where_clause {
54+
fn hash<H>(&self, state: &mut H) where H: ::core::hash::Hasher {
5255
INSObject::hash_code(self).hash(state);
5356
}
5457
}
55-
).to_tokens(&mut gen);
58+
)
59+
.to_tokens(&mut gen);
5660

5761
quote!(
58-
impl #impl_generics ::std::fmt::Debug for #name #ty_generics #where_clause {
59-
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
62+
impl #impl_generics ::core::fmt::Debug for #name #ty_generics #where_clause {
63+
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6064
let s = INSObject::description(self);
61-
::std::fmt::Display::fmt(&*s, f)
65+
::core::fmt::Display::fmt(&*s, f)
6266
}
6367
}
64-
).to_tokens(&mut gen);
68+
)
69+
.to_tokens(&mut gen);
6570

6671
// Return the generated impl
6772
gen.parse().unwrap()

objc_foundation/src/array.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::cmp::Ordering;
2-
use std::marker::PhantomData;
3-
use std::ops::{Index, Range};
4-
use std::os::raw::c_void;
1+
use alloc::vec::Vec;
2+
use core::cmp::Ordering;
3+
use core::ffi::c_void;
4+
use core::marker::PhantomData;
5+
use core::ops::{Index, Range};
56

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

407408
#[cfg(test)]
408409
mod tests {
410+
use alloc::vec;
411+
use alloc::vec::Vec;
412+
409413
use super::{INSArray, INSMutableArray, NSArray, NSMutableArray};
410414
use crate::{INSObject, INSString, NSObject, NSString};
411415
use objc_id::Id;

objc_foundation/src/data.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use std::ops::Range;
2-
use std::os::raw::c_void;
3-
use std::slice;
1+
#[cfg(feature = "block")]
2+
use alloc::vec::Vec;
3+
use core::ffi::c_void;
4+
use core::ops::Range;
5+
use core::slice;
46

57
use super::{INSCopying, INSMutableCopying, INSObject, NSRange};
68
#[cfg(feature = "block")]
@@ -57,7 +59,7 @@ pub trait INSData: INSObject {
5759
let obj: *mut Self = msg_send![obj, initWithBytesNoCopy:bytes_ptr
5860
length:bytes.len()
5961
deallocator:dealloc];
60-
std::mem::forget(bytes);
62+
core::mem::forget(bytes);
6163
Id::from_retained_ptr(obj)
6264
}
6365
}
@@ -135,6 +137,8 @@ impl INSMutableCopying for NSMutableData {
135137
mod tests {
136138
use super::{INSData, INSMutableData, NSData, NSMutableData};
137139
use crate::INSObject;
140+
#[cfg(feature = "block")]
141+
use alloc::vec;
138142

139143
#[test]
140144
fn test_bytes() {

objc_foundation/src/dictionary.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::cmp::min;
2-
use std::marker::PhantomData;
3-
use std::ops::Index;
4-
use std::ptr;
1+
use alloc::vec::Vec;
2+
use core::cmp::min;
3+
use core::marker::PhantomData;
4+
use core::ops::Index;
5+
use core::ptr;
56

67
use objc::runtime::Class;
78
use objc::{class, msg_send};
@@ -164,9 +165,11 @@ where
164165

165166
#[cfg(test)]
166167
mod tests {
168+
use alloc::vec;
169+
use objc_id::Id;
170+
167171
use super::{INSDictionary, NSDictionary};
168172
use crate::{INSArray, INSObject, INSString, NSObject, NSString};
169-
use objc_id::Id;
170173

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

0 commit comments

Comments
 (0)