From ae7567553f31283c92b6a1cf7f1f483d5c6bf6fd Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Thu, 7 Jan 2016 12:36:58 -1000 Subject: [PATCH] Use `#![no_std]` and switch dependencies from `std` to `core`. The tests still depend on libstd. --- src/aead.rs | 5 +++-- src/agreement.rs | 6 +++--- src/digest.rs | 2 ++ src/file_test.rs | 2 ++ src/input.rs | 4 ++-- src/lib.rs | 5 +++++ src/polyfill.rs | 13 ++++++------- 7 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/aead.rs b/src/aead.rs index 3925f39dd4..52c9302bfd 100644 --- a/src/aead.rs +++ b/src/aead.rs @@ -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”) @@ -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()) }) } @@ -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() { diff --git a/src/agreement.rs b/src/agreement.rs index d37b6880ee..42ff2a14e3 100644 --- a/src/agreement.rs +++ b/src/agreement.rs @@ -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; @@ -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) }); } } @@ -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]; diff --git a/src/digest.rs b/src/digest.rs index 26fa2a197f..e684408b4b 100644 --- a/src/digest.rs +++ b/src/digest.rs @@ -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. @@ -462,6 +463,7 @@ mod tests { } mod shavs { + use std::vec::Vec; use super::super::super::{digest, file_test}; macro_rules! shavs_tests { diff --git a/src/file_test.rs b/src/file_test.rs index 5f0e692e7f..b803b9c61f 100644 --- a/src/file_test.rs +++ b/src/file_test.rs @@ -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; diff --git a/src/input.rs b/src/input.rs index 37b766e458..f4e254cdff 100644 --- a/src/input.rs +++ b/src/input.rs @@ -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, @@ -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) }) diff --git a/src/lib.rs b/src/lib.rs index 531ed9e8d3..76bb96a5eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,10 +27,15 @@ //! functionality still uses the heap. //! +#![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; diff --git a/src/polyfill.rs b/src/polyfill.rs index ac1568a2f9..db51c5a8af 100644 --- a/src/polyfill.rs +++ b/src/polyfill.rs @@ -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)] @@ -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) } } }