Skip to content

Commit

Permalink
Don't automatically enable std and alloc features of dependencies
Browse files Browse the repository at this point in the history
Unless it's currently required.

Part of #656.
  • Loading branch information
madsmtm committed Dec 25, 2024
1 parent 321d4cb commit 74d3254
Show file tree
Hide file tree
Showing 105 changed files with 481 additions and 648 deletions.
8 changes: 4 additions & 4 deletions crates/block2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ workspace = true
default = ["std"]

# Currently not possible to turn off, put here for forwards compatibility.
std = ["alloc", "objc2/std"]
alloc = ["objc2/alloc"]
std = ["alloc"]
alloc = []

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

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

[dev-dependencies.objc2-foundation]
path = "../../framework-crates/objc2-foundation"
default-features = false
features = ["NSError"]
features = ["std", "NSError"]

[package.metadata.docs.rs]
default-target = "aarch64-apple-darwin"
Expand Down
7 changes: 4 additions & 3 deletions crates/block2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,13 @@
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg_hide))]
#![cfg_attr(docsrs, doc(cfg_hide(doc)))]

#[cfg(not(feature = "alloc"))]
compile_error!("The `alloc` feature currently must be enabled.");

extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

#[cfg(not(feature = "std"))]
compile_error!("The `std` feature currently must be enabled.");

