Skip to content

Commit

Permalink
Use #![no_std] and switch dependencies from std to core.
Browse files Browse the repository at this point in the history
The tests still depend on libstd.
  • Loading branch information
briansmith committed Jan 21, 2016
1 parent abb5cbe commit ae75675
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//!
//! Go analog: [`crypto.cipher.AEAD`](https://golang.org/pkg/crypto/cipher/#AEAD)

use std;
use core;
use super::{c, ffi};

/// A key for authenticating and decrypting (“opening”)
Expand Down Expand Up @@ -208,7 +208,7 @@ impl Key {
ffi::map_bssl_result(unsafe {
(self.algorithm.init)(
self.ctx_buf.as_mut_ptr(),
std::mem::size_of::<[u64; KEY_CTX_BUF_ELEMS]>(),
core::mem::size_of::<[u64; KEY_CTX_BUF_ELEMS]>(),
key_bytes.as_ptr(), key_bytes.len())
})
}
Expand Down Expand Up @@ -418,6 +418,7 @@ extern {
#[cfg(test)]
mod tests {
use super::super::{aead, file_test};
use std::vec::Vec;

#[test]
pub fn test_aes_gcm_128() {
Expand Down
6 changes: 3 additions & 3 deletions src/agreement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

//! Key agreement: ECDH.

use std;
use core;
use super::{c, digest, ecc};
#[cfg(not(feature = "no_heap"))] use super::ffi;
use super::input::Input;
Expand Down Expand Up @@ -179,7 +179,7 @@ pub extern fn SHA512_5(out: *mut u8, out_len: c::size_t,
part_len: c::size_t) {
if part_len != 0 {
assert!(!part.is_null());
ctx.update(unsafe { std::slice::from_raw_parts(part, part_len) });
ctx.update(unsafe { core::slice::from_raw_parts(part, part_len) });
}
}

Expand All @@ -191,7 +191,7 @@ pub extern fn SHA512_5(out: *mut u8, out_len: c::size_t,
maybe_update(&mut ctx, part5, part5_len);
let digest = ctx.finish();
let digest = digest.as_ref();
let out = unsafe { std::slice::from_raw_parts_mut(out, out_len) };
let out = unsafe { core::slice::from_raw_parts_mut(out, out_len) };
assert_eq!(out.len(), digest.len());
for i in 0..digest.len() {
out[i] = digest[i];
Expand Down
2 changes: 2 additions & 0 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ pub mod test_util {

#[cfg(test)]
mod tests {
use std::vec::Vec;
use super::super::{digest, file_test};

/// Test vectors from BoringSSL.
Expand Down Expand Up @@ -462,6 +463,7 @@ mod tests {
}

mod shavs {
use std::vec::Vec;
use super::super::super::{digest, file_test};

macro_rules! shavs_tests {
Expand Down
2 changes: 2 additions & 0 deletions src/file_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use rustc_serialize::hex::FromHex;
use std;
use std::string::String;
use std::vec::Vec;
use std::io::BufRead;
use super::digest;

Expand Down
4 changes: 2 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
//! parser is a good example of a real-world use of the Input/Reader framework
//! to parse complex data.

use std;
use core;

/// Calls `read` with the given input as a `Reader`, ensuring that `read`
/// consumed the entire input. If `read` does not consume the entire input,
Expand Down Expand Up @@ -138,7 +138,7 @@ impl<'a> Input<'a> {
// This limit is important for avoiding integer overflow. In particular,
// `Reader` assumes that an `i + 1 > i` if `input.value.get(i)` does
// not return `None`.
if bytes.len() > std::usize::MAX - 1 {
if bytes.len() > core::usize::MAX - 1 {
return Err(())
}
Ok(Input { value: no_panic::NoPanicSlice::new(bytes) })
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@
//! functionality still uses the heap.
//! </table>

#![no_std]

#[cfg(test)]
extern crate rustc_serialize;

#[cfg(test)]
#[macro_use(format, print, println, vec)]
extern crate std;

pub mod aead;
pub mod agreement;
mod c;
Expand Down
13 changes: 6 additions & 7 deletions src/polyfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
//! standard library soon.

pub mod slice {
use core;

// https://github.com/rust-lang/rust/issues/27750
// https://internals.rust-lang.org/t/stabilizing-basic-functions-on-arrays-and-slices/2868
#[inline(always)]
Expand All @@ -29,29 +31,26 @@ pub mod slice {
// https://internals.rust-lang.org/t/stabilizing-basic-functions-on-arrays-and-slices/2868
#[inline(always)]
pub fn fill_from_slice(dest: &mut [u8], src: &[u8]) {
use std;
assert_eq!(dest.len(), src.len());
unsafe {
std::ptr::copy_nonoverlapping(src.as_ptr(), dest.as_mut_ptr(),
src.len())
core::ptr::copy_nonoverlapping(src.as_ptr(), dest.as_mut_ptr(),
src.len())
}
}

// https://internals.rust-lang.org/t/safe-trasnsmute-for-slices-e-g-u64-u32-particularly-simd-types/2871
#[inline(always)]
pub fn u64_as_u8<'a>(src: &'a [u64]) -> &'a [u8] {
use std;
unsafe {
std::slice::from_raw_parts(src.as_ptr() as *const u8, src.len() * 8)
core::slice::from_raw_parts(src.as_ptr() as *const u8, src.len() * 8)
}
}

// https://internals.rust-lang.org/t/safe-trasnsmute-for-slices-e-g-u64-u32-particularly-simd-types/2871
#[inline(always)]
pub fn u64_as_u32<'a>(src: &'a [u64]) -> &'a [u32] {
use std;
unsafe {
std::slice::from_raw_parts(src.as_ptr() as *const u32, src.len() * 2)
core::slice::from_raw_parts(src.as_ptr() as *const u32, src.len() * 2)
}
}
}

0 comments on commit ae75675

Please sign in to comment.