Skip to content

Commit 34c7cd1

Browse files
committed
no_std support (closes #11)
Changes references from std:: to core:: where applicable, ensuring the crate builds in no_std environments.
1 parent 029223c commit 34c7cd1

File tree

6 files changed

+35
-25
lines changed

6 files changed

+35
-25
lines changed

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ rust:
44
- beta
55
- nightly
66
env:
7-
- FEATURES=
8-
- FEATURES=--features=no_cc
9-
- FEATURES=--features=nightly
7+
- FEATURES=--no-default-features
8+
- FEATURES=--no-default-features --features=cc
9+
- FEATURES=--no-default-features --features=nightly
1010
matrix:
1111
exclude:
1212
- rust: stable
1313
env: FEATURES=--features=nightly
1414
- rust: beta
1515
env: FEATURES=--features=nightly
16+
1617
script:
1718
- cargo build --verbose $FEATURES
1819
- cargo test --verbose $FEATURES

Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ license = "MIT OR Apache-2.0"
1212
build = "build.rs"
1313

1414
[features]
15-
no_cc = []
16-
nightly = ["no_cc"]
15+
default = ["cc"]
16+
cc = ["libc"]
17+
nightly = []
1718

1819
[build-dependencies]
1920
gcc = "0.3"
2021

21-
[dependencies]
22+
[dependencies.libc]
23+
version = "*"
24+
optional = true

src/clear.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
//! assert!(!as_bytes(&place).contains(&0x41));
4040
//! ```
4141
42-
use std::mem;
43-
use std::ptr;
42+
use core::mem;
43+
use core::ptr;
4444

4545
use hide::hide_mem_impl;
4646

src/clear_on_drop.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::borrow::{Borrow, BorrowMut};
2-
use std::cmp::Ordering;
3-
use std::fmt;
4-
use std::hash::{Hash, Hasher};
5-
use std::mem;
6-
use std::ops::{Deref, DerefMut};
7-
use std::ptr;
1+
use core::borrow::{Borrow, BorrowMut};
2+
use core::cmp::Ordering;
3+
use core::fmt;
4+
use core::hash::{Hash, Hasher};
5+
use core::mem;
6+
use core::ops::{Deref, DerefMut};
7+
use core::ptr;
88

99
use clear::Clear;
1010

@@ -138,7 +138,7 @@ impl<P> Drop for ClearOnDrop<P>
138138
}
139139
}
140140

141-
// std::convert traits
141+
// core::convert traits
142142

143143
impl<P, T: ?Sized> AsRef<T> for ClearOnDrop<P>
144144
where P: DerefMut + AsRef<T>,
@@ -160,7 +160,7 @@ impl<P, T: ?Sized> AsMut<T> for ClearOnDrop<P>
160160
}
161161
}
162162

163-
// std::borrow traits
163+
// core::borrow traits
164164

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

191-
// std::hash traits
191+
// core::hash traits
192192

193193
impl<P> Hash for ClearOnDrop<P>
194194
where P: DerefMut + Hash,
@@ -200,7 +200,7 @@ impl<P> Hash for ClearOnDrop<P>
200200
}
201201
}
202202

203-
// std::cmp traits
203+
// core::cmp traits
204204

205205
impl<P, Q> PartialEq<ClearOnDrop<Q>> for ClearOnDrop<P>
206206
where P: DerefMut + PartialEq<Q>,

src/hide.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ pub fn hide_ptr<P>(mut ptr: P) -> P {
2525
#[cfg(feature = "nightly")]
2626
pub use self::nightly::*;
2727

28-
#[cfg(not(feature = "no_cc"))]
28+
#[cfg(feature = "cc")]
2929
pub use self::cc::*;
3030

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

3434
// On nightly, inline assembly can be used.
@@ -63,9 +63,11 @@ mod nightly {
6363
}
6464

6565
// When a C compiler is available, a dummy C function can be used.
66-
#[cfg(not(feature = "no_cc"))]
66+
#[cfg(feature = "cc")]
6767
mod cc {
68-
use std::os::raw::c_void;
68+
extern crate libc;
69+
70+
use self::libc::c_void;
6971

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

8284
// When neither is available, pretend the pointer is sent to a thread,
8385
// and hope this is enough to confuse the optimizer.
84-
#[cfg(all(feature = "no_cc", not(feature = "nightly")))]
86+
#[cfg(not(any(feature = "cc", feature = "nightly")))]
8587
mod fallback {
86-
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
88+
use core::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
8789

8890
#[inline]
8991
pub fn hide_mem_impl<T: ?Sized>(ptr: *mut T) {

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![cfg_attr(not(test), no_std)]
12
#![cfg_attr(feature = "nightly", feature(asm))]
23
#![cfg_attr(feature = "nightly", feature(i128_type))]
34
#![cfg_attr(feature = "nightly", feature(specialization))]
@@ -55,6 +56,9 @@
5556
//! the `no_cc` feature, works on stable Rust, and does not need a C
5657
//! compiler.
5758
59+
#[cfg(test)]
60+
extern crate core;
61+
5862
pub mod clear;
5963
mod clear_on_drop;
6064
mod clear_stack_on_return;

0 commit comments

Comments
 (0)