Skip to content

Commit 321d4cb

Browse files
committed
Improve no_std support in dispatch2 and objc2-foundation
1 parent e6eda12 commit 321d4cb

File tree

20 files changed

+56
-33
lines changed

20 files changed

+56
-33
lines changed

crates/dispatch2/src/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![allow(missing_docs, non_camel_case_types)]
44

55
use core::ffi::{c_long, c_uint, c_ulong, c_void};
6-
use std::ptr::addr_of;
6+
use core::ptr::addr_of;
77

88
#[cfg(feature = "objc2")]
99
use objc2::encode::{Encode, Encoding, RefEncode};

crates/dispatch2/src/group.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Dispatch group definition.
22
3-
use std::time::Duration;
4-
3+
use alloc::boxed::Box;
54
use core::ffi::c_void;
5+
use core::time::Duration;
66

77
use super::object::DispatchObject;
88
use super::queue::Queue;

crates/dispatch2/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@
1717
//! queue.exec_async(|| println!("Hello"));
1818
//! queue.exec_sync(|| println!("World"));
1919
//! ```
20+
#![no_std]
2021
#![allow(unreachable_patterns)]
2122
#![warn(missing_docs)]
2223
#![warn(clippy::undocumented_unsafe_blocks)]
2324
#![warn(clippy::missing_safety_doc)]
2425
// Update in Cargo.toml as well.
2526
#![doc(html_root_url = "https://docs.rs/dispatch2/0.1.0")]
2627

28+
#[cfg(not(feature = "alloc"))]
29+
compile_error!("The `alloc` feature currently must be enabled.");
30+
31+
extern crate alloc;
32+
33+
#[cfg(feature = "std")]
34+
extern crate std;
35+
2736
use self::ffi::dispatch_qos_class_t;
2837

2938
pub mod ffi;

