35

My project's path structure is as follows:

demo
├── benches
│   └── crypto_bench.rs
├── src
│   ├── main.rs
│   └── crypto.rs
├── Cargo.lock
└── Cargo.toml

crypto.rs contains a struct Crypto with implementation. crypto.rs is referred to from main.rs using mod crypto;

How can I use crypto.rs from crypto_bench.rs inside the benches folder?

I have tried all kinds of variations of extern crate, mod, super and use. All examples I could find online are for library projects with a lib.rs and those "imports" don't work when using a project with a main.rs file.

1 Answer 1

40

Here's a literal answer, but don't actually use this!

#![feature(test)]
extern crate test;

#[path = "../src/foo.rs"] // Here
mod foo;

#[bench]
fn bencher(_: &mut test::Bencher) {
    println!("{:?}", foo::Thang);
}

In fact, it's very likely that this won't work because your code in foo.rs needs supporting code from other files that won't be included.


Instead of doing this, just create a library. You have the pure definition of a library - a piece of code that wants to be used in two different executables. You don't have to give up having an executable or even create separate directories (see Rust package with both a library and a binary?), but creating reusable code is a key component of making good code.

Your end state would look something like:

demo
├── Cargo.lock
├── Cargo.toml
├── benches
│   └── crypto_bench.rs
├── benchmarks
└── src
    ├── bin
    │   └── main.rs
    ├── crypto.rs
    └── lib.rs

Move the reusable code to a library:

src/lib.rs

pub mod crypto;

src/crypto.rs

pub struct Crypto;
impl Crypto {
    pub fn secret() {}
}

Then import your library from the benchmark and the binary:

benches/crypto_bench.rs

#![feature(test)]

extern crate test;

use demo::crypto::Crypto;
use test::Bencher;

#[bench]
fn speedy(b: &mut Bencher) {
    b.iter(|| Crypto::secret());
}

src/bin/main.rs

use demo::crypto::Crypto;

fn main() {
    Crypto::secret();
    eprintln!("Did the secret thing!");
}

You can then run it in different ways:

$ cargo build
   Compiling demo v0.1.0 (/private/tmp/example)
    Finished dev [unoptimized + debuginfo] target(s) in 0.51s

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/main`
Did the secret thing!

$ cargo +nightly bench
   Compiling demo v0.1.0 (/private/tmp/example)
    Finished release [optimized] target(s) in 0.70s
     Running target/release/deps/my_benchmark-5c9c5716763252a0

running 1 test
test speedy ... bench:           1 ns/iter (+/- 0)

test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured; 0 filtered out

See also:

Sign up to request clarification or add additional context in comments.

1 Comment

To add onto this answer, if your library crate-type is exclusively a "cdylib", you'll get a "can't find create for demo" compilation error - see github.com/rust-lang/cargo/issues/6659. To get integration tests / benchmarks working, you'll want add "(r)lib" to the list of crate-type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.