diff --git a/objc/src/cache.rs b/objc/src/cache.rs index 7400c1f04..372eacb77 100644 --- a/objc/src/cache.rs +++ b/objc/src/cache.rs @@ -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}; diff --git a/objc/src/declare.rs b/objc/src/declare.rs index c716c0b4b..95c1d377b 100644 --- a/objc/src/declare.rs +++ b/objc/src/declare.rs @@ -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}; @@ -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() diff --git a/objc/src/encode.rs b/objc/src/encode.rs index 9e277cf49..2eee71cbe 100644 --- a/objc/src/encode.rs +++ b/objc/src/encode.rs @@ -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] diff --git a/objc/src/lib.rs b/objc/src/lib.rs index 40d9e56a7..8515fcc1f 100644 --- a/objc/src/lib.rs +++ b/objc/src/lib.rs @@ -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; diff --git a/objc/src/message/apple/arm.rs b/objc/src/message/apple/arm.rs index 2871cac64..3d0c9c13e 100644 --- a/objc/src/message/apple/arm.rs +++ b/objc/src/message/apple/arm.rs @@ -1,5 +1,5 @@ -use std::any::{Any, TypeId}; -use std::mem; +use core::any::{Any, TypeId}; +use core::mem; use crate::runtime::Imp; diff --git a/objc/src/message/apple/mod.rs b/objc/src/message/apple/mod.rs index 6ed5140be..c587280ba 100644 --- a/objc/src/message/apple/mod.rs +++ b/objc/src/message/apple/mod.rs @@ -1,4 +1,4 @@ -use std::any::Any; +use core::any::Any; use super::{Message, MessageArguments, MessageError, Super}; use crate::runtime::{Class, Object, Sel}; diff --git a/objc/src/message/apple/x86.rs b/objc/src/message/apple/x86.rs index 89a93734a..eb11dea30 100644 --- a/objc/src/message/apple/x86.rs +++ b/objc/src/message/apple/x86.rs @@ -1,5 +1,5 @@ -use std::any::{Any, TypeId}; -use std::mem; +use core::any::{Any, TypeId}; +use core::mem; use crate::runtime::Imp; diff --git a/objc/src/message/apple/x86_64.rs b/objc/src/message/apple/x86_64.rs index 80f73bce4..9fe4a0709 100644 --- a/objc/src/message/apple/x86_64.rs +++ b/objc/src/message/apple/x86_64.rs @@ -1,4 +1,4 @@ -use std::mem; +use core::mem; use crate::runtime::Imp; diff --git a/objc/src/message/gnustep.rs b/objc/src/message/gnustep.rs index 6785de8a9..129bb91e4 100644 --- a/objc/src/message/gnustep.rs +++ b/objc/src/message/gnustep.rs @@ -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}; diff --git a/objc/src/message/mod.rs b/objc/src/message/mod.rs index dc41e3b13..d2691c43b 100644 --- a/objc/src/message/mod.rs +++ b/objc/src/message/mod.rs @@ -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}; @@ -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); diff --git a/objc/src/message/verify.rs b/objc/src/message/verify.rs index 7b401cabc..a42442a3b 100644 --- a/objc/src/message/verify.rs +++ b/objc/src/message/verify.rs @@ -1,4 +1,4 @@ -use std::fmt; +use core::fmt; use crate::runtime::{Class, Method, Object, Sel}; use crate::{Encode, EncodeArguments, Encoding}; diff --git a/objc/src/rc/autorelease.rs b/objc/src/rc/autorelease.rs index 7c7be6952..266ec5bd6 100644 --- a/objc/src/rc/autorelease.rs +++ b/objc/src/rc/autorelease.rs @@ -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 { diff --git a/objc/src/rc/strong.rs b/objc/src/rc/strong.rs index 971692d63..2c13cbc58 100644 --- a/objc/src/rc/strong.rs +++ b/objc/src/rc/strong.rs @@ -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}; diff --git a/objc/src/rc/weak.rs b/objc/src/rc/weak.rs index 975fb94bc..3ac31f477 100644 --- a/objc/src/rc/weak.rs +++ b/objc/src/rc/weak.rs @@ -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}; diff --git a/objc/src/runtime.rs b/objc/src/runtime.rs index f1a0732ff..dd419f27b 100644 --- a/objc/src/runtime.rs +++ b/objc/src/runtime.rs @@ -3,12 +3,13 @@ //! For more information on foreign functions, see Apple's documentation: //! +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; diff --git a/objc/src/test_utils.rs b/objc/src/test_utils.rs index 2e2277b15..7b2a80435 100644 --- a/objc/src/test_utils.rs +++ b/objc/src/test_utils.rs @@ -1,4 +1,4 @@ -use std::ops::{Deref, DerefMut}; +use core::ops::{Deref, DerefMut}; use std::os::raw::c_char; use std::sync::Once; diff --git a/objc_encode/src/encoding.rs b/objc_encode/src/encoding.rs index 868c5d4cc..3df447c87 100644 --- a/objc_encode/src/encoding.rs +++ b/objc_encode/src/encoding.rs @@ -102,7 +102,7 @@ impl PartialEq> for str { #[cfg(test)] mod tests { use super::Encoding; - use std::string::ToString; + use alloc::string::ToString; #[test] fn test_array_display() { diff --git a/objc_encode/src/lib.rs b/objc_encode/src/lib.rs index e7990402a..a7ed5e40b 100644 --- a/objc_encode/src/lib.rs +++ b/objc_encode/src/lib.rs @@ -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; diff --git a/objc_exception/Cargo.toml b/objc_exception/Cargo.toml index ea842f180..599fdf393 100644 --- a/objc_exception/Cargo.toml +++ b/objc_exception/Cargo.toml @@ -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" diff --git a/objc_exception/extern/exception.m b/objc_exception/extern/exception.m index be3ea58e3..6229b196a 100644 --- a/objc_exception/extern/exception.m +++ b/objc_exception/extern/exception.m @@ -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) { diff --git a/objc_exception/src/lib.rs b/objc_exception/src/lib.rs index 80098a24f..cd87092be 100644 --- a/objc_exception/src/lib.rs +++ b/objc_exception/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")] // TODO: "C-unwind" @@ -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. @@ -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() { diff --git a/objc_foundation/derive/src/lib.rs b/objc_foundation/derive/src/lib.rs index 8e381121f..ab55a0c6e 100644 --- a/objc_foundation/derive/src/lib.rs +++ b/objc_foundation/derive/src/lib.rs @@ -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 { @@ -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 { @@ -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(&self, state: &mut H) where H: ::std::hash::Hasher { + impl #impl_generics ::core::hash::Hash for #name #ty_generics #where_clause { + fn hash(&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() diff --git a/objc_foundation/src/array.rs b/objc_foundation/src/array.rs index 8ccea5297..c3461c0d5 100644 --- a/objc_foundation/src/array.rs +++ b/objc_foundation/src/array.rs @@ -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}; @@ -406,6 +407,9 @@ pub type NSMutableSharedArray = NSMutableArray; #[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; diff --git a/objc_foundation/src/data.rs b/objc_foundation/src/data.rs index 8d377082a..cb37c574c 100644 --- a/objc_foundation/src/data.rs +++ b/objc_foundation/src/data.rs @@ -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")] @@ -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) } } @@ -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() { diff --git a/objc_foundation/src/dictionary.rs b/objc_foundation/src/dictionary.rs index 52c4ad14e..720d4aee7 100644 --- a/objc_foundation/src/dictionary.rs +++ b/objc_foundation/src/dictionary.rs @@ -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}; @@ -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> { let string = NSString::from_str(key); diff --git a/objc_foundation/src/enumerator.rs b/objc_foundation/src/enumerator.rs index 641661a0f..62e9a486c 100644 --- a/objc_foundation/src/enumerator.rs +++ b/objc_foundation/src/enumerator.rs @@ -1,8 +1,8 @@ -use std::marker::PhantomData; -use std::mem; +use core::marker::PhantomData; +use core::mem; +use core::ptr; +use core::slice; use std::os::raw::c_ulong; -use std::ptr; -use std::slice; use objc::msg_send; use objc::runtime::Object; diff --git a/objc_foundation/src/lib.rs b/objc_foundation/src/lib.rs index da610293e..dfdd9c7d7 100644 --- a/objc_foundation/src/lib.rs +++ b/objc_foundation/src/lib.rs @@ -1,5 +1,9 @@ +#![no_std] #![crate_name = "objc_foundation"] +extern crate alloc; +extern crate std; + pub use self::array::{ INSArray, INSMutableArray, NSArray, NSComparisonResult, NSMutableArray, NSMutableSharedArray, NSRange, NSSharedArray, diff --git a/objc_foundation/src/macros.rs b/objc_foundation/src/macros.rs index 9b45930af..23aa962f9 100644 --- a/objc_foundation/src/macros.rs +++ b/objc_foundation/src/macros.rs @@ -21,29 +21,29 @@ macro_rules! object_struct { } } - impl ::std::cmp::PartialEq for $name { + impl ::core::cmp::PartialEq for $name { fn eq(&self, other: &Self) -> bool { use $crate::INSObject; self.is_equal(other) } } - impl ::std::cmp::Eq for $name {} + impl ::core::cmp::Eq for $name {} - impl ::std::hash::Hash for $name { + impl ::core::hash::Hash for $name { fn hash(&self, state: &mut H) where - H: ::std::hash::Hasher, + H: ::core::hash::Hasher, { use $crate::INSObject; self.hash_code().hash(state); } } - impl ::std::fmt::Debug for $name { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + impl ::core::fmt::Debug for $name { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use $crate::{INSObject, INSString}; - ::std::fmt::Debug::fmt(self.description().as_str(), f) + ::core::fmt::Debug::fmt(self.description().as_str(), f) } } }; diff --git a/objc_foundation/src/object.rs b/objc_foundation/src/object.rs index 5737df7e2..6999b3714 100644 --- a/objc_foundation/src/object.rs +++ b/objc_foundation/src/object.rs @@ -1,4 +1,4 @@ -use std::any::Any; +use core::any::Any; use objc::msg_send; use objc::runtime::{Class, BOOL, NO}; @@ -56,6 +56,7 @@ object_struct!(NSObject); mod tests { use super::{INSObject, NSObject}; use crate::{INSString, NSString}; + use alloc::format; #[test] fn test_is_equal() { diff --git a/objc_foundation/src/string.rs b/objc_foundation/src/string.rs index 38bb80b47..83244d86c 100644 --- a/objc_foundation/src/string.rs +++ b/objc_foundation/src/string.rs @@ -1,7 +1,8 @@ -use std::fmt; -use std::os::raw::{c_char, c_void}; -use std::slice; -use std::str; +use core::ffi::c_void; +use core::fmt; +use core::slice; +use core::str; +use std::os::raw::c_char; use objc::msg_send; use objc_id::{Id, ShareId}; diff --git a/objc_foundation/src/value.rs b/objc_foundation/src/value.rs index 2bd71144e..5597f00b0 100644 --- a/objc_foundation/src/value.rs +++ b/objc_foundation/src/value.rs @@ -1,9 +1,11 @@ -use std::any::Any; +use alloc::string::ToString; +use core::any::Any; +use core::ffi::c_void; +use core::marker::PhantomData; +use core::mem::MaybeUninit; +use core::str; use std::ffi::{CStr, CString}; -use std::marker::PhantomData; -use std::mem::MaybeUninit; -use std::os::raw::{c_char, c_void}; -use std::str; +use std::os::raw::c_char; use objc::runtime::Class; use objc::Encode; diff --git a/objc_id/src/id.rs b/objc_id/src/id.rs index a7bf2e730..1185d12c5 100644 --- a/objc_id/src/id.rs +++ b/objc_id/src/id.rs @@ -1,8 +1,8 @@ -use std::any::Any; -use std::fmt; -use std::hash; -use std::marker::PhantomData; -use std::ops::{Deref, DerefMut}; +use core::any::Any; +use core::fmt; +use core::hash; +use core::marker::PhantomData; +use core::ops::{Deref, DerefMut}; use objc::rc::{StrongPtr, WeakPtr}; use objc::runtime::Object; diff --git a/objc_id/src/lib.rs b/objc_id/src/lib.rs index b291bd086..99abae3bc 100644 --- a/objc_id/src/lib.rs +++ b/objc_id/src/lib.rs @@ -38,6 +38,9 @@ assert!(weak.load().is_none()); ``` */ +// This crate is, but its dependencies are not +#![no_std] + pub use id::{Id, Owned, Ownership, ShareId, Shared, WeakId}; mod id;