Skip to main content

impl_dependency

Macro impl_dependency 

Source
macro_rules! impl_dependency {
    ($type:ty {
        name: $name:ident,
        name_range: $name_range:ident,
        version: $version:ident,
        version_range: $version_range:ident $(,)?
    }) => { ... };
    ($type:ty {
        name: $name:ident,
        name_range: $name_range:ident,
        version: $version:ident,
        version_range: $version_range:ident,
        source: $source:ident $(,)?
    }) => { ... };
    ($type:ty {
        name: $name:ident,
        name_range: $name_range:ident,
        version: $version:ident,
        version_range: $version_range:ident,
        source: $source:ident,
        version_literal: $version_literal:ident $(,)?
    }) => { ... };
}
Expand description

Implement the Dependency trait for a struct.

§Arguments

  • $type - The struct type name
  • name - Field name for the dependency name (PackageName)
  • name_range - Field name for the name range (Range)
  • version - Field name for version requirement (Option<VersionReq>)
  • version_range - Field name for version range (Option<Range>)
  • source - Optional: field name for dependency source (DependencySource), cloned (defaults to the constant DependencySource::Registry when omitted). A field name rather than an arbitrary expression: a $source:expr fragment can express the same thing as a closure (impl_version!’s status/prerelease arguments use exactly that pattern), but a closure returning a borrow out of self (as source()’s caller-side equivalent would need for, e.g., a non-cloning accessor) hits a real lifetime footgun — the closure’s return type can’t name a lifetime tied to its &self parameter without HRTB ceremony the call sites here shouldn’t have to write. A bare field identifier, substituted into the generated method’s own self.$field, sidesteps that entirely and keeps every call site a one-line field name — the same reasoning that makes name, name_range, version and version_range above plain idents rather than expressions.
  • version_literal - Optional, requires source to also be given (fields are matched in this exact order — version_literal alone, or before source, matches no arm): field name for Dependency::version_literal (Option<String>), read via .as_deref() (defaults to the trait’s None when omitted)

§Examples

ⓘ
use deps_core::{impl_dependency, PackageName, VersionReq};

pub struct MyDependency {
    pub name: PackageName,
    pub name_range: Range,
    pub version_req: Option<VersionReq>,
    pub version_range: Option<Range>,
}

impl_dependency!(MyDependency {
    name: name,
    name_range: name_range,
    version: version_req,
    version_range: version_range,
});