## Package

# Struct Package

Source

    pub struct Package {Show 18 fields
        name: String,
        vers: String,
        deps: Vec<Dependency>,
        files: Vec<PackageFile>,
        yanked: bool,
        features: BTreeMap<String, Vec<String>>,
        local: bool,
        alternative: bool,
        invalid_index_line: bool,
        index_line: Option<String>,
        edition: Option<String>,
        resolver: Option<String>,
        proc_macro: bool,
        links: Option<String>,
        rust_version: Option<String>,
        cargo_features: Vec<String>,
        pubtime: Option<String>,
        v: Option<u32>,
    }

Expand description

Published package builder for `TestRegistry`

This uses “source replacement” using an automatically generated `.cargo/config` file to ensure that dependencies will use these packages instead of contacting crates.io. See `source-replacement.md` for more details on how source replacement works.

Call `Package::publish` to finalize and create the package.

If no files are specified, an empty `lib.rs` file is automatically created.

The `Cargo.toml` file is automatically generated based on the methods called on `Package` (for example, calling `Package::dep()` will add to the `[dependencies]` automatically). You may also specify a `Cargo.toml` file to override the generated one.

This supports different registry types:

- Regular source replacement that replaces `crates.io` (the default).
- A “local registry” which is a subset for vendoring (see `Package::local`).
- An “alternative registry” which requires specifying the registry name (see `Package::alternative`).

This does not support “directory sources”. See `directory.rs` for `VendorPackage` which implements directory sources.

## Fields§

§`name: String`§`vers: String`§`deps: Vec<Dependency>`§`files: Vec<PackageFile>`§`yanked: bool`§`features: BTreeMap<String, Vec<String>>`§`local: bool`§`alternative: bool`§`invalid_index_line: bool`§`index_line: Option<String>`§`edition: Option<String>`§`resolver: Option<String>`§`proc_macro: bool`§`links: Option<String>`§`rust_version: Option<String>`§`cargo_features: Vec<String>`§`pubtime: Option<String>`§`v: Option<u32>`

## Implementations§

Source§

### impl Package

Source

#### pub fn new(name: &str, vers: &str) -> Package

Creates a new package builder. Call `publish()` to finalize and build the package.

Source

#### pub fn local(&mut self, local: bool) -> &mut Package

Call with `true` to publish in a “local registry”.

See `source-replacement.html#local-registry-sources` for more details on local registries. See `local_registry.rs` for the tests that use this.

Source

#### pub fn alternative(&mut self, alternative: bool) -> &mut Package

Call with `true` to publish in an “alternative registry”.

The name of the alternative registry is called “alternative”.

See `src/doc/src/reference/registries.md` for more details on alternative registries. See `alt_registry.rs` for the tests that use this.

Requires: `alt_init`

Source

#### pub fn file(&mut self, name: &str, contents: &str) -> &mut Package

Adds a file to the package.

Source

#### pub fn file_with_mode( &mut self, path: &str, mode: u32, contents: &str, ) -> &mut Package

Adds a file with a specific Unix mode.

Source

#### pub fn symlink(&mut self, dst: &str, src: &str) -> &mut Package

Adds a symlink to a path to the package.

Source

#### pub fn extra_file(&mut self, path: &str, contents: &str) -> &mut Package

Adds an “extra” file that is not rooted within the package.

Normal files are automatically placed within a directory named `$PACKAGE-$VERSION`. This allows you to override that behavior, typically for testing invalid behavior.

Source

#### pub fn dep(&mut self, name: &str, vers: &str) -> &mut Package

Adds a normal dependency. Example:

    [dependencies]
    foo = {version = "1.0"}

Source

#### pub fn feature_dep( &mut self, name: &str, vers: &str, features: &[&str], ) -> &mut Package

Adds a dependency with the given feature. Example:

    [dependencies]
    foo = {version = "1.0", "features": ["feat1", "feat2"]}

Source

#### pub fn target_dep( &mut self, name: &str, vers: &str, target: &str, ) -> &mut Package

Adds a platform-specific dependency. Example:

    [target.'cfg(windows)'.dependencies]
    foo = {version = "1.0"}

Source

#### pub fn registry_dep(&mut self, name: &str, vers: &str) -> &mut Package

Adds a dependency to the alternative registry.

Source

#### pub fn dev_dep(&mut self, name: &str, vers: &str) -> &mut Package

Adds a dev-dependency. Example:

    [dev-dependencies]
    foo = {version = "1.0"}

Source

#### pub fn build_dep(&mut self, name: &str, vers: &str) -> &mut Package

Adds a build-dependency. Example:

    [build-dependencies]
    foo = {version = "1.0"}

Source

