Skip to content

Commit cfe3c3e

Browse files
committed
Don't automatically enable std and alloc features of dependencies
Unless it's currently required. Part of #656.
1 parent 321d4cb commit cfe3c3e

File tree

107 files changed

+477
-642
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+477
-642
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ env:
4646
--package=objc2-exception-helper
4747
4848
# BEGIN AUTOMATICALLY GENERATED
49-
FRAMEWORKS_MACOS_10_12: --package=objc2-accounts --package=objc2-audio-toolbox --package=objc2-automator --package=objc2-av-foundation --package=objc2-av-kit --package=objc2-cloud-kit --package=objc2-contacts --package=objc2-contacts-ui --package=objc2-core-bluetooth --package=objc2-core-data --package=objc2-core-foundation --package=objc2-core-graphics --package=objc2-core-image --package=objc2-core-location --package=objc2-core-media --package=objc2-core-text --package=objc2-core-video --package=objc2-core-wlan --package=objc2-event-kit --package=objc2-exception-handling --package=objc2-finder-sync --package=objc2-foundation --package=objc2-game-controller --package=objc2-game-kit --package=objc2-input-method-kit --package=objc2-io-surface --package=objc2-local-authentication --package=objc2-map-kit --package=objc2-media-player --package=objc2-metal --package=objc2-metal-kit --package=objc2-multipeer-connectivity --package=objc2-network-extension --package=objc2-open-directory --package=objc2-osa-kit --package=objc2-photos-ui --package=objc2-quartz-core --package=objc2-screen-saver --package=objc2-service-management --package=objc2-social --package=objc2-store-kit --package=objc2-web-kit
49+
FRAMEWORKS_MACOS_10_12: --package=objc2-accounts --package=objc2-audio-toolbox --package=objc2-automator --package=objc2-av-foundation --package=objc2-av-kit --package=objc2-cloud-kit --package=objc2-contacts --package=objc2-contacts-ui --package=objc2-core-bluetooth --package=objc2-core-data --package=objc2-core-foundation --package=objc2-core-graphics --package=objc2-core-location --package=objc2-core-media --package=objc2-core-text --package=objc2-core-video --package=objc2-core-wlan --package=objc2-event-kit --package=objc2-exception-handling --package=objc2-finder-sync --package=objc2-foundation --package=objc2-game-controller --package=objc2-game-kit --package=objc2-input-method-kit --package=objc2-io-surface --package=objc2-local-authentication --package=objc2-map-kit --package=objc2-media-player --package=objc2-metal --package=objc2-metal-kit --package=objc2-multipeer-connectivity --package=objc2-network-extension --package=objc2-open-directory --package=objc2-osa-kit --package=objc2-photos-ui --package=objc2-quartz-core --package=objc2-screen-saver --package=objc2-service-management --package=objc2-social --package=objc2-store-kit --package=objc2-web-kit
5050
FRAMEWORKS_MACOS_10_13: --package=objc2-accounts --package=objc2-audio-toolbox --package=objc2-automator --package=objc2-av-foundation --package=objc2-av-kit --package=objc2-cloud-kit --package=objc2-contacts --package=objc2-contacts-ui --package=objc2-core-bluetooth --package=objc2-core-data --package=objc2-core-foundation --package=objc2-core-graphics --package=objc2-core-image --package=objc2-core-location --package=objc2-core-media --package=objc2-core-ml --package=objc2-core-text --package=objc2-core-video --package=objc2-core-wlan --package=objc2-event-kit --package=objc2-exception-handling --package=objc2-external-accessory --package=objc2-finder-sync --package=objc2-foundation --package=objc2-game-controller --package=objc2-game-kit --package=objc2-input-method-kit --package=objc2-io-surface --package=objc2-local-authentication --package=objc2-map-kit --package=objc2-media-player --package=objc2-metal --package=objc2-metal-kit --package=objc2-metal-performance-shaders --package=objc2-multipeer-connectivity --package=objc2-network-extension --package=objc2-open-directory --package=objc2-osa-kit --package=objc2-photos-ui --package=objc2-quartz-core --package=objc2-screen-saver --package=objc2-service-management --package=objc2-social --package=objc2-store-kit --package=objc2-vision --package=objc2-web-kit
5151
FRAMEWORKS_MACOS_11: --package=objc2-accessibility --package=objc2-accounts --package=objc2-ad-support --package=objc2-app-kit --package=objc2-app-tracking-transparency --package=objc2-audio-toolbox --package=objc2-authentication-services --package=objc2-automatic-assessment-configuration --package=objc2-automator --package=objc2-av-foundation --package=objc2-av-kit --package=objc2-business-chat --package=objc2-class-kit --package=objc2-cloud-kit --package=objc2-contacts --package=objc2-contacts-ui --package=objc2-core-audio-types --package=objc2-core-bluetooth --package=objc2-core-data --package=objc2-core-foundation --package=objc2-core-graphics --package=objc2-core-image --package=objc2-core-location --package=objc2-core-media --package=objc2-core-ml --package=objc2-core-motion --package=objc2-core-text --package=objc2-core-video --package=objc2-core-wlan --package=objc2-device-check --package=objc2-event-kit --package=objc2-exception-handling --package=objc2-external-accessory --package=objc2-file-provider --package=objc2-file-provider-ui --package=objc2-finder-sync --package=objc2-foundation --package=objc2-game-controller --package=objc2-game-kit --package=objc2-identity-lookup --package=objc2-input-method-kit --package=objc2-io-surface --package=objc2-link-presentation --package=objc2-local-authentication --package=objc2-map-kit --package=objc2-media-player --package=objc2-metal --package=objc2-metal-kit --package=objc2-metal-performance-shaders --package=objc2-metal-performance-shaders-graph --package=objc2-ml-compute --package=objc2-multipeer-connectivity --package=objc2-natural-language --package=objc2-nearby-interaction --package=objc2-network-extension --package=objc2-open-directory --package=objc2-osa-kit --package=objc2-photos --package=objc2-photos-ui --package=objc2-quartz-core --package=objc2-screen-saver --package=objc2-service-management --package=objc2-social --package=objc2-sound-analysis --package=objc2-speech --package=objc2-store-kit --package=objc2-system-extensions --package=objc2-uniform-type-identifiers --package=objc2-user-notifications --package=objc2-virtualization --package=objc2-vision --package=objc2-web-kit
5252
FRAMEWORKS_MACOS_12: --package=objc2-accessibility --package=objc2-accounts --package=objc2-ad-services --package=objc2-ad-support --package=objc2-app-kit --package=objc2-app-tracking-transparency --package=objc2-audio-toolbox --package=objc2-authentication-services --package=objc2-automatic-assessment-configuration --package=objc2-automator --package=objc2-av-foundation --package=objc2-av-kit --package=objc2-avf-audio --package=objc2-business-chat --package=objc2-class-kit --package=objc2-cloud-kit --package=objc2-contacts --package=objc2-contacts-ui --package=objc2-core-audio-types --package=objc2-core-bluetooth --package=objc2-core-data --package=objc2-core-foundation --package=objc2-core-graphics --package=objc2-core-image --package=objc2-core-location --package=objc2-core-media --package=objc2-core-ml --package=objc2-core-motion --package=objc2-core-text --package=objc2-core-video --package=objc2-core-wlan --package=objc2-data-detection --package=objc2-device-check --package=objc2-event-kit --package=objc2-exception-handling --package=objc2-external-accessory --package=objc2-file-provider --package=objc2-file-provider-ui --package=objc2-finder-sync --package=objc2-foundation --package=objc2-game-controller --package=objc2-game-kit --package=objc2-identity-lookup --package=objc2-input-method-kit --package=objc2-io-surface --package=objc2-link-presentation --package=objc2-local-authentication --package=objc2-local-authentication-embedded-ui --package=objc2-mail-kit --package=objc2-map-kit --package=objc2-media-player --package=objc2-metal --package=objc2-metal-kit --package=objc2-metal-performance-shaders --package=objc2-metal-performance-shaders-graph --package=objc2-metric-kit --package=objc2-ml-compute --package=objc2-multipeer-connectivity --package=objc2-natural-language --package=objc2-nearby-interaction --package=objc2-network-extension --package=objc2-open-directory --package=objc2-osa-kit --package=objc2-photos --package=objc2-photos-ui --package=objc2-quartz-core --package=objc2-screen-saver --package=objc2-service-management --package=objc2-social --package=objc2-sound-analysis --package=objc2-speech --package=objc2-store-kit --package=objc2-system-extensions --package=objc2-uniform-type-identifiers --package=objc2-user-notifications --package=objc2-virtualization --package=objc2-vision --package=objc2-web-kit

