Skip to content

Commit 4105a15

Browse files
committed
docs(unstable): doc comments for Features and Feature
1 parent d0390c2 commit 4105a15

File tree

1 file changed

+57
-35
lines changed

1 file changed

+57
-35
lines changed

src/cargo/core/features.rs

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -358,20 +358,43 @@ enum Status {
358358
Removed,
359359
}
360360

361+
/// A listing of stable and unstable new syntax in Cargo.toml.
362+
///
363+
/// This generates definitions and impls for [`Features`] and [`Feature`]
364+
/// for each new syntax.
365+
///
366+
/// Note that all feature names in the macro invocation are valid Rust
367+
/// identifiers, but the `_` character is translated to `-` when specified in
368+
/// the `cargo-features` manifest entry in `Cargo.toml`.
369+
///
370+
/// See the [module-level documentation](self#new-cargotoml-syntax)
371+
/// for the process of adding a new syntax.
361372
macro_rules! features {
362373
(
363-
$(($stab:ident, $feature:ident, $version:expr, $docs:expr),)*
374+
$(
375+
$(#[$attr:meta])*
376+
($stab:ident, $feature:ident, $version:expr, $docs:expr),
377+
)*
364378
) => (
379+
/// Unstable feature context for querying if a new Cargo.toml syntax
380+
/// is allowed to use.
381+
///
382+
/// See the [module-level documentation](self#new-cargotoml-syntax) for the usage.
365383
#[derive(Default, Clone, Debug)]
366384
pub struct Features {
367385
$($feature: bool,)*
386+
/// The current activated features.
368387
activated: Vec<String>,
388+
/// Whether is allowed to use any unstable features.
369389
nightly_features_allowed: bool,
390+
/// Whether the source mainfest is from a local package.
370391
is_local: bool,
371392
}
372393

373394
impl Feature {
374395
$(
396+
$(#[$attr])*
397+
#[doc = concat!("\n\n\nSee <https://doc.rust-lang.org/nightly/cargo/", $docs, ">.")]
375398
pub fn $feature() -> &'static Feature {
376399
fn get(features: &Features) -> bool {
377400
stab!($stab) == Status::Stable || features.$feature
@@ -387,6 +410,7 @@ macro_rules! features {
387410
}
388411
)*
389412

413+
/// Whether this feature is allowed to use in the given [`Features`] context.
390414
fn is_enabled(&self, features: &Features) -> bool {
391415
(self.get)(features)
392416
}
@@ -421,96 +445,91 @@ macro_rules! stab {
421445
};
422446
}
423447

424-
// A listing of all features in Cargo.
425-
//
426448
// "look here"
427-
//
428-
// This is the macro that lists all stable and unstable features in Cargo.
429-
// You'll want to add to this macro whenever you add a feature to Cargo, also
430-
// following the directions above.
431-
//
432-
// Note that all feature names here are valid Rust identifiers, but the `_`
433-
// character is translated to `-` when specified in the `cargo-features`
434-
// manifest entry in `Cargo.toml`.
435449
features! {
436-
// A dummy feature that doesn't actually gate anything, but it's used in
437-
// testing to ensure that we can enable stable features.
450+
/// A dummy feature that doesn't actually gate anything, but it's used in
451+
/// testing to ensure that we can enable stable features.
438452
(stable, test_dummy_stable, "1.0", ""),
439453

440-
// A dummy feature that gates the usage of the `im-a-teapot` manifest
441-
// entry. This is basically just intended for tests.
454+
/// A dummy feature that gates the usage of the `im-a-teapot` manifest
455+
/// entry. This is basically just intended for tests.
442456
(unstable, test_dummy_unstable, "", "reference/unstable.html"),
443457

444-
// Downloading packages from alternative registry indexes.
458+
/// Downloading packages from alternative registry indexes.
445459
(stable, alternative_registries, "1.34", "reference/registries.html"),
446460

447-
// Using editions
461+
/// Using editions
448462
(stable, edition, "1.31", "reference/manifest.html#the-edition-field"),
449463

450-
// Renaming a package in the manifest via the `package` key
464+
/// Renaming a package in the manifest via the `package` key.
451465
(stable, rename_dependency, "1.31", "reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml"),
452466

453-
// Whether a lock file is published with this crate
467+
/// Whether a lock file is published with this crate.
454468
(removed, publish_lockfile, "1.37", "reference/unstable.html#publish-lockfile"),
455469

456-
// Overriding profiles for dependencies.
470+
/// Overriding profiles for dependencies.
457471
(stable, profile_overrides, "1.41", "reference/profiles.html#overrides"),
458472

459-
// "default-run" manifest option,
473+
/// "default-run" manifest option.
460474
(stable, default_run, "1.37", "reference/manifest.html#the-default-run-field"),
461475

462-
// Declarative build scripts.
476+
/// Declarative build scripts.
463477
(unstable, metabuild, "", "reference/unstable.html#metabuild"),
464478

465-
// Specifying the 'public' attribute on dependencies
479+
/// Specifying the 'public' attribute on dependencies.
466480
(unstable, public_dependency, "", "reference/unstable.html#public-dependency"),
467481

468-
// Allow to specify profiles other than 'dev', 'release', 'test', etc.
482+
/// Allow to specify profiles other than 'dev', 'release', 'test', etc.
469483
(stable, named_profiles, "1.57", "reference/profiles.html#custom-profiles"),
470484

471-
// Opt-in new-resolver behavior.
485+
/// Opt-in new-resolver behavior.
472486
(stable, resolver, "1.51", "reference/resolver.html#resolver-versions"),
473487

474-
// Allow to specify whether binaries should be stripped.
488+
/// Allow to specify whether binaries should be stripped.
475489
(stable, strip, "1.58", "reference/profiles.html#strip-option"),
476490

477-
// Specifying a minimal 'rust-version' attribute for crates
491+
/// Specifying a minimal 'rust-version' attribute for crates.
478492
(stable, rust_version, "1.56", "reference/manifest.html#the-rust-version-field"),
479493

480-
// Support for 2021 edition.
494+
/// Support for 2021 edition.
481495
(stable, edition2021, "1.56", "reference/manifest.html#the-edition-field"),
482496

483-
// Allow to specify per-package targets (compile kinds)
497+
/// Allow to specify per-package targets (compile kinds).
484498
(unstable, per_package_target, "", "reference/unstable.html#per-package-target"),
485499

486-
// Allow to specify which codegen backend should be used.
500+
/// Allow to specify which codegen backend should be used.
487501
(unstable, codegen_backend, "", "reference/unstable.html#codegen-backend"),
488502

489-
// Allow specifying different binary name apart from the crate name
503+
/// Allow specifying different binary name apart from the crate name.
490504
(unstable, different_binary_name, "", "reference/unstable.html#different-binary-name"),
491505

492-
// Allow specifying rustflags directly in a profile
506+
/// Allow specifying rustflags directly in a profile.
493507
(unstable, profile_rustflags, "", "reference/unstable.html#profile-rustflags-option"),
494508

495-
// Allow specifying rustflags directly in a profile
509+
/// Allow workspace members to inherit fields and dependencies from a workspace.
496510
(stable, workspace_inheritance, "1.64", "reference/unstable.html#workspace-inheritance"),
497511

498-
// Support for 2024 edition.
512+
/// Support for 2024 edition.
499513
(unstable, edition2024, "", "reference/unstable.html#edition-2024"),
500514

501-
// Allow setting trim-paths in a profile to control the sanitisation of file paths in build outputs.
515+
/// Allow setting trim-paths in a profile to control the sanitisation of file paths in build outputs.
502516
(unstable, trim_paths, "", "reference/unstable.html#profile-trim-paths-option"),
503517
}
504518

519+
/// Status and metadata for a single unstable feature.
505520
pub struct Feature {
521+
/// Feature name. This is valid Rust identifer so no dash only underscore.
506522
name: &'static str,
507523
stability: Status,
524+
/// Version that this feature was stabilized or removed.
508525
version: &'static str,
526+
/// Link to the unstable documentation.
509527
docs: &'static str,
510528
get: fn(&Features) -> bool,
511529
}
512530

513531
impl Features {
532+
/// Creates a new unstable features context.
514533
pub fn new(
515534
features: &[String],
516535
config: &Config,
@@ -617,10 +636,12 @@ impl Features {
617636
Ok(())
618637
}
619638

639+
/// Gets the current activated features.
620640
pub fn activated(&self) -> &[String] {
621641
&self.activated
622642
}
623643

644+
/// Checks if the given feature is enabled.
624645
pub fn require(&self, feature: &Feature) -> CargoResult<()> {
625646
if feature.is_enabled(self) {
626647
return Ok(());
@@ -666,6 +687,7 @@ impl Features {
666687
bail!("{}", msg);
667688
}
668689

690+
/// Whether the given feature is allowed to use in this context.
669691
pub fn is_enabled(&self, feature: &Feature) -> bool {
670692
feature.is_enabled(self)
671693
}

0 commit comments

Comments
 (0)