Skip to content

Commit

Permalink
Use alloc instead of std when possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jul 10, 2019
1 parent 0c96267 commit 7d36600
Show file tree
Hide file tree
Showing 16 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ The C code generally uses the C `int` type as a return value, where 1 indicates
success and 0 indicates failure. The module [ring::bssl](src/bssl.rs) contains
a [transparent] `Result` type which should be used as the return type when
declaring foreign functions which follow this convention. A
`ring::bssl::Result` should be converted to a `std::result::Result` using the
`ring::bssl::Result` should be converted to a `core::result::Result` using the
pattern in the following example (note the placement of `unsafe`):

[transparent]: https://doc.rust-lang.org/nightly/reference/type-layout.html#the-transparent-representation
Expand Down
2 changes: 1 addition & 1 deletion src/aead/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ pub const KEY_LEN: usize = KEY_BLOCKS * BLOCK_LEN;
mod tests {
use super::*;
use crate::test;
use alloc::vec;
use core::convert::TryInto;
use std::vec;

// This verifies the encryption functionality provided by ChaCha20_ctr32
// is successful when either computed on disjoint input/output buffers,
Expand Down
3 changes: 3 additions & 0 deletions src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@

#[macro_use]
pub mod constant;

#[cfg(feature = "use_heap")]
pub mod bigint;

pub mod montgomery;
4 changes: 2 additions & 2 deletions src/arithmetic/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ use crate::{
bits, bssl, c, error,
limb::{self, Limb, LimbMask, LIMB_BITS, LIMB_BYTES},
};
use alloc::{borrow::ToOwned as _, boxed::Box, vec, vec::Vec};
use core::{
marker::PhantomData,
ops::{Deref, DerefMut},
};
use std::{borrow::ToOwned as _, boxed::Box, vec, vec::Vec};
use untrusted;

pub unsafe trait Prime {}
Expand Down Expand Up @@ -1292,7 +1292,7 @@ extern "C" {
mod tests {
use super::*;
use crate::test;
use std::format;
use alloc::format;
use untrusted;

// Type-level representation of an arbitrary modulus.
Expand Down
2 changes: 1 addition & 1 deletion src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ mod tests {
mod max_input {
use super::super::super::digest;
use crate::polyfill;
use std::vec;
use alloc::vec;

macro_rules! max_input_tests {
( $algorithm_name:ident ) => {
Expand Down
3 changes: 2 additions & 1 deletion src/ec/suite_b/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ extern "C" {
mod tests {
use super::*;
use crate::test;
use std::{format, print, vec, vec::Vec};
use alloc::{format, vec, vec::Vec};
use std::print;
use untrusted;

const ZERO_SCALAR: Scalar = Scalar {
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl KeyRejected {
}
}

#[cfg(feature = "use_heap")]
#[cfg(feature = "use_std")]
impl std::error::Error for KeyRejected {
fn cause(&self) -> Option<&dyn std::error::Error> {
None
Expand Down
2 changes: 1 addition & 1 deletion src/io/der_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use super::{der::*, writer::*, *};
use std::boxed::Box;
use alloc::boxed::Box;

pub(crate) fn write_positive_integer(output: &mut dyn Accumulator, value: &Positive) {
let first_byte = value.first_byte();
Expand Down
2 changes: 1 addition & 1 deletion src/io/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use std::{boxed::Box, vec::Vec};
use alloc::{boxed::Box, vec::Vec};

pub trait Accumulator {
fn write_byte(&mut self, value: u8);
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
#![no_std]
#![cfg_attr(feature = "internal_benches", allow(unstable_features), feature(test))]

#[cfg(any(test, feature = "use_heap"))]
extern crate alloc;

#[cfg(any(test, feature = "use_heap"))]
extern crate std;

Expand Down
2 changes: 1 addition & 1 deletion src/rsa/padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ rsa_pss_padding!(
mod test {
use super::*;
use crate::{digest, error, test};
use std::vec;
use alloc::vec;
use untrusted;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/rsa/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
io::{self, der, der_writer},
pkcs8, rand, signature,
};
use std::boxed::Box;
use alloc::boxed::Box;
use untrusted;

/// An RSA key pair, used for signing.
Expand Down
6 changes: 3 additions & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ use crate::bits;

use crate::{digest, error};

use std::{format, string::String, vec::Vec};
use std::{panic, println};
use alloc::{format, string::String, vec::Vec};
use std::println;

/// `compile_time_assert_clone::<T>();` fails to compile if `T` doesn't
/// implement `Clone`.
Expand Down Expand Up @@ -311,7 +311,7 @@ where

#[allow(box_pointers)]
while let Some(mut test_case) = parse_test_case(&mut current_section, lines) {
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
f(&current_section, &mut test_case)
}));
let result = match result {
Expand Down
6 changes: 2 additions & 4 deletions tests/ecdsa_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ fn ecdsa_from_pkcs8_test() {
test::run(
test_file!("ecdsa_from_pkcs8_tests.txt"),
|section, test_case| {
use std::error::Error;

assert_eq!(section, "");

let curve_name = test_case.consume_string("Curve");
Expand Down Expand Up @@ -85,7 +83,7 @@ fn ecdsa_from_pkcs8_test() {
(Ok(_), None) => (),
(Err(e), None) => panic!("Failed with error \"{}\", but expected to succeed", e),
(Ok(_), Some(e)) => panic!("Succeeded, but expected error \"{}\"", e),
(Err(actual), Some(expected)) => assert_eq!(actual.description(), expected),
(Err(actual), Some(expected)) => assert_eq!(format!("{}", actual), expected),
};

match (
Expand All @@ -95,7 +93,7 @@ fn ecdsa_from_pkcs8_test() {
(Ok(_), None) => (),
(Err(e), None) => panic!("Failed with error \"{}\", but expected to succeed", e),
(Ok(_), Some(e)) => panic!("Succeeded, but expected error \"{}\"", e),
(Err(actual), Some(expected)) => assert_eq!(actual.description(), expected),
(Err(actual), Some(expected)) => assert_eq!(format!("{}", actual), expected),
};

assert!(signature::EcdsaKeyPair::from_pkcs8(other_fixed, &input).is_err());
Expand Down
2 changes: 1 addition & 1 deletion tests/pbkdf2_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
)]

use ring::{digest, error, pbkdf2, test, test_file};
use std::num::NonZeroU32;
use core::num::NonZeroU32;

#[test]
pub fn pbkdf2_tests() {
Expand Down
5 changes: 1 addition & 4 deletions tests/rsa_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ fn rsa_from_pkcs8_test() {
test::run(
test_file!("rsa_from_pkcs8_tests.txt"),
|section, test_case| {
use std::error::Error;

assert_eq!(section, "");

let input = test_case.consume_bytes("Input");
Expand All @@ -57,7 +55,7 @@ fn rsa_from_pkcs8_test() {
(Ok(_), None) => (),
(Err(e), None) => panic!("Failed with error \"{}\", but expected to succeed", e),
(Ok(_), Some(e)) => panic!("Succeeded, but expected error \"{}\"", e),
(Err(actual), Some(expected)) => assert_eq!(actual.description(), expected),
(Err(actual), Some(expected)) => assert_eq!(format!("{}", actual), expected),
};

Ok(())
Expand Down Expand Up @@ -93,7 +91,6 @@ fn test_signature_rsa_pkcs1_sign() {
return Ok(());
}
let key_pair = key_pair.unwrap();
let key_pair = std::sync::Arc::new(key_pair);

// XXX: This test is too slow on Android ARM Travis CI builds.
// TODO: re-enable these tests on Android ARM.
Expand Down

0 comments on commit 7d36600

Please sign in to comment.