Skip to content

Commit

Permalink
Slightly improve no_std support
Browse files Browse the repository at this point in the history
See also #305
  • Loading branch information
madsmtm committed Sep 12, 2024
1 parent 883c832 commit ec058b3
Show file tree
Hide file tree
Showing 45 changed files with 76 additions and 71 deletions.
4 changes: 2 additions & 2 deletions crates/block2/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
//! [ABI]: https://clang.llvm.org/docs/Block-ABI-Apple.html
#![allow(unused)]

use core::ffi::{c_char, c_int, c_ulong, c_void};
use core::fmt;
use core::{ffi::c_void, mem::MaybeUninit};
use std::os::raw::{c_char, c_int, c_ulong};
use core::mem::MaybeUninit;

use alloc::format;

Expand Down
2 changes: 1 addition & 1 deletion crates/block2/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::ffi::CStr;
use core::fmt::{Debug, DebugStruct, Error, Formatter};
use core::ptr;
use std::ffi::CStr;

use crate::abi::{BlockDescriptorPtr, BlockFlags, BlockHeader};
use crate::ffi;
Expand Down
9 changes: 5 additions & 4 deletions crates/block2/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! # Raw bindings to `Block.h`
use core::cell::UnsafeCell;
use core::ffi::c_int;
use core::ffi::c_void;
use core::marker::{PhantomData, PhantomPinned};
use std::os::raw::c_int;