#[cfg(all(
not(docsrs),
not(any(
Expand Down
4 changes: 2 additions & 2 deletions crates/block2/src/rc_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<F: ?Sized> RcBlock<F> {
/// # Example
///
/// ```
/// # use std::ffi::CStr;
/// # use core::ffi::CStr;
/// # use block2::{Block, ManualBlockEncoding, RcBlock};
/// # use objc2_foundation::NSError;
/// #
Expand All @@ -173,7 +173,7 @@ impl<F: ?Sized> RcBlock<F> {
/// let my_block = RcBlock::with_encoding::<_, _, _, MyBlockEncoding>(|_err: *mut NSError| {
/// 42i32
/// });
/// assert_eq!(my_block.call((std::ptr::null_mut(),)), 42);
/// assert_eq!(my_block.call((core::ptr::null_mut(),)), 42);
/// ```
#[inline]
pub fn with_encoding<'f, A, R, Closure, E>(closure: Closure) -> Self
Expand Down
4 changes: 2 additions & 2 deletions crates/block2/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ where
/// # Example
///
/// ```
/// # use std::ffi::CStr;
/// # use core::ffi::CStr;
/// # use block2::{Block, ManualBlockEncoding, StackBlock};
/// # use objc2_foundation::NSError;
/// #
Expand All @@ -224,7 +224,7 @@ where
/// let my_block = StackBlock::with_encoding::<MyBlockEncoding>(|_err: *mut NSError| {
/// 42i32
/// });
/// assert_eq!(my_block.call((std::ptr::null_mut(),)), 42);
/// assert_eq!(my_block.call((core::ptr::null_mut(),)), 42);
/// ```
#[inline]
pub fn with_encoding<E>(closure: Closure) -> Self
Expand Down
15 changes: 7 additions & 8 deletions crates/block2/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use core::ffi::CStr;
use core::marker::PhantomData;
use core::mem;
use core::ptr;
// TODO: use `core` when the MSRV is at least 1.64.
use std::ffi::CStr;

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

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

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

// Only `NoBlockEncoding` should be `IS_NONE`.
assert!(std::convert::identity(NoBlockEncoding::<(), ()>::IS_NONE));
assert!(std::convert::identity(
assert!(core::convert::identity(NoBlockEncoding::<(), ()>::IS_NONE));
assert!(core::convert::identity(
NoBlockEncoding::<(i32, f32), u8>::IS_NONE
));
assert!(std::convert::identity(
assert!(core::convert::identity(
NoBlockEncoding::<(*const u8,), *const c_char>::IS_NONE
));
}
Expand Down
1 change: 1 addition & 0 deletions crates/dispatch2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
crate.
- Added `Once`, a wrapper over `dispatch_once_f` which works similarly to
`std::sync::Once`.
- Added `#![no_std]` support

### Changed
- Moved to the `objc2` project.
Expand Down
10 changes: 4 additions & 6 deletions crates/dispatch2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ workspace = true

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

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

[features]
default = ["std"]

# Currently not possible to turn off, put here for forwards compatibility.
std = ["alloc", "bitflags/std", "block2?/std", "libc?/std", "objc2?/std"]
alloc = ["block2?/alloc", "objc2?/alloc"]
std = ["alloc", "bitflags/std"]
alloc = []
block2 = ["dep:block2"]
libc = ["dep:libc"]
objc2 = ["dep:objc2"]
Expand Down
2 changes: 0 additions & 2 deletions crates/header-translator/src/default_cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,5 @@ targets = [

[features]
default = ["std"]

# Currently not possible to turn off, put here for forwards compatibility.
std = ["alloc"]
alloc = []
31 changes: 21 additions & 10 deletions crates/header-translator/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ impl Library {
}
}

for (krate, (_, _, krate_features)) in &mut dependencies {
// std is currently required for objc2
if *krate == "objc2" {
krate_features.insert("std".into());
}
// alloc is currently required for these crates
if matches!(*krate, "block2" | "dispatch2" | "objc2-foundation") {
krate_features.insert("alloc".into());
}
}

dependencies
}

Expand Down Expand Up @@ -273,6 +284,10 @@ see that for related crates.", self.data.krate)?;
(false, false) => format!("../{krate}"),
};
let mut table = match *krate {
"dispatch2" => InlineTable::from_iter([
("path", Value::from(path)),
("version", Value::from("0.1.0")),
]),
"objc2" => InlineTable::from_iter([
("path", Value::from(path)),
("version", Value::from("0.5.2")),
Expand Down Expand Up @@ -327,19 +342,15 @@ see that for related crates.", self.data.krate)?;
.entry(krate)
.or_insert(Item::Value(Value::InlineTable(table)));

cargo_toml["features"]["std"]
.as_array_mut()
.unwrap()
.push(Value::from(format!(
"{krate}{}/std",
if *required { "" } else { "?" },
)));
if *krate != "bitflags" && *krate != "libc" {
cargo_toml["features"]["alloc"]
// Bitflags is more of an "internal" dependency, just like
// objc2-encode is mostly an internal dependency to objc2, so it's
// fine to special-case std for that here.
if *krate == "bitflags" {
cargo_toml["features"]["std"]
.as_array_mut()
.unwrap()
.push(Value::from(format!(
"{krate}{}/alloc",
"{krate}{}/std",
if *required { "" } else { "?" },
)));
}
Expand Down
4 changes: 1 addition & 3 deletions crates/objc2-encode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@
#[cfg(not(feature = "alloc"))]
compile_error!("the `alloc` feature currently must be enabled");

extern crate alloc;
#[cfg(any(feature = "std", doc))]
extern crate std;

#[cfg(any(feature = "alloc", test))]
extern crate alloc;

mod encoding;
mod encoding_box;
mod helper;
Expand Down
7 changes: 4 additions & 3 deletions crates/objc2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ workspace = true
default = ["std"]

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

# Enables `objc2::exception::throw` and `objc2::exception::catch`
exception = ["dep:objc2-exception-helper"]
Expand Down Expand Up @@ -125,11 +125,12 @@ objc2-exception-helper = { path = "../objc2-exception-helper", version = "0.1.0"
iai = { version = "0.1", git = "https://github.com/madsmtm/iai", branch = "callgrind" }
static_assertions = "1.1.0"
memoffset = "0.9.0"
block2 = { path = "../block2", default-features = false }
block2 = { path = "../block2" }
objc2-core-foundation = { path = "../../framework-crates/objc2-core-foundation", default-features = false, features = [
"CFCGTypes",
] }
objc2-foundation = { path = "../../framework-crates/objc2-foundation", default-features = false, features = [
"std",
"NSArray",
"NSDate",
"NSDictionary",
Expand Down
3 changes: 3 additions & 0 deletions crates/objc2/src/topics/about_generated/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* **BREAKING**: The feature flag guarding `SCSensitivityAnalysis` changed.
* **BREAKING**: `-[NSSavePanel beginSheetForDirectory:file:modalForWindow:modalDelegate:didEndSelector:contextInfo:]`
now takes an optional value as the file path.
* **BREAKING**: No longer automatically enable `std` and `alloc` features of
dependencies. If you want a certain framework crate to use `std` or `alloc`
features, you cannot rely on a higher-level crate to enable that for you.

### Deprecated
* Moved `MainThreadMarker` from `objc2-foundation` to `objc2`.
Expand Down
20 changes: 10 additions & 10 deletions crates/test-assembly/crates/test_define_class/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ publish = false
path = "lib.rs"

[dependencies]
objc2 = { path = "../../../objc2", optional = true }
objc2-foundation = { path = "../../../../framework-crates/objc2-foundation", optional = true }
objc2 = { path = "../../../objc2" }
objc2-foundation = { path = "../../../../framework-crates/objc2-foundation" }

[features]
default = ["all", "objc2", "objc2-foundation"]
default = ["all"]
# Runtime
gnustep-1-7 = ["objc2?/gnustep-1-7", "objc2-foundation?/gnustep-1-7"]
gnustep-1-8 = ["gnustep-1-7", "objc2?/gnustep-1-8", "objc2-foundation?/gnustep-1-8"]
gnustep-1-9 = ["gnustep-1-8", "objc2?/gnustep-1-9", "objc2-foundation?/gnustep-1-9"]
gnustep-2-0 = ["gnustep-1-9", "objc2?/gnustep-2-0", "objc2-foundation?/gnustep-2-0"]
gnustep-2-1 = ["gnustep-2-0", "objc2?/gnustep-2-1", "objc2-foundation?/gnustep-2-1"]
gnustep-1-7 = ["objc2/gnustep-1-7", "objc2-foundation/gnustep-1-7"]
gnustep-1-8 = ["gnustep-1-7", "objc2/gnustep-1-8", "objc2-foundation/gnustep-1-8"]
gnustep-1-9 = ["gnustep-1-8", "objc2/gnustep-1-9", "objc2-foundation/gnustep-1-9"]
gnustep-2-0 = ["gnustep-1-9", "objc2/gnustep-2-0", "objc2-foundation/gnustep-2-0"]
gnustep-2-1 = ["gnustep-2-0", "objc2/gnustep-2-1", "objc2-foundation/gnustep-2-1"]

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

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

[package.metadata.release]
release = false
4 changes: 2 additions & 2 deletions crates/test-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ gnustep-2-1 = ["gnustep-2-0", "block2/gnustep-2-1", "objc2/gnustep-2-1", "objc2-
trybuild = { version = "1.0.72", optional = true }
block2 = { path = "../block2" }
objc2 = { path = "../objc2" }
objc2-foundation = { path = "../../framework-crates/objc2-foundation" }
objc2-foundation = { path = "../../framework-crates/objc2-foundation", default-features = false, features = ["std"] }

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

[[bin]]
name = "test-ui"
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ unstable-objfw = ["block2/unstable-objfw", "objc2/unstable-objfw"]
[dependencies]
block2 = { path = "../block2" }
objc2 = { path = "../objc2" }
objc2-foundation = { path = "../../framework-crates/objc2-foundation" }
objc2-foundation = { path = "../../framework-crates/objc2-foundation", default-features = false, features = ["std"] }

[build-dependencies]
cc = "1.0"
Expand Down
12 changes: 5 additions & 7 deletions framework-crates/objc2-accessibility/Cargo.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions framework-crates/objc2-accounts/Cargo.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 74d3254

Please sign in to comment.