pub struct Metadata(_);
Expand description

The Metadata is a hash used to make unique file names for each unit in a build. It is also use for symbol mangling.

For example:

  • A project may depend on crate A and crate B, so the package name must be in the file name.
  • Similarly a project may depend on two versions of A, so the version must be in the file name.

In general this must include all things that need to be distinguished in different parts of the same build. This is absolutely required or we override things before we get chance to use them.

It is also used for symbol mangling, because if you have two versions of the same crate linked together, their symbols need to be differentiated.

We use a hash because it is an easy way to guarantee that all the inputs can be converted to a valid path.

This also acts as the main layer of caching provided by Cargo. For example, we want to cache cargo build and cargo doc separately, so that running one does not invalidate the artifacts for the other. We do this by including CompileMode in the hash, thus the artifacts go in different folders and do not override each other. If we don’t add something that we should have, for this reason, we get the correct output but rebuild more than is needed.

Some things that need to be tracked to ensure the correct output should definitely not go in the Metadata. For example, the modification time of a file, should be tracked to make a rebuild when the file changes. However, it would be wasteful to include in the Metadata. The old artifacts are never going to be needed again. We can save space by just overwriting them. If we add something that we should not have, for this reason, we get the correct output but take more space than needed. This makes not including something in Metadata a form of cache invalidation.

You should also avoid anything that would interfere with reproducible builds. For example, any absolute path should be avoided. This is one reason that RUSTFLAGS is not in Metadata, because it often has absolute paths (like --remap-path-prefix which is fundamentally used for reproducible builds and has absolute paths in it). Also, in some cases the mangled symbols need to be stable between different builds with different settings. For example, profile-guided optimizations need to swap RUSTFLAGS between runs, but needs to keep the same symbol names.

Note that the Fingerprint is in charge of tracking everything needed to determine if a rebuild is needed.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.