#### pub fn add_dep(&mut self, dep: &Dependency) -> &mut Package

Source

#### pub fn yanked(&mut self, yanked: bool) -> &mut Package

Specifies whether or not the package is “yanked”.

Source

#### pub fn edition(&mut self, edition: &str) -> &mut Package

Specifies `package.edition`

Source

#### pub fn resolver(&mut self, resolver: &str) -> &mut Package

Specifies `package.resolver`

Source

#### pub fn proc_macro(&mut self, proc_macro: bool) -> &mut Package

Specifies whether or not this is a proc macro.

Source

#### pub fn feature(&mut self, name: &str, deps: &[&str]) -> &mut Package

Adds an entry in the `[features]` section.

Source

#### pub fn rust_version(&mut self, rust_version: &str) -> &mut Package

Specify a minimal Rust version.

Source

#### pub fn invalid_index_line(&mut self, invalid: bool) -> &mut Package

Causes the JSON line emitted in the index to be invalid, presumably causing Cargo to skip over this version.

Source

#### pub fn index_line(&mut self, line: &str) -> &mut Package

Override the auto-generated index line

This can give more control over error cases than `Package::invalid_index_line`

Source

#### pub fn links(&mut self, links: &str) -> &mut Package

Source

#### pub fn cargo_feature(&mut self, feature: &str) -> &mut Package

Source

#### pub fn pubtime(&mut self, time: &str) -> &mut Package

The publish time for the package in ISO8601 with UTC timezone (e.g. 2025-11-12T19:30:12Z)

Source

#### pub fn schema_version(&mut self, version: u32) -> &mut Package

Sets the index schema version for this package.

See `cargo::sources::registry::IndexPackage` for more information.

Source

#### pub fn publish(&self) -> String

Creates the package and place it in the registry.

This does not actually use Cargo’s publishing system, but instead manually creates the entry in the registry on the filesystem.

Returns the checksum for the package.

Source

#### fn make_archive(&self)

Source

#### fn append_manifest<W: Write>(&self, ar: &mut Builder<W>)

Source

#### fn append<W: Write>( &self, ar: &mut Builder<W>, file: &str, mode: u32, contents: &EntryData, )

Source

#### fn append_raw<W: Write>( &self, ar: &mut Builder<W>, path: &str, mode: u32, contents: &EntryData, )

Source

#### pub fn archive_dst(&self) -> PathBuf

Returns the path to the compressed package file.

## Auto Trait Implementations§

§

### impl Freeze for Package

§

### impl RefUnwindSafe for Package

§

### impl Send for Package

§

### impl Sync for Package

§

### impl Unpin for Package

§

### impl UnwindSafe for Package

## Blanket Implementations§

§

### impl<T> Any for T
where T: 'static + ?Sized,

§

#### fn type_id(&self) -> TypeId

Gets the `TypeId` of `self`. Read more
§

### impl<T> Borrow<T> for T
where T: ?Sized,

§

#### fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

### impl<T> BorrowMut<T> for T
where T: ?Sized,

§

#### fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

### impl<T> From<T> for T

§

#### fn from(t: T) -> T

Returns the argument unchanged.

§

### impl<T> Instrument for T

§

#### fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [`Span`], returning an `Instrumented` wrapper. Read more
§

#### fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current `Span`, returning an `Instrumented` wrapper. Read more
§

### impl<T, U> Into<U> for T
where U: From<T>,

§

#### fn into(self) -> U

Calls `U::from(self)`.

That is, this conversion is whatever the implementation of `From<T> for U` chooses to do.

§

### impl<T> IntoEither for T

§

#### fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts `self` into a `Left` variant of `Either<Self, Self>` if `into_left` is `true`. Converts `self` into a `Right` variant of `Either<Self, Self>` otherwise. Read more
§

#### fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts `self` into a `Left` variant of `Either<Self, Self>` if `into_left(&self)` returns `true`. Converts `self` into a `Right` variant of `Either<Self, Self>` otherwise. Read more
§

### impl<T> Pointable for T

§

#### const ALIGN: usize

The alignment of pointer.
§

#### type Init = T

The type for initializers.
§

#### unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

#### unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

#### unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

#### unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

### impl<T> Same for T

§

#### type Output = T

Should always be `Self`
§

### impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

#### type Error = Infallible

The type returned in the event of a conversion error.
§

#### fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

### impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

#### type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

#### fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

### impl<T> WithSubscriber for T

§

#### fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided `Subscriber` to this type, returning a [`WithDispatch`] wrapper. Read more
§

#### fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default `Subscriber` to this type, returning a [`WithDispatch`] wrapper. Read more

## Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain `repr(...)` attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 304 bytes