Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 views
in Technique[技术] by (71.8m points)

rust - How can I use a module from outside the src folder in a binary project, such as for integration tests or benchmarks?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...