crates/dispatch2/src/main_thread_bound.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<T> MainThreadBound<T> {
120120
/// Create a shared static that is only available from the main thread.
121121
///
122122
/// ```
123-
/// use std::cell::Cell;
123+
/// use core::cell::Cell;
124124
/// use dispatch2::MainThreadBound;
125125
/// use objc2::MainThreadMarker;
126126
///

crates/dispatch2/src/object.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Dispatch object definition.
22
3+
use alloc::boxed::Box;
4+
35
use super::{ffi::*, queue::Queue, utils::function_wrapper, QualityOfServiceClass};
46

57
/// Error returned by [DispatchObject::set_target_queue].

crates/dispatch2/src/once.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
use core::cell::UnsafeCell;
12
use core::ffi::c_void;
23
use core::fmt;
4+
use core::panic::{RefUnwindSafe, UnwindSafe};
35
use core::ptr::NonNull;
4-
use std::cell::UnsafeCell;
5-
use std::panic::{RefUnwindSafe, UnwindSafe};
6-
use std::sync::atomic::AtomicIsize;
7-
use std::sync::atomic::Ordering;
6+
use core::sync::atomic::{AtomicIsize, Ordering};
87

98
use crate::ffi;
109

@@ -166,13 +165,13 @@ impl Once {
166165
invoke_dispatch_once(predicate, work);
167166
}
168167

169-
// NOTE: Unlike in C, we cannot use `std::hint::assert_unchecked`,
168+
// NOTE: Unlike in C, we cannot use `core::hint::assert_unchecked`,
170169
// since that would actually be lying from a language perspective;
171170
// the value seems to only settle on being !0 after some time
172171
// (something about the _COMM_PAGE_CPU_QUIESCENT_COUNTER?)
173172
//
174173
// TODO: Investigate this further!
175-
// std::hint::assert_unchecked(atomic_predicate.load(Ordering::Acquire) == !0);
174+
// core::hint::assert_unchecked(atomic_predicate.load(Ordering::Acquire) == !0);
176175
} else {
177176
invoke_dispatch_once(predicate, work);
178177
}
@@ -196,9 +195,8 @@ impl fmt::Debug for Once {
196195

197196
#[cfg(test)]
198197
mod tests {
199-
use std::cell::Cell;
200-
use std::mem::ManuallyDrop;
201-
use std::thread;
198+
use core::cell::Cell;
199+
use core::mem::ManuallyDrop;
202200

203201
use super::*;
204202

@@ -249,12 +247,13 @@ mod tests {
249247
}
250248

251249
#[test]
250+
#[cfg(feature = "std")]
252251
fn test_threaded() {
253252
let once = Once::new();
254253

255254
let num = AtomicIsize::new(0);
256255

257-
thread::scope(|scope| {
256+
std::thread::scope(|scope| {
258257
scope.spawn(|| {
259258
once.call_once(|| {
260259
num.fetch_add(1, Ordering::Relaxed);
@@ -329,9 +328,7 @@ mod tests {
329328
let once = Once::new();
330329

331330
once.call_once(|| {
332-
once.call_once(|| {
333-
println!("foo");
334-
});
331+
once.call_once(|| {});
335332
});
336333
}
337334
}

crates/dispatch2/src/queue.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! Dispatch queue definition.
22
3-
use std::borrow::{Borrow, BorrowMut};
4-
use std::ffi::CString;
5-
use std::ops::{Deref, DerefMut};
6-
use std::ptr::NonNull;
7-
use std::time::Duration;
3+
use alloc::boxed::Box;
4+
use alloc::ffi::CString;
5+
use core::borrow::{Borrow, BorrowMut};
6+
use core::ops::{Deref, DerefMut};
7+
use core::ptr::NonNull;
8+
use core::time::Duration;
89

910
use super::object::{DispatchObject, QualityOfServiceClassFloorError, TargetQueueError};
1011
use super::utils::function_wrapper;

crates/dispatch2/src/semaphore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Dispatch semaphore definition.
22
3-
use std::time::Duration;
3+
use core::time::Duration;
44

55
use super::ffi::*;
66
use super::object::DispatchObject;

crates/dispatch2/src/utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::time::Duration;
2-
1+
use alloc::boxed::Box;
32
use core::ffi::c_void;
3+
use core::time::Duration;
44

55
use super::ffi::{dispatch_time, dispatch_time_t, DISPATCH_TIME_NOW};
66

framework-crates/objc2-core-graphics/translation-config.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct.__IOSurface.skipped = true
2424
# Needs io_service_t from the kernel
2525
fn.CGDisplayIOServicePort.skipped = true
2626

27-
# Needs std::ffi::VaList, currently unstable
27+
# Needs core::ffi::VaList, currently unstable
2828
fn.CGColorConversionInfoCreateFromListWithArguments.skipped = true
2929

3030
# Unknown how to handle the calling convention here?

framework-crates/objc2-foundation/src/data.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl NSData {
3939
}
4040

4141
#[cfg(feature = "block2")]
42+
#[cfg(feature = "alloc")]
4243
pub fn from_vec(bytes: Vec<u8>) -> Retained<Self> {
4344
// GNUStep's NSData `initWithBytesNoCopy:length:deallocator:` has a
4445
// bug; it forgets to assign the input buffer and length to the
@@ -201,7 +202,7 @@ pub struct Iter<'a> {
201202
data: &'a NSData,
202203
#[cfg(debug_assertions)]
203204
length: usize,
204-
bytes: std::vec::IntoIter<u8>,
205+
bytes: alloc::vec::IntoIter<u8>,
205206
}
206207

207208
impl<'a> Iter<'a> {
@@ -289,6 +290,7 @@ impl<'a> Extend<&'a u8> for &NSMutableData {
289290
}
290291
}
291292

293+
#[cfg(feature = "std")]
292294
impl std::io::Write for &NSMutableData {
293295
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
294296
self.extend_from_slice(buf);
@@ -322,6 +324,7 @@ impl RetainedFromIterator<u8> for NSMutableData {
322324
}
323325

324326
#[cfg(feature = "block2")]
327+
#[cfg(feature = "alloc")]
325328
unsafe fn with_vec<T: objc2::Message>(obj: objc2::rc::Allocated<T>, bytes: Vec<u8>) -> Retained<T> {
326329
use core::mem::ManuallyDrop;
327330

framework-crates/objc2-foundation/src/dictionary.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Utilities for the `NSDictionary` and `NSMutableDictionary` classes.
2+
#[cfg(feature = "alloc")]
23
use alloc::vec::Vec;
34
use core::fmt;
45
use core::mem;
@@ -215,6 +216,7 @@ impl<KeyType: Message, ObjectType: Message> NSDictionary<KeyType, ObjectType> {
215216
/// The dictionary must not be mutated while the returned references are
216217
/// alive.
217218
#[doc(alias = "getObjects:andKeys:")]
219+
#[cfg(feature = "alloc")]
218220
pub unsafe fn to_vecs_unchecked(&self) -> (Vec<&KeyType>, Vec<&ObjectType>) {
219221
let len = self.len();
220222
let mut keys = Vec::with_capacity(len);
@@ -314,6 +316,7 @@ impl<KeyType: Message, ObjectType: Message> NSDictionary<KeyType, ObjectType> {
314316
/// }
315317
/// ```
316318
#[doc(alias = "getObjects:")]
319+
#[cfg(feature = "alloc")]
317320
pub fn to_vecs(&self) -> (Vec<Retained<KeyType>>, Vec<Retained<ObjectType>>) {
318321
// SAFETY: We retain the elements below, so that we know that the
319322
// dictionary isn't mutated while they are alive.

framework-crates/objc2-foundation/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
#![allow(non_snake_case)]
7373
#![recursion_limit = "512"]
7474

75-
#[cfg(feature = "alloc")]
75+
#[cfg(not(feature = "alloc"))]
76+
compile_error!("The `alloc` feature currently must be enabled.");
77+
7678
extern crate alloc;
7779

7880
#[cfg(feature = "std")]

framework-crates/objc2-foundation/src/tests/bundle.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use alloc::format;
33

44
use crate::NSBundle;
5+
use objc2::runtime::NSObjectProtocol;
56

67
#[test]
78
#[cfg(feature = "NSString")]
@@ -10,8 +11,9 @@ use crate::NSBundle;
1011
fn try_running_functions() {
1112
// This is mostly empty since cargo doesn't bundle the application
1213
// before executing.
14+
1315
let bundle = NSBundle::mainBundle();
14-
std::println!("{bundle:?}");
16+
let _ = bundle.description();
1517
assert_eq!(format!("{:?}", bundle.infoDictionary().unwrap()), "{}");
1618
assert_eq!(bundle.name(), None);
1719
}

framework-crates/objc2-foundation/src/tests/dictionary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#![cfg(feature = "NSDictionary")]
22
#![cfg(feature = "NSString")]
33
#![cfg(feature = "NSObject")]
4+
use alloc::ffi::CString;
45
use alloc::format;
56
use alloc::string::ToString;
67
use core::ptr;
7-
use std::ffi::CString;
88

99
use objc2::{
1010
ffi, msg_send,

framework-crates/objc2-foundation/src/tests/mutable_array.rs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ fn test_iter_mutation_detection() {
6969
feature = "gnustep-1-7",
7070
ignore = "thread safety issues regarding initialization"
7171
)]
72+
#[cfg(feature = "std")]
7273
fn test_threaded() {
7374
std::thread::scope(|s| {
7475
s.spawn(|| {

framework-crates/objc2-foundation/src/tests/number.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ fn float_int_equality() {
113113
#[test]
114114
#[cfg(feature = "NSString")]
115115
fn display_debug() {
116-
use std::{fmt, format};
116+
use alloc::format;
117+
use core::fmt;
117118

118119
fn assert_display_debug<T: fmt::Debug + fmt::Display>(val: T, expected: &str) {
119120
// The two impls for these happen to be the same

framework-crates/objc2-foundation/src/tests/string.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,11 @@ fn test_strips_first_leading_zero_width_no_break_space() {
136136
}
137137

138138
#[test]
139+
#[cfg(feature = "std")]
139140
fn test_hash() {
141+
use core::hash::Hash;
140142
use core::hash::Hasher;
141143
use std::collections::hash_map::DefaultHasher;
142-
use std::hash::Hash;
143144

144145
let s1 = NSString::from_str("example string goes here");
145146
let s2 = NSString::from_str("example string goes here");

framework-crates/objc2-foundation/src/tests/thread.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn test_main_thread() {
2525
}
2626

2727
#[test]
28+
#[cfg(feature = "std")]
2829
fn test_not_main_thread() {
2930
let res = std::thread::spawn(|| NSThread::currentThread().isMainThread())
3031
.join()

framework-crates/objc2-metal/src/slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(unused_imports)]
22
#![allow(clippy::missing_safety_doc)]
3-
use std::ptr::NonNull;
3+
use core::ptr::NonNull;
44

55
use crate::*;
66

0 commit comments

Comments
 (0)