Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no_std support (closes #11) #12

Merged
merged 1 commit into from
Aug 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/hide.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__attribute__ ((visibility ("hidden")))
#endif
#endif
void *clear_on_drop_hide(void *ptr) {
unsigned char *clear_on_drop_hide(unsigned char *ptr) {
#if defined(__GNUC__)
/* Not needed with MSVC, since Rust uses LLVM and LTO can't inline this. */
__asm__ volatile ("" : "=r" (ptr) : "0" (ptr) : "memory");
Expand Down
8 changes: 3 additions & 5 deletions src/hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,14 @@ mod nightly {
// When a C compiler is available, a dummy C function can be used.
#[cfg(not(feature = "no_cc"))]
mod cc {
use std::os::raw::c_void;

extern "C" {
fn clear_on_drop_hide(ptr: *mut c_void) -> *mut c_void;
fn clear_on_drop_hide(ptr: *mut u8) -> *mut u8;
}

#[inline]
pub fn hide_mem_impl<T: ?Sized>(ptr: *mut T) {
unsafe {
clear_on_drop_hide(ptr as *mut c_void);
clear_on_drop_hide(ptr as *mut u8);
}
}
}
Expand All @@ -83,7 +81,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<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