Skip to content
Open
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
2 changes: 1 addition & 1 deletion dash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test-utils = []
# The no-std feature doesn't disable std - you need to turn off the std feature for that by disabling default.
# Instead no-std enables additional features required for this crate to be usable without std.
# As a result, both can be enabled without conflict.
std = ["secp256k1/std", "dashcore_hashes/std", "bech32/std", "internals/std"]
std = ["secp256k1/std", "dashcore_hashes/std", "bech32/std"]
no-std = ["core2", "dashcore_hashes/alloc", "dashcore_hashes/core2", "secp256k1/alloc"]

[package.metadata.docs.rs]
Expand Down
4 changes: 2 additions & 2 deletions hashes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ exclude = ["tests", "contrib"]

[features]
default = ["std"]
std = ["alloc", "internals/std"]
alloc = ["internals/alloc"]
std = ["alloc"]
alloc = []
schemars = ["actual-schemars"]
serde-std = ["serde/std"]
x11 = ["rs-x11-hash"]
Expand Down
2 changes: 0 additions & 2 deletions internals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ exclude = ["tests", "contrib"]

[features]
default = []
std = ["alloc"]
alloc = []

[package.metadata.docs.rs]
all-features = true
Expand Down
97 changes: 41 additions & 56 deletions internals/src/hex/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use core::fmt;
use super::Case;
use super::buf_encoder::{BufEncoder, OutBytes};
use crate::hex::buf_encoder::FixedLenBuf;
#[cfg(feature = "alloc")]
use crate::prelude::*;

/// Extension trait for types that can be displayed as hex.
///
Expand All @@ -34,7 +32,6 @@ pub trait DisplayHex: Copy + sealed::IsRef {
/// A shorthand for `to_hex_string(Case::Lower)`, so that `Case` doesn't need to be imported.
///
/// This may be faster than `.display_hex().to_string()` because it uses `reserve_suggestion`.
#[cfg(feature = "alloc")]
fn to_lower_hex_string(self) -> String {
self.to_hex_string(Case::Lower)
}
Expand All @@ -44,15 +41,13 @@ pub trait DisplayHex: Copy + sealed::IsRef {
/// A shorthand for `to_hex_string(Case::Upper)`, so that `Case` doesn't need to be imported.
///
/// This may be faster than `.display_hex().to_string()` because it uses `reserve_suggestion`.
#[cfg(feature = "alloc")]
fn to_upper_hex_string(self) -> String {
self.to_hex_string(Case::Upper)
}

/// Create a hex-encoded string.
///
/// This may be faster than `.display_hex().to_string()` because it uses `reserve_suggestion`.
#[cfg(feature = "alloc")]
fn to_hex_string(self, case: Case) -> String {
let mut string = String::new();
self.append_hex_to_string(case, &mut string);
Expand All @@ -63,7 +58,6 @@ pub trait DisplayHex: Copy + sealed::IsRef {
///
/// This may be faster than `write!(string, "{:x}", self.display_hex())` because it uses
/// `reserve_suggestion`.
#[cfg(feature = "alloc")]
fn append_hex_to_string(self, case: Case, string: &mut String) {
use fmt::Write;

Expand Down Expand Up @@ -117,8 +111,7 @@ impl<'a> DisplayHex for &'a [u8] {
}
}

#[cfg(feature = "alloc")]
impl<'a> DisplayHex for &'a alloc::vec::Vec<u8> {
impl<'a> DisplayHex for &'a Vec<u8> {
type Display = DisplayByteSlice<'a>;

#[inline]
Expand Down Expand Up @@ -274,67 +267,59 @@ where

#[cfg(test)]
mod tests {
#[cfg(feature = "alloc")]
use super::*;

#[cfg(feature = "alloc")]
mod alloc {
use super::*;
fn check_encoding(bytes: &[u8]) {
use core::fmt::Write;

fn check_encoding(bytes: &[u8]) {
use core::fmt::Write;

let s1 = bytes.to_lower_hex_string();
let mut s2 = String::with_capacity(bytes.len() * 2);
for b in bytes {
write!(s2, "{:02x}", b).unwrap();
}
assert_eq!(s1, s2);
}

#[test]
fn empty() {
check_encoding(b"");
let s1 = bytes.to_lower_hex_string();
let mut s2 = String::with_capacity(bytes.len() * 2);
for b in bytes {
write!(s2, "{:02x}", b).unwrap();
}
assert_eq!(s1, s2);
}

#[test]
fn single() {
check_encoding(b"*");
}
#[test]
fn empty() {
check_encoding(b"");
}

#[test]
fn two() {
check_encoding(b"*x");
}
#[test]
fn single() {
check_encoding(b"*");
}

#[test]
fn just_below_boundary() {
check_encoding(&[42; 512]);
}
#[test]
fn two() {
check_encoding(b"*x");
}

#[test]
fn just_above_boundary() {
check_encoding(&[42; 513]);
}
#[test]
fn just_below_boundary() {
check_encoding(&[42; 512]);
}

#[test]
fn just_above_double_boundary() {
check_encoding(&[42; 1025]);
}
#[test]
fn just_above_boundary() {
check_encoding(&[42; 513]);
}

#[test]
fn fmt_exact_macro() {
use crate::alloc::string::ToString;
#[test]
fn just_above_double_boundary() {
check_encoding(&[42; 1025]);
}

struct Dummy([u8; 32]);
#[test]
fn fmt_exact_macro() {
struct Dummy([u8; 32]);

impl fmt::Display for Dummy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_hex_exact!(f, 32, &self.0, Case::Lower)
}
impl fmt::Display for Dummy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_hex_exact!(f, 32, &self.0, Case::Lower)
}

assert_eq!(Dummy([42; 32]).to_string(), "2a".repeat(32));
}

assert_eq!(Dummy([42; 32]).to_string(), "2a".repeat(32));
}
}
13 changes: 0 additions & 13 deletions internals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,11 @@
//! [rust-dash](https://github.com/rust-dashcore) ecosystem.
//!

#![no_std]
// Experimental features we need.
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
// Coding conventions
#![warn(missing_docs)]

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

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

pub mod error;
pub mod hex;
pub mod macros;

/// Mainly reexports based on features.
pub(crate) mod prelude {
#[cfg(feature = "alloc")]
pub(crate) use alloc::string::String;
}
Loading