Skip to main content

Crate ekege_artifact

Crate ekege_artifact 

Source
Expand description

General-purpose build artifact generation crate, used in Ekege’s build script.

It provides a mechanism to generate and store arbitrary data during the build process (typically in a build.rs script) and then load that data at compile time in your main crate code.

§Usage

First, implement the Artifact trait for your custom types. Then use store_artifact in your build script to serialize them, and load_artifacts! in your main code to make the constants available.

§Examples

Given an artifact MyVec:

struct MyVec(Vec<u16>);

// Store `MyVec`'s data using tokens, as a build artifact
impl ToTokens for MyVec {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let items = &self.0;

        tokens.append_all(quote! { [#(#items),*] });
    }
}

// Get `MyVec`'s build artifact type, as tokens
impl Artifact for MyVec {
    fn generate_type(&self) -> impl ToTokens {
        let length = self.0.len();

        quote! { [u16; #length] }
    }
}

We can store it using store_artifact like so:

ekege_artifact::store_artifact(
    format_ident!("MY_VEC"),
    MyVec(vec![1, 2, 3]),
);

At this point, the stored artifact will look somewhat like:

const MY_VEC: [u16; 3] = [1, 2, 3];

Using load_artifacts! we can then use that artifact in our main crate code:

load_artifacts!();

println!("{}", MY_VEC[0]);

Macros§

artifact_file
Returns the filename used for storing artifact data.
format_ident
Formatting macro for constructing Idents.
load_artifacts
Includes all stored artifacts at compile time.
quote
The whole point.

Structs§

TokenStream
An abstract stream of tokens, or more concretely a sequence of token trees.

Traits§

Artifact
A type that can be serialized as a compile-time artifact.
ToTokens
Types that can be interpolated inside a quote! invocation.
TokenStreamExt
TokenStream extension trait with methods for appending tokens.

Functions§

store_artifact
Stores an artifact in the build output directory.