crates/block2/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ workspace = true
2222
default = ["std"]
2323

2424
# Currently not possible to turn off, put here for forwards compatibility.
25-
std = ["alloc", "objc2/std"]
26-
alloc = ["objc2/alloc"]
25+
std = ["alloc"]
26+
alloc = []
2727

2828
# Deprecated; this is the default on Apple platforms, and not applicable on other platforms.
2929
apple = []
@@ -48,12 +48,12 @@ unstable-objfw = []
4848
unstable-private = []
4949

5050
[dependencies]
51-
objc2 = { path = "../objc2", version = "0.5.2", default-features = false }
51+
objc2 = { path = "../objc2", version = "0.5.2", default-features = false, features = ["std"] }
5252

5353
[dev-dependencies.objc2-foundation]
5454
path = "../../framework-crates/objc2-foundation"
5555
default-features = false
56-
features = ["NSError"]
56+
features = ["std", "NSError"]
5757

5858
[package.metadata.docs.rs]
5959
default-target = "aarch64-apple-darwin"

crates/block2/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,13 @@
304304
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg_hide))]
305305
#![cfg_attr(docsrs, doc(cfg_hide(doc)))]
306306

307+
#[cfg(not(feature = "alloc"))]
308+
compile_error!("The `alloc` feature currently must be enabled.");
309+
307310
extern crate alloc;
311+
#[cfg(feature = "std")]
308312
extern crate std;
309313

