Skip to content

Commit 74d3254

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 74d3254

File tree

105 files changed

+481
-648
lines changed

Some content is hidden

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

105 files changed

+481
-648
lines changed

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/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`.

crates/test-assembly/crates/test_define_class/Cargo.toml

+10-10
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ publish = false
88
path = "lib.rs"
99

1010
[dependencies]
11-
objc2 = { path = "../../../objc2", optional = true }
12-
objc2-foundation = { path = "../../../../framework-crates/objc2-foundation", optional = true }
11+
objc2 = { path = "../../../objc2" }
12+
objc2-foundation = { path = "../../../../framework-crates/objc2-foundation" }
1313

1414
[features]
15-
default = ["all", "objc2", "objc2-foundation"]
15+
default = ["all"]
1616
# Runtime
17-
gnustep-1-7 = ["objc2?/gnustep-1-7", "objc2-foundation?/gnustep-1-7"]
18-
gnustep-1-8 = ["gnustep-1-7", "objc2?/gnustep-1-8", "objc2-foundation?/gnustep-1-8"]
19-
gnustep-1-9 = ["gnustep-1-8", "objc2?/gnustep-1-9", "objc2-foundation?/gnustep-1-9"]
20-
gnustep-2-0 = ["gnustep-1-9", "objc2?/gnustep-2-0", "objc2-foundation?/gnustep-2-0"]
21-
gnustep-2-1 = ["gnustep-2-0", "objc2?/gnustep-2-1", "objc2-foundation?/gnustep-2-1"]
17+
gnustep-1-7 = ["objc2/gnustep-1-7", "objc2-foundation/gnustep-1-7"]
18+
gnustep-1-8 = ["gnustep-1-7", "objc2/gnustep-1-8", "objc2-foundation/gnustep-1-8"]
19+
gnustep-1-9 = ["gnustep-1-8", "objc2/gnustep-1-9", "objc2-foundation/gnustep-1-9"]
20+
gnustep-2-0 = ["gnustep-1-9", "objc2/gnustep-2-0", "objc2-foundation/gnustep-2-0"]
21+
gnustep-2-1 = ["gnustep-2-0", "objc2/gnustep-2-1", "objc2-foundation/gnustep-2-1"]
2222

23-
all = ["objc2-foundation?/block2", "objc2-foundation?/NSObject", "objc2-foundation?/NSZone"]
23+
all = ["objc2-foundation/block2", "objc2-foundation/NSObject", "objc2-foundation/NSZone"]
2424

2525
# Hack to prevent the feature flag from being enabled in the entire project
26-
assembly-features = ["all", "objc2?/unstable-static-sel-inlined", "objc2?/unstable-static-class-inlined"]
26+
assembly-features = ["all", "objc2/unstable-static-sel-inlined", "objc2/unstable-static-class-inlined"]
2727

2828
[package.metadata.release]
2929
release = false

crates/test-ui/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ gnustep-2-1 = ["gnustep-2-0", "block2/gnustep-2-1", "objc2/gnustep-2-1", "objc2-
3939
trybuild = { version = "1.0.72", optional = true }
4040
block2 = { path = "../block2" }
4141
objc2 = { path = "../objc2" }
42-
objc2-foundation = { path = "../../framework-crates/objc2-foundation" }
42+
objc2-foundation = { path = "../../framework-crates/objc2-foundation", default-features = false, features = ["std"] }
4343

4444
# To make CI work
4545
[target.'cfg(not(target_vendor = "apple"))'.dependencies]
4646
block2 = { path = "../block2", features = ["gnustep-1-7"] }
4747
objc2 = { path = "../objc2", features = ["gnustep-1-7"] }
48-
objc2-foundation = { path = "../../framework-crates/objc2-foundation", features = ["gnustep-1-7"] }
48+
objc2-foundation = { path = "../../framework-crates/objc2-foundation", default-features = false, features = ["std", "gnustep-1-7"] }
4949

5050
[[bin]]
5151
name = "test-ui"

crates/tests/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ unstable-objfw = ["block2/unstable-objfw", "objc2/unstable-objfw"]
3838
[dependencies]
3939
block2 = { path = "../block2" }
4040
objc2 = { path = "../objc2" }
41-
objc2-foundation = { path = "../../framework-crates/objc2-foundation" }
41+
objc2-foundation = { path = "../../framework-crates/objc2-foundation", default-features = false, features = ["std"] }
4242

4343
[build-dependencies]
4444
cc = "1.0"

framework-crates/objc2-accessibility/Cargo.toml

+5-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

framework-crates/objc2-accounts/Cargo.toml

+5-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)