Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test crates with #![no_std]?

Tags:

testing

rust

I'm writing a runtime for a programming language implementation in Rust. I'm planning on linking in this runtime with the compiled code I generate, so to keep the binary small I don't want to rely on std.

When I try to cargo test my runtime, I get errors saying saying that std::slice::AsSlice can't be found, which I found is because some of the test harness requires std library code.

How do I go about testing this code? Is there a way to conditionally include the #![no_std] pragma, i.e. still include the std library while testing? I've also tried creating a separate test crate with the std library included, extern crateing the runtime crate into it and running my tests there, but that has introduced a whole new set of issues.

like image 712
Zach Smith Avatar asked Jan 28 '15 06:01

Zach Smith


2 Answers

You can cfg_attr to conditionally set no_std.

#![cfg_attr(not(test), no_std)]
like image 196
Matthew Meyer Avatar answered Sep 26 '22 23:09

Matthew Meyer


#[cfg(test)]
#[macro_use]
extern crate std;

(The #[macro_use] part is optional.)

like image 32
Chris Morgan Avatar answered Sep 23 '22 23:09

Chris Morgan