#[cfg(not(feature = "unstable-c-unwind"))]
#[doc(hidden)]
Expand Down Expand Up @@ -122,9 +122,9 @@ extern_c_unwind! {
pub mod private {
use super::*;
#[cfg(any(doc, target_vendor = "apple", feature = "gnustep-1-7"))]
use std::os::raw::c_char;
use core::ffi::c_char;
#[cfg(any(doc, target_vendor = "apple", feature = "compiler-rt"))]
use std::os::raw::c_ulong;
use core::ffi::c_ulong;

extern_c_unwind! {
pub static _NSConcreteMallocBlock: Class;
Expand Down Expand Up @@ -186,7 +186,6 @@ pub mod private {
mod tests {
use super::*;
use core::ptr;
use std::println;

#[test]
fn smoke() {
Expand All @@ -196,7 +195,9 @@ mod tests {

#[test]
#[allow(unused_unsafe)]
#[cfg(feature = "std")]
fn test_linkable() {
use std::println;
println!("{:?}", unsafe { ptr::addr_of!(_NSConcreteGlobalBlock) });
println!("{:?}", unsafe { ptr::addr_of!(_NSConcreteStackBlock) });
println!("{:?}", unsafe {
Expand Down
2 changes: 1 addition & 1 deletion crates/block2/src/global.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use core::ffi::c_ulong;
use core::fmt;
use core::marker::PhantomData;
use core::mem;
use core::mem::MaybeUninit;
use core::ops::Deref;
use core::ptr::{self, NonNull};
use std::os::raw::c_ulong;

use crate::abi::{BlockDescriptor, BlockDescriptorPtr, BlockFlags, BlockHeader};
use crate::debug::debug_block_header;
Expand Down
2 changes: 1 addition & 1 deletion crates/block2/src/stack.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use core::ffi::c_ulong;
use core::ffi::c_void;
use core::fmt;
use core::marker::PhantomData;
use core::mem::{self, MaybeUninit};
use core::ops::Deref;
use core::panic::{RefUnwindSafe, UnwindSafe};
use core::ptr::{self, NonNull};
use std::os::raw::c_ulong;

use objc2::encode::{EncodeArguments, EncodeReturn, Encoding, RefEncode};

Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/benches/autorelease.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::ffi::c_void;
use std::mem::ManuallyDrop;
use core::mem::ManuallyDrop;

use objc2::rc::{autoreleasepool, Retained};
use objc2::runtime::{AnyClass, NSObject, Sel};
Expand Down
9 changes: 4 additions & 5 deletions crates/objc2/src/__framework_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// Note: While this is not public, it is still a breaking change to remove
// entries in here, since framework crates rely on it.

pub use core::ffi::c_void;
pub use core::marker::PhantomData;
pub use core::ptr::NonNull;
pub use std::os::raw::{
pub use core::ffi::{
c_char, c_double, c_float, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint,
c_ulong, c_ulonglong, c_ushort,
c_ulong, c_ulonglong, c_ushort, c_void,
};
pub use core::marker::PhantomData;
pub use core::ptr::NonNull;

pub use crate::encode::{Encode, Encoding, RefEncode};
pub use crate::ffi::{NSInteger, NSIntegerMax, NSUInteger, NSUIntegerMax};
Expand Down
3 changes: 1 addition & 2 deletions crates/objc2/src/__macro_helpers/cache.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use core::ffi::{c_char, c_void, CStr};
use core::ptr;
use core::str;
use core::sync::atomic::{AtomicPtr, Ordering};
use std::ffi::CStr;
use std::os::raw::{c_char, c_void};

use crate::ffi;
use crate::runtime::{AnyClass, Sel};
Expand Down
14 changes: 9 additions & 5 deletions crates/objc2/src/__macro_helpers/declared_ivars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,6 @@ pub(crate) unsafe fn get_initialized_ivar_ptr<T: DeclaredClass>(
mod tests {
use alloc::vec::Vec;
use core::cell::Cell;
use std::println;
use std::sync::Mutex;

use super::*;
use crate::rc::{Allocated, PartialInit, RcTestObject, Retained, ThreadTestData};
Expand Down Expand Up @@ -459,7 +457,10 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_dealloc_and_dealloc_subclasses() {
use std::sync::Mutex;

#[derive(Debug, PartialEq)]
enum Operation {
DropIvar,
Expand Down Expand Up @@ -674,7 +675,8 @@ mod tests {
assert!(IvarZst::class().instance_variable(ivar_name).is_none());

let obj = unsafe { init(IvarZst::alloc()) };
println!("{:?}", obj.ivars().get());
#[cfg(feature = "std")]
std::println!("{:?}", obj.ivars().get());
obj.ivars().set(Ivar);
}

Expand Down Expand Up @@ -838,7 +840,8 @@ mod tests {

// Accessing superclass ivars is valid
// SAFETY: Cell not accessed while ivar is borrowed
println!("{:?}", unsafe { &*(**obj).ivars().as_ptr() });
#[cfg(feature = "std")]
std::println!("{:?}", unsafe { &*(**obj).ivars().as_ptr() });

drop(obj);
expected.release += 1;
Expand All @@ -865,7 +868,8 @@ mod tests {
);

let obj = unsafe { init_only_superclasses(InvalidAccess::alloc()) };
println!("{:?}", obj.ivars());
#[cfg(feature = "std")]
std::println!("{:?}", obj.ivars());
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/ffi/class.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(non_camel_case_types)]
use std::os::raw::{c_char, c_int, c_uint};
use core::ffi::{c_char, c_int, c_uint};

use crate::runtime::{AnyClass, AnyProtocol, Bool, Imp, Method, Sel};
#[cfg(any(doc, not(feature = "unstable-objfw")))]
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/ffi/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(non_camel_case_types)]

#[cfg(any(doc, target_vendor = "apple"))]
use std::os::raw::c_int;
use core::ffi::c_int;

use crate::runtime::{AnyClass, AnyObject};

Expand Down
4 changes: 2 additions & 2 deletions crates/objc2/src/ffi/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::runtime::AnyObject;
type objc_exception_matcher = unsafe extern "C" fn(
catch_type: *mut crate::runtime::AnyClass,
exception: *mut AnyObject,
) -> std::os::raw::c_int;
) -> core::ffi::c_int;

/// Remember that this is non-null!
#[cfg(any(
Expand Down Expand Up @@ -107,7 +107,7 @@ extern_c! {
// present when building libobjc2.
//
// #[cfg(any(doc, feature = "gnustep-1-7"))]
// pub fn objc_set_apple_compatible_objcxx_exceptions(newValue: std::os::raw::c_int) -> std::os::raw::c_int;
// pub fn objc_set_apple_compatible_objcxx_exceptions(newValue: core::ffi::c_int) -> core::ffi::c_int;

#[cold]
#[cfg(any(doc, all(target_vendor = "apple", not(all(target_os = "macos", target_arch = "x86")))))]
Expand Down
4 changes: 2 additions & 2 deletions crates/objc2/src/ffi/method.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::os::raw::c_char;
use core::ffi::c_char;
#[cfg(any(doc, not(feature = "unstable-objfw")))]
use std::os::raw::c_uint;
use core::ffi::c_uint;

use crate::runtime::{Imp, Method, Sel};

Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ pub(crate) type OpaqueData = UnsafeCell<PhantomData<(*const UnsafeCell<()>, Phan
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CStr;
use core::ffi::CStr;

#[test]
fn smoke() {
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/ffi/object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::ffi::c_char;
#[cfg(any(doc, not(feature = "unstable-objfw")))]
use core::ffi::c_void;
use std::os::raw::c_char;

#[cfg(any(doc, not(feature = "unstable-objfw")))]
use crate::runtime::Ivar;
Expand Down
4 changes: 2 additions & 2 deletions crates/objc2/src/ffi/property.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::os::raw::c_char;
use core::ffi::c_char;
#[cfg(any(doc, not(feature = "unstable-objfw")))]
use std::os::raw::c_uint;
use core::ffi::c_uint;

use crate::ffi::OpaqueData;

Expand Down
4 changes: 2 additions & 2 deletions crates/objc2/src/ffi/protocol.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::os::raw::c_char;
use core::ffi::c_char;
#[cfg(any(doc, not(feature = "unstable-objfw")))]
use std::os::raw::c_uint;
use core::ffi::c_uint;

#[cfg(any(doc, not(feature = "unstable-objfw")))]
use crate::ffi::{objc_method_description, objc_property, objc_property_attribute_t};
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/ffi/selector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::os::raw::c_char;
use core::ffi::c_char;

use crate::runtime::{Bool, Sel};

Expand Down
8 changes: 4 additions & 4 deletions crates/objc2/src/ffi/various.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::ffi::c_void;
#[cfg(any(doc, not(feature = "unstable-objfw")))]
use std::os::raw::c_char;
use std::os::raw::c_int;
use core::ffi::c_char;
use core::ffi::c_int;
#[cfg(any(doc, target_vendor = "apple"))]
use std::os::raw::c_uint;
use core::ffi::c_uint;
use core::ffi::c_void;

use crate::runtime::AnyObject;
use crate::runtime::Imp;
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/macros/declare_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
/// instance variables and methods, and implements the `NSCopying` protocol.
///
/// ```
/// use std::os::raw::c_int;
/// use std::ffi::c_int;
///
/// # use objc2::runtime::{NSObject, NSObjectProtocol, NSZone};
/// # #[cfg(available_in_foundation)]
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ macro_rules! __class_inner {
/// The recommended way of using this macro is by defining a wrapper function:
///
/// ```
/// # use std::os::raw::{c_int, c_char};
/// # use std::ffi::{c_int, c_char};
/// # use objc2::msg_send;
/// # use objc2::runtime::NSObject;
/// unsafe fn do_something(obj: &NSObject, arg: c_int) -> *const c_char {
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/main_thread_marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn is_main_thread() -> bool {
// crate.
#[cfg_attr(not(feature = "std"), link(name = "c", kind = "dylib"))]
extern "C" {
fn pthread_main_np() -> std::os::raw::c_int;
fn pthread_main_np() -> core::ffi::c_int;
}

// SAFETY: Can be called from any thread.
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/runtime/__nsstring.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::ffi::c_char;
use core::slice;
use core::str;
use std::os::raw::c_char;

use crate::ffi::NSUInteger;
use crate::msg_send;
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/runtime/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod inner {
mod inner {
// windows && !32bit-MinGW
#[cfg(all(windows, not(all(target_pointer_width = "64", target_env = "gnu"))))]
pub(crate) type BOOL = std::os::raw::c_int;
pub(crate) type BOOL = core::ffi::c_int;

// The inverse
#[cfg(not(all(windows, not(all(target_pointer_width = "64", target_env = "gnu")))))]
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/runtime/declare.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! # Dynamically creating classes and protocols.
use alloc::ffi::CString;
use alloc::format;
use alloc::string::ToString;
use core::mem;
use core::mem::ManuallyDrop;
use core::ptr;
use core::ptr::NonNull;
use std::ffi::CString;

use crate::encode::{Encode, EncodeArguments, EncodeReturn, Encoding};
use crate::ffi;
Expand Down
4 changes: 2 additions & 2 deletions crates/objc2/src/runtime/malloc.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! A minimal alternative to crates like `malloc_buf`, `mbox` and `malloced`.
use core::ffi::c_char;
use core::ffi::CStr;
use core::fmt;
use core::marker::PhantomData;
use core::ops::Deref;
use core::ptr::{self, NonNull};
use core::str;
use core::str::Utf8Error;
use std::ffi::CStr;
use std::os::raw::c_char;

use crate::ffi;

Expand Down
7 changes: 4 additions & 3 deletions crates/objc2/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
//! ```
#![allow(clippy::missing_panics_doc)]

use alloc::ffi::CString;
use alloc::vec::Vec;
use core::ffi::c_char;
use core::ffi::c_uint;
use core::ffi::{c_void, CStr};
use core::fmt;
use core::hash;
use core::panic::{RefUnwindSafe, UnwindSafe};
use core::ptr::{self, NonNull};
use core::str;
use std::ffi::{c_void, CStr, CString};
use std::os::raw::c_char;
use std::os::raw::c_uint;

// Note: While this is not public, it is still a breaking change to remove,
// since `objc2-foundation` relies on it.
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::ffi::c_char;
use core::ops::Deref;
use std::os::raw::c_char;
use std::sync::Once;

use crate::encode::{Encode, Encoding, RefEncode};
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
feature(repr_simd)
)]

use std::os::raw::c_void;
use core::ffi::c_void;

use block2::Block;
extern crate alloc;
Expand Down
4 changes: 2 additions & 2 deletions crates/tests/src/test_encode_utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![allow(non_snake_case)]
use core::ffi::CStr;
use core::ffi::*;
use core::fmt::Display;
use core::sync::atomic::{AtomicI32, AtomicPtr};
use objc2::ffi::{NSInteger, NSUInteger};
use objc2::runtime::{AnyClass, AnyObject, Bool, Sel};
use objc2::{Encode, Encoding};
use paste::paste;
use std::ffi::CStr;
use std::os::raw::*;
use std::string::ToString;

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/src/test_object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::missing_safety_doc)]
use core::ffi::c_int;
use core::mem::{size_of, ManuallyDrop};
use std::os::raw::c_int;

use objc2::encode::{Encoding, RefEncode};
use objc2::rc::{autoreleasepool, AutoreleasePool, Retained};
Expand Down
Loading

0 comments on commit ec058b3

Please sign in to comment.