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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 10 additions & 13 deletions
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

Lines changed: 6 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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?

0 commit comments

Comments
 (0)