From dd2e32cabd3b6717449a3baf046a095d37b934ee Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sat, 5 Aug 2017 10:24:13 -0700 Subject: [PATCH] no_std support Changes references from std:: to core:: where applicable, ensuring the crate builds in no_std environments. --- .travis.yml | 6 ++++++ Cargo.toml | 4 ++-- src/clear.rs | 4 ++-- src/clear_on_drop.rs | 22 +++++++++++----------- src/hide.rs | 6 +++--- src/lib.rs | 4 ++++ 6 files changed, 28 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index a88ac06..2c62af1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,12 @@ matrix: env: FEATURES=--features=nightly - rust: beta env: FEATURES=--features=nightly + include: + - rust: beta + script: cargo build --verbose --no-default-features --features=no_cc + script: cargo build --verbose --no-default-features --features=nightly + script: cargo build --verbose --release --no-default-features --features=no_cc + script: cargo build --verbose --release --no-default-features --features=nightly script: - cargo build --verbose $FEATURES - cargo test --verbose $FEATURES diff --git a/Cargo.toml b/Cargo.toml index f616b4f..ad6f514 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,10 +12,10 @@ license = "MIT OR Apache-2.0" build = "build.rs" [features] +default = ["std"] no_cc = [] nightly = ["no_cc"] +std = [] [build-dependencies] gcc = "0.3" - -[dependencies] diff --git a/src/clear.rs b/src/clear.rs index ab3292b..71ffa60 100644 --- a/src/clear.rs +++ b/src/clear.rs @@ -39,8 +39,8 @@ //! assert!(!as_bytes(&place).contains(&0x41)); //! ``` -use std::mem; -use std::ptr; +use core::mem; +use core::ptr; use hide::hide_mem_impl; diff --git a/src/clear_on_drop.rs b/src/clear_on_drop.rs index 87c068f..c43c49c 100644 --- a/src/clear_on_drop.rs +++ b/src/clear_on_drop.rs @@ -1,10 +1,10 @@ -use std::borrow::{Borrow, BorrowMut}; -use std::cmp::Ordering; -use std::fmt; -use std::hash::{Hash, Hasher}; -use std::mem; -use std::ops::{Deref, DerefMut}; -use std::ptr; +use core::borrow::{Borrow, BorrowMut}; +use core::cmp::Ordering; +use core::fmt; +use core::hash::{Hash, Hasher}; +use core::mem; +use core::ops::{Deref, DerefMut}; +use core::ptr; use clear::Clear; @@ -138,7 +138,7 @@ impl

Drop for ClearOnDrop

} } -// std::convert traits +// core::convert traits impl AsRef for ClearOnDrop

where P: DerefMut + AsRef, @@ -160,7 +160,7 @@ impl AsMut for ClearOnDrop

} } -// std::borrow traits +// core::borrow traits // The `T: Clear` bound avoids a conflict with the blanket impls // `impl Borrow for T` and `impl BorrowMut for T`, since @@ -188,7 +188,7 @@ impl BorrowMut for ClearOnDrop

} } -// std::hash traits +// core::hash traits impl

Hash for ClearOnDrop

where P: DerefMut + Hash, @@ -200,7 +200,7 @@ impl

Hash for ClearOnDrop

} } -// std::cmp traits +// core::cmp traits impl PartialEq> for ClearOnDrop

where P: DerefMut + PartialEq, diff --git a/src/hide.rs b/src/hide.rs index dcfe839..6489d2b 100644 --- a/src/hide.rs +++ b/src/hide.rs @@ -25,7 +25,7 @@ pub fn hide_ptr

(mut ptr: P) -> P { #[cfg(feature = "nightly")] pub use self::nightly::*; -#[cfg(not(feature = "no_cc"))] +#[cfg(all(not(feature = "no_cc"), feature = "std"))] pub use self::cc::*; #[cfg(all(feature = "no_cc", not(feature = "nightly")))] @@ -63,7 +63,7 @@ mod nightly { } // When a C compiler is available, a dummy C function can be used. -#[cfg(not(feature = "no_cc"))] +#[cfg(all(not(feature = "no_cc"), feature = "std"))] mod cc { use std::os::raw::c_void; @@ -83,7 +83,7 @@ mod cc { // and hope this is enough to confuse the optimizer. #[cfg(all(feature = "no_cc", not(feature = "nightly")))] mod fallback { - use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering}; + use core::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering}; #[inline] pub fn hide_mem_impl(ptr: *mut T) { diff --git a/src/lib.rs b/src/lib.rs index 62a6032..f36011d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(feature = "nightly", feature(asm))] #![cfg_attr(feature = "nightly", feature(i128_type))] #![cfg_attr(feature = "nightly", feature(specialization))] @@ -55,6 +56,9 @@ //! the `no_cc` feature, works on stable Rust, and does not need a C //! compiler. +#[cfg(feature = "std")] +extern crate core; + pub mod clear; mod clear_on_drop; mod clear_stack_on_return;