Skip to content

Commit

Permalink
no_std support (closes #11)
Browse files Browse the repository at this point in the history
Changes references from std:: to core:: where applicable, ensuring the crate
builds in no_std environments.
  • Loading branch information
tarcieri committed Aug 5, 2017
1 parent 029223c commit 4341586
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 28 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ rust:
- nightly
env:
- FEATURES=
- FEATURES=--features=no_cc
- FEATURES=--features=cc
- FEATURES=--features=nightly
matrix:
exclude:
Expand All @@ -14,8 +14,8 @@ matrix:
- rust: beta
env: FEATURES=--features=nightly
script:
- cargo build --verbose $FEATURES
- cargo test --verbose $FEATURES
- cargo build --verbose --release $FEATURES
- cargo test --verbose --release $FEATURES
- '[ "$TRAVIS_RUST_VERSION" != "nightly" ] || cargo bench --verbose $FEATURES'
- cargo build --verbose --no-default-features $FEATURES
- cargo test --verbose --no-default-features $FEATURES
- cargo build --verbose --release --no-default-features $FEATURES
- cargo test --verbose --release --no-default-features $FEATURES
- '[ "$TRAVIS_RUST_VERSION" != "nightly" ] || cargo bench --verbose --no-default-features $FEATURES'
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ license = "MIT OR Apache-2.0"
build = "build.rs"

[features]
no_cc = []
nightly = ["no_cc"]
default = ["cc"]
cc = ["libc"]
nightly = []

[build-dependencies]
gcc = "0.3"

[dependencies]
[dependencies.libc]
version = "*"
optional = true
4 changes: 2 additions & 2 deletions src/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
22 changes: 11 additions & 11 deletions src/clear_on_drop.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -138,7 +138,7 @@ impl<P> Drop for ClearOnDrop<P>
}
}

// std::convert traits
// core::convert traits

impl<P, T: ?Sized> AsRef<T> for ClearOnDrop<P>
where P: DerefMut + AsRef<T>,
Expand All @@ -160,7 +160,7 @@ impl<P, T: ?Sized> AsMut<T> for ClearOnDrop<P>
}
}

// std::borrow traits
// core::borrow traits

// The `T: Clear` bound avoids a conflict with the blanket impls
// `impl<T> Borrow<T> for T` and `impl<T> BorrowMut<T> for T`, since
Expand Down Expand Up @@ -188,7 +188,7 @@ impl<P, T: ?Sized> BorrowMut<T> for ClearOnDrop<P>
}
}

// std::hash traits
// core::hash traits

impl<P> Hash for ClearOnDrop<P>
where P: DerefMut + Hash,
Expand All @@ -200,7 +200,7 @@ impl<P> Hash for ClearOnDrop<P>
}
}

// std::cmp traits
// core::cmp traits

impl<P, Q> PartialEq<ClearOnDrop<Q>> for ClearOnDrop<P>
where P: DerefMut + PartialEq<Q>,
Expand Down
14 changes: 8 additions & 6 deletions src/hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub fn hide_ptr<P>(mut ptr: P) -> P {
#[cfg(feature = "nightly")]
pub use self::nightly::*;

#[cfg(not(feature = "no_cc"))]
#[cfg(feature = "cc")]
pub use self::cc::*;

#[cfg(all(feature = "no_cc", not(feature = "nightly")))]
#[cfg(not(any(feature = "cc", feature = "nightly")))]
pub use self::fallback::*;

// On nightly, inline assembly can be used.
Expand Down Expand Up @@ -63,9 +63,11 @@ mod nightly {
}

// When a C compiler is available, a dummy C function can be used.
#[cfg(not(feature = "no_cc"))]
#[cfg(feature = "cc")]
mod cc {
use std::os::raw::c_void;
extern crate libc;

use self::libc::c_void;

extern "C" {
fn clear_on_drop_hide(ptr: *mut c_void) -> *mut c_void;
Expand All @@ -81,9 +83,9 @@ mod cc {

// When neither is available, pretend the pointer is sent to a thread,
// and hope this is enough to confuse the optimizer.
#[cfg(all(feature = "no_cc", not(feature = "nightly")))]
#[cfg(not(any(feature = "cc", 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<T: ?Sized>(ptr: *mut T) {
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(test), no_std)]
#![cfg_attr(feature = "nightly", feature(asm))]
#![cfg_attr(feature = "nightly", feature(i128_type))]
#![cfg_attr(feature = "nightly", feature(specialization))]
Expand Down Expand Up @@ -55,6 +56,9 @@
//! the `no_cc` feature, works on stable Rust, and does not need a C
//! compiler.
#[cfg(test)]
extern crate core;

pub mod clear;
mod clear_on_drop;
mod clear_stack_on_return;
Expand Down

0 comments on commit 4341586

Please sign in to comment.