1515//! # Hex encoding and decoding
1616//!
1717
18- use std:: fmt;
19- use std:: str;
18+ use core:: { fmt, str} ;
2019use { Error , Hash } ;
2120
2221/// Trait for objects that can be serialized as hex strings
22+ #[ cfg( any( test, feature = "std" ) ) ]
2323pub trait ToHex {
2424 /// Hex representation of the object
2525 fn to_hex ( & self ) -> String ;
@@ -39,6 +39,7 @@ pub trait FromHex: Sized {
3939 }
4040}
4141
42+ #[ cfg( any( test, feature = "std" ) ) ]
4243impl < T : fmt:: LowerHex > ToHex for T {
4344 /// Outputs the hash in hexadecimal form
4445 fn to_hex ( & self ) -> String {
@@ -152,9 +153,10 @@ pub fn format_hex_reverse(data: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
152153 Ok ( ( ) )
153154}
154155
156+ #[ cfg( any( test, feature = "std" ) ) ]
155157impl ToHex for [ u8 ] {
156158 fn to_hex ( & self ) -> String {
157- use std :: fmt:: Write ;
159+ use core :: fmt:: Write ;
158160 let mut ret = String :: with_capacity ( 2 * self . len ( ) ) ;
159161 for ch in self {
160162 write ! ( ret, "{:02x}" , ch) . expect ( "writing to string" ) ;
@@ -163,6 +165,7 @@ impl ToHex for [u8] {
163165 }
164166}
165167
168+ #[ cfg( any( test, feature = "std" ) ) ]
166169impl FromHex for Vec < u8 > {
167170 fn from_byte_iter < I > ( iter : I ) -> Result < Self , Error >
168171 where I : Iterator < Item =Result < u8 , Error > > +
@@ -217,9 +220,9 @@ impl_fromhex_array!(512);
217220
218221#[ cfg( test) ]
219222mod tests {
220- use std :: fmt;
223+ use core :: fmt;
221224
222- use super :: { format_hex, format_hex_reverse, ToHex , FromHex } ;
225+ use super :: { format_hex, format_hex_reverse, FromHex , ToHex } ;
223226 use Error ;
224227
225228 #[ test]
0 commit comments