Skip to content

Commit

Permalink
Merge pull request RustCrypto#163 from RustCrypto/digest/alloc-feature
Browse files Browse the repository at this point in the history
digest: add `alloc` feature
  • Loading branch information
tarcieri committed Jun 2, 2020
2 parents 88bdef6 + fb0ace3 commit a853a6f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/digest.yml
Expand Up @@ -52,5 +52,6 @@ jobs:
- run: cargo check --all-features
- run: cargo test --release
- run: cargo test --features dev --release
- run: cargo test --features alloc --release
- run: cargo test --features std --release
- run: cargo test --all-features --release
3 changes: 2 additions & 1 deletion digest/Cargo.toml
Expand Up @@ -16,7 +16,8 @@ generic-array = "0.14"
blobby = { version = "0.1", optional = true }

[features]
std = []
alloc = []
std = ["alloc"]
dev = ["blobby"]

[package.metadata.docs.rs]
Expand Down
4 changes: 2 additions & 2 deletions digest/src/dyn_digest.rs
@@ -1,5 +1,5 @@
#![cfg(feature = "std")]
use std::boxed::Box;
#![cfg(feature = "alloc")]
use alloc::boxed::Box;

use super::{FixedOutput, Reset, Update};
use generic_array::typenum::Unsigned;
Expand Down
21 changes: 12 additions & 9 deletions digest/src/lib.rs
Expand Up @@ -19,8 +19,11 @@
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo_small.png")]
#![warn(missing_docs, rust_2018_idioms)]

#[cfg(feature = "std")]
#[cfg(feature = "alloc")]
#[macro_use]
extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "dev")]
Expand All @@ -35,14 +38,14 @@ pub use crate::digest::{Digest, Output};
pub use crate::errors::InvalidOutputSize;
pub use generic_array::{self, typenum::consts};

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub use dyn_digest::DynDigest;

use generic_array::{ArrayLength, GenericArray};

#[cfg(feature = "std")]
use std::vec::Vec;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

/// Trait for updating digest state with input data.
pub trait Update {
Expand Down Expand Up @@ -99,8 +102,8 @@ pub trait VariableOutput: core::marker::Sized {
fn finalize_variable<F: FnOnce(&[u8])>(self, f: F);

/// Retrieve result into vector and consume hasher.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
fn finalize_vec(self) -> Vec<u8> {
let mut buf = Vec::with_capacity(self.output_size());
self.finalize_variable(|res| buf.extend_from_slice(res));
Expand All @@ -124,8 +127,8 @@ pub trait ExtendableOutput: core::marker::Sized {
fn finalize_xof(self) -> Self::Reader;

/// Retrieve result into vector of specified length.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
fn finalize_vec(self, n: usize) -> Vec<u8> {
let mut buf = vec![0u8; n];
self.finalize_xof().read(&mut buf);
Expand Down

0 comments on commit a853a6f

Please sign in to comment.