310-
#[cfg(not(feature = "std"))]
311-
compile_error!("The `std` feature currently must be enabled.");
312-
313314
#[cfg(all(
314315
not(docsrs),
315316
not(any(

crates/block2/src/rc_block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<F: ?Sized> RcBlock<F> {
154154
/// # Example
155155
///
156156
/// ```
157-
/// # use std::ffi::CStr;
157+
/// # use core::ffi::CStr;
158158
/// # use block2::{Block, ManualBlockEncoding, RcBlock};
159159
/// # use objc2_foundation::NSError;
160160
/// #
@@ -173,7 +173,7 @@ impl<F: ?Sized> RcBlock<F> {
173173
/// let my_block = RcBlock::with_encoding::<_, _, _, MyBlockEncoding>(|_err: *mut NSError| {
174174
/// 42i32
175175
/// });
176-
/// assert_eq!(my_block.call((std::ptr::null_mut(),)), 42);
176+
/// assert_eq!(my_block.call((core::ptr::null_mut(),)), 42);
177177
/// ```
178178
#[inline]
179179
pub fn with_encoding<'f, A, R, Closure, E>(closure: Closure) -> Self

crates/block2/src/stack.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ where
205205
/// # Example
206206
///
207207
/// ```
208-
/// # use std::ffi::CStr;
208+
/// # use core::ffi::CStr;
209209
/// # use block2::{Block, ManualBlockEncoding, StackBlock};
210210
/// # use objc2_foundation::NSError;
211211
/// #
@@ -224,7 +224,7 @@ where
224224
/// let my_block = StackBlock::with_encoding::<MyBlockEncoding>(|_err: *mut NSError| {
225225
/// 42i32
226226
/// });
227-
/// assert_eq!(my_block.call((std::ptr::null_mut(),)), 42);
227+
/// assert_eq!(my_block.call((core::ptr::null_mut(),)), 42);
228228
/// ```
229229
#[inline]
230230
pub fn with_encoding<E>(closure: Closure) -> Self

crates/block2/src/traits.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use core::ffi::CStr;
12
use core::marker::PhantomData;
23
use core::mem;
34
use core::ptr;
4-
// TODO: use `core` when the MSRV is at least 1.64.
5-
use std::ffi::CStr;
65

76
use objc2::encode::EncodeArguments;
87
use objc2::encode::{EncodeArgument, EncodeReturn};
@@ -347,7 +346,7 @@ mod tests {
347346
unsafe { CStr::from_bytes_with_nul_unchecked(b"C12@?0i4f8\0") };
348347
}
349348
// HACK: use `identity` in order to circumvent a Clippy warning.
350-
assert!(!std::convert::identity(UserSpecified::<Enc1>::IS_NONE));
349+
assert!(!core::convert::identity(UserSpecified::<Enc1>::IS_NONE));
351350

352351
// No input + no output case.
353352
struct Enc2;
@@ -361,7 +360,7 @@ mod tests {
361360
const ENCODING_CSTR: &'static CStr =
362361
unsafe { CStr::from_bytes_with_nul_unchecked(b"v4@?0\0") };
363362
}
364-
assert!(!std::convert::identity(UserSpecified::<Enc2>::IS_NONE));
363+
assert!(!core::convert::identity(UserSpecified::<Enc2>::IS_NONE));
365364

366365
// Ensure we don't rely on the encoding string's emptiness.
367366
struct Enc3;
@@ -371,14 +370,14 @@ mod tests {
371370
const ENCODING_CSTR: &'static CStr =
372371
unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") };
373372
}
374-
assert!(!std::convert::identity(UserSpecified::<Enc3>::IS_NONE));
373+
assert!(!core::convert::identity(UserSpecified::<Enc3>::IS_NONE));
375374

376375
// Only `NoBlockEncoding` should be `IS_NONE`.
377-
assert!(std::convert::identity(NoBlockEncoding::<(), ()>::IS_NONE));
378-
assert!(std::convert::identity(
376+
assert!(core::convert::identity(NoBlockEncoding::<(), ()>::IS_NONE));
377+
assert!(core::convert::identity(
379378
NoBlockEncoding::<(i32, f32), u8>::IS_NONE
380379
));
381-
assert!(std::convert::identity(
380+
assert!(core::convert::identity(
382381
NoBlockEncoding::<(*const u8,), *const c_char>::IS_NONE
383382
));
384383
}

crates/dispatch2/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
5050
crate.
5151
- Added `Once`, a wrapper over `dispatch_once_f` which works similarly to
5252
`std::sync::Once`.
53+
- Added `#![no_std]` support
5354

5455
### Changed
5556
- Moved to the `objc2` project.

crates/dispatch2/Cargo.toml

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ workspace = true
1818

1919
[dependencies]
2020
bitflags = { version = "2.5.0", default-features = false }
21-
block2 = { path = "../block2", version = "0.5.1", default-features = false, optional = true }
21+
block2 = { path = "../block2", version = "0.5.1", default-features = false, optional = true, features = ["alloc"] }
2222
libc = { version = "0.2.80", default-features = false, optional = true }
23-
objc2 = { path = "../objc2", version = "0.5.2", default-features = false, optional = true }
23+
objc2 = { path = "../objc2", version = "0.5.2", default-features = false, optional = true, features = ["std"] }
2424

2525
[package.metadata.docs.rs]
2626
default-target = "aarch64-apple-darwin"
@@ -39,10 +39,8 @@ targets = [
3939

4040
[features]
4141
default = ["std"]
42-
43-
# Currently not possible to turn off, put here for forwards compatibility.
44-
std = ["alloc", "bitflags/std", "block2?/std", "libc?/std", "objc2?/std"]
45-
alloc = ["block2?/alloc", "objc2?/alloc"]
42+
std = ["alloc", "bitflags/std"]
43+
alloc = []
4644
block2 = ["dep:block2"]
4745
libc = ["dep:libc"]
4846
objc2 = ["dep:objc2"]

crates/header-translator/src/default_cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,5 @@ targets = [
2626

2727
[features]
2828
default = ["std"]
29-
30-
# Currently not possible to turn off, put here for forwards compatibility.
3129
std = ["alloc"]
3230
alloc = []

crates/header-translator/src/library.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ impl Library {
116116
}
117117
}
118118

119+
for (krate, (_, _, krate_features)) in &mut dependencies {
120+
// std is currently required for objc2
121+
if *krate == "objc2" {
122+
krate_features.insert("std".into());
123+
}
124+
// alloc is currently required for these crates
125+
if matches!(*krate, "block2" | "dispatch2" | "objc2-foundation") {
126+
krate_features.insert("alloc".into());
127+
}
128+
}
129+
119130
dependencies
120131
}
121132

@@ -273,6 +284,10 @@ see that for related crates.", self.data.krate)?;
273284
(false, false) => format!("../{krate}"),
274285
};
275286
let mut table = match *krate {
287+
"dispatch2" => InlineTable::from_iter([
288+
("path", Value::from(path)),
289+
("version", Value::from("0.1.0")),
290+
]),
276291
"objc2" => InlineTable::from_iter([
277292
("path", Value::from(path)),
278293
("version", Value::from("0.5.2")),
@@ -327,19 +342,15 @@ see that for related crates.", self.data.krate)?;
327342
.entry(krate)
328343
.or_insert(Item::Value(Value::InlineTable(table)));
329344

330-
cargo_toml["features"]["std"]
331-
.as_array_mut()
332-
.unwrap()
333-
.push(Value::from(format!(
334-
"{krate}{}/std",
335-
if *required { "" } else { "?" },
336-
)));
337-
if *krate != "bitflags" && *krate != "libc" {
338-
cargo_toml["features"]["alloc"]
345+
// Bitflags is more of an "internal" dependency, just like
346+
// objc2-encode is mostly an internal dependency to objc2, so it's
347+
// fine to special-case std for that here.
348+
if *krate == "bitflags" {
349+
cargo_toml["features"]["std"]
339350
.as_array_mut()
340351
.unwrap()
341352
.push(Value::from(format!(
342-
"{krate}{}/alloc",
353+
"{krate}{}/std",
343354
if *required { "" } else { "?" },
344355
)));
345356
}

crates/header-translator/src/main.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,10 @@ fn update_ci(workspace_dir: &Path, config: &Config) -> io::Result<()> {
576576
lib.macos
577577
.as_ref()
578578
.is_some_and(|v| VersionReq::parse("<=10.12").unwrap().matches(v))
579-
// HACK: These depend on `objc2-uniform-type-identifiers`, which
580-
// is not available on macOS 10.12, but will be enabled by `"all"`
581-
&& !["objc2-app-kit", "objc2-file-provider", "objc2-health-kit", "objc2-photos"].contains(&&*lib.krate)
579+
// HACK: These depend on `objc2-uniform-type-identifiers` or
580+
// `objc2-core-ml`, which is not available on macOS 10.12, but
581+
// will be enabled by `"all"`.
582+
&& !["objc2-app-kit", "objc2-file-provider", "objc2-health-kit", "objc2-photos", "objc2-core-image"].contains(&&*lib.krate)
582583
})?;
583584
writer(&mut ci, config, "FRAMEWORKS_MACOS_10_13", |lib| {
584585
lib.macos

crates/objc2-encode/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@
4545
#[cfg(not(feature = "alloc"))]
4646
compile_error!("the `alloc` feature currently must be enabled");
4747

48+
extern crate alloc;
4849
#[cfg(any(feature = "std", doc))]
4950
extern crate std;
5051

51-
#[cfg(any(feature = "alloc", test))]
52-
extern crate alloc;
53-
5452
mod encoding;
5553
mod encoding_box;
5654
mod helper;

crates/objc2/Cargo.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ workspace = true
2525
default = ["std"]
2626

2727
# Currently not possible to turn off, put here for forwards compatibility.
28-
std = ["alloc", "objc2-encode/std", "block2/std", "objc2-foundation/std", "objc2-exception-helper?/std"]
29-
alloc = ["objc2-encode/alloc", "block2/alloc", "objc2-foundation/alloc", "objc2-exception-helper?/alloc"]
28+
std = ["alloc", "objc2-encode/std"]
29+
alloc = ["objc2-encode/alloc"]
3030

3131
# Enables `objc2::exception::throw` and `objc2::exception::catch`
3232
exception = ["dep:objc2-exception-helper"]
@@ -125,11 +125,12 @@ objc2-exception-helper = { path = "../objc2-exception-helper", version = "0.1.0"
125125
iai = { version = "0.1", git = "https://github.com/madsmtm/iai", branch = "callgrind" }
126126
static_assertions = "1.1.0"
127127
memoffset = "0.9.0"
128-
block2 = { path = "../block2", default-features = false }
128+
block2 = { path = "../block2" }
129129
objc2-core-foundation = { path = "../../framework-crates/objc2-core-foundation", default-features = false, features = [
130130
"CFCGTypes",
131131
] }
132132
objc2-foundation = { path = "../../framework-crates/objc2-foundation", default-features = false, features = [
133+
"std",
133134
"NSArray",
134135
"NSDate",
135136
"NSDictionary",

crates/objc2/src/topics/about_generated/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
105105
* **BREAKING**: The feature flag guarding `SCSensitivityAnalysis` changed.
106106
* **BREAKING**: `-[NSSavePanel beginSheetForDirectory:file:modalForWindow:modalDelegate:didEndSelector:contextInfo:]`
107107
now takes an optional value as the file path.
108+
* **BREAKING**: No longer automatically enable `std` and `alloc` features of
109+
dependencies. If you want a certain framework crate to use `std` or `alloc`
110+
features, you cannot rely on a higher-level crate to enable that for you.
108111

109112
### Deprecated
110113
* Moved `MainThreadMarker` from `objc2-foundation` to `objc2`.

0 commit comments

Comments
 (0)