@@ -358,20 +358,43 @@ enum Status {
358
358
Removed ,
359
359
}
360
360
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.
361
372
macro_rules! features {
362
373
(
363
- $( ( $stab: ident, $feature: ident, $version: expr, $docs: expr) , ) *
374
+ $(
375
+ $( #[ $attr: meta] ) *
376
+ ( $stab: ident, $feature: ident, $version: expr, $docs: expr) ,
377
+ ) *
364
378
) => (
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.
365
383
#[ derive( Default , Clone , Debug ) ]
366
384
pub struct Features {
367
385
$( $feature: bool , ) *
386
+ /// The current activated features.
368
387
activated: Vec <String >,
388
+ /// Whether is allowed to use any unstable features.
369
389
nightly_features_allowed: bool ,
390
+ /// Whether the source mainfest is from a local package.
370
391
is_local: bool ,
371
392
}
372
393
373
394
impl Feature {
374
395
$(
396
+ $( #[ $attr] ) *
397
+ #[ doc = concat!( "\n \n \n See <https://doc.rust-lang.org/nightly/cargo/" , $docs, ">." ) ]
375
398
pub fn $feature( ) -> & ' static Feature {
376
399
fn get( features: & Features ) -> bool {
377
400
stab!( $stab) == Status :: Stable || features. $feature
@@ -387,6 +410,7 @@ macro_rules! features {
387
410
}
388
411
) *
389
412
413
+ /// Whether this feature is allowed to use in the given [`Features`] context.
390
414
fn is_enabled( & self , features: & Features ) -> bool {
391
415
( self . get) ( features)
392
416
}
@@ -421,96 +445,91 @@ macro_rules! stab {
421
445
} ;
422
446
}
423
447
424
- // A listing of all features in Cargo.
425
- //
426
448
// "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`.
435
449
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.
438
452
( stable, test_dummy_stable, "1.0" , "" ) ,
439
453
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.
442
456
( unstable, test_dummy_unstable, "" , "reference/unstable.html" ) ,
443
457
444
- // Downloading packages from alternative registry indexes.
458
+ /// Downloading packages from alternative registry indexes.
445
459
( stable, alternative_registries, "1.34" , "reference/registries.html" ) ,
446
460
447
- // Using editions
461
+ /// Using editions
448
462
( stable, edition, "1.31" , "reference/manifest.html#the-edition-field" ) ,
449
463
450
- // Renaming a package in the manifest via the `package` key
464
+ /// Renaming a package in the manifest via the `package` key.
451
465
( stable, rename_dependency, "1.31" , "reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml" ) ,
452
466
453
- // Whether a lock file is published with this crate
467
+ /// Whether a lock file is published with this crate.
454
468
( removed, publish_lockfile, "1.37" , "reference/unstable.html#publish-lockfile" ) ,
455
469
456
- // Overriding profiles for dependencies.
470
+ /// Overriding profiles for dependencies.
457
471
( stable, profile_overrides, "1.41" , "reference/profiles.html#overrides" ) ,
458
472
459
- // "default-run" manifest option,
473
+ /// "default-run" manifest option.
460
474
( stable, default_run, "1.37" , "reference/manifest.html#the-default-run-field" ) ,
461
475
462
- // Declarative build scripts.
476
+ /// Declarative build scripts.
463
477
( unstable, metabuild, "" , "reference/unstable.html#metabuild" ) ,
464
478
465
- // Specifying the 'public' attribute on dependencies
479
+ /// Specifying the 'public' attribute on dependencies.
466
480
( unstable, public_dependency, "" , "reference/unstable.html#public-dependency" ) ,
467
481
468
- // Allow to specify profiles other than 'dev', 'release', 'test', etc.
482
+ /// Allow to specify profiles other than 'dev', 'release', 'test', etc.
469
483
( stable, named_profiles, "1.57" , "reference/profiles.html#custom-profiles" ) ,
470
484
471
- // Opt-in new-resolver behavior.
485
+ /// Opt-in new-resolver behavior.
472
486
( stable, resolver, "1.51" , "reference/resolver.html#resolver-versions" ) ,
473
487
474
- // Allow to specify whether binaries should be stripped.
488
+ /// Allow to specify whether binaries should be stripped.
475
489
( stable, strip, "1.58" , "reference/profiles.html#strip-option" ) ,
476
490
477
- // Specifying a minimal 'rust-version' attribute for crates
491
+ /// Specifying a minimal 'rust-version' attribute for crates.
478
492
( stable, rust_version, "1.56" , "reference/manifest.html#the-rust-version-field" ) ,
479
493
480
- // Support for 2021 edition.
494
+ /// Support for 2021 edition.
481
495
( stable, edition2021, "1.56" , "reference/manifest.html#the-edition-field" ) ,
482
496
483
- // Allow to specify per-package targets (compile kinds)
497
+ /// Allow to specify per-package targets (compile kinds).
484
498
( unstable, per_package_target, "" , "reference/unstable.html#per-package-target" ) ,
485
499
486
- // Allow to specify which codegen backend should be used.
500
+ /// Allow to specify which codegen backend should be used.
487
501
( unstable, codegen_backend, "" , "reference/unstable.html#codegen-backend" ) ,
488
502
489
- // Allow specifying different binary name apart from the crate name
503
+ /// Allow specifying different binary name apart from the crate name.
490
504
( unstable, different_binary_name, "" , "reference/unstable.html#different-binary-name" ) ,
491
505
492
- // Allow specifying rustflags directly in a profile
506
+ /// Allow specifying rustflags directly in a profile.
493
507
( unstable, profile_rustflags, "" , "reference/unstable.html#profile-rustflags-option" ) ,
494
508
495
- // Allow specifying rustflags directly in a profile
509
+ /// Allow workspace members to inherit fields and dependencies from a workspace.
496
510
( stable, workspace_inheritance, "1.64" , "reference/unstable.html#workspace-inheritance" ) ,
497
511
498
- // Support for 2024 edition.
512
+ / // Support for 2024 edition.
499
513
( unstable, edition2024, "" , "reference/unstable.html#edition-2024" ) ,
500
514
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.
502
516
( unstable, trim_paths, "" , "reference/unstable.html#profile-trim-paths-option" ) ,
503
517
}
504
518
519
+ /// Status and metadata for a single unstable feature.
505
520
pub struct Feature {
521
+ /// Feature name. This is valid Rust identifer so no dash only underscore.
506
522
name : & ' static str ,
507
523
stability : Status ,
524
+ /// Version that this feature was stabilized or removed.
508
525
version : & ' static str ,
526
+ /// Link to the unstable documentation.
509
527
docs : & ' static str ,
510
528
get : fn ( & Features ) -> bool ,
511
529
}
512
530
513
531
impl Features {
532
+ /// Creates a new unstable features context.
514
533
pub fn new (
515
534
features : & [ String ] ,
516
535
config : & Config ,
@@ -617,10 +636,12 @@ impl Features {
617
636
Ok ( ( ) )
618
637
}
619
638
639
+ /// Gets the current activated features.
620
640
pub fn activated ( & self ) -> & [ String ] {
621
641
& self . activated
622
642
}
623
643
644
+ /// Checks if the given feature is enabled.
624
645
pub fn require ( & self , feature : & Feature ) -> CargoResult < ( ) > {
625
646
if feature. is_enabled ( self ) {
626
647
return Ok ( ( ) ) ;
@@ -666,6 +687,7 @@ impl Features {
666
687
bail ! ( "{}" , msg) ;
667
688
}
668
689
690
+ /// Whether the given feature is allowed to use in this context.
669
691
pub fn is_enabled ( & self , feature : & Feature ) -> bool {
670
692
feature. is_enabled ( self )
671
693
}
0 commit comments