@@ -9,6 +9,7 @@ use alloc::{
9
9
borrow:: { Cow , ToOwned } ,
10
10
boxed:: Box ,
11
11
rc:: Rc ,
12
+ string:: String ,
12
13
sync:: Arc ,
13
14
vec:: Vec ,
14
15
} ;
@@ -18,7 +19,7 @@ use alloc::{
18
19
/// This trait is implemented for all `T` which implement `AsRef<[u8]>`. This
19
20
/// includes `String`, `str`, `Vec<u8>` and `[u8]`.
20
21
///
21
- /// *Note*: instead of using this trait, you might want to use [`encode`].
22
+ /// *Note*: instead of using this trait, you might want to use [`encode`](crate::encode) or [`ToHexExt`] .
22
23
///
23
24
/// # Examples
24
25
///
@@ -27,20 +28,54 @@ use alloc::{
27
28
/// use const_hex::ToHex;
28
29
///
29
30
/// assert_eq!("Hello world!".encode_hex::<String>(), "48656c6c6f20776f726c6421");
31
+ /// assert_eq!("Hello world!".encode_hex_upper::<String>(), "48656C6C6F20776F726C6421");
30
32
/// ```
31
33
#[ cfg_attr( feature = "alloc" , doc = "\n [`encode`]: crate::encode" ) ]
32
34
#[ cfg_attr( not( feature = "alloc" ) , doc = "\n [`encode`]: crate::encode_to_slice" ) ]
33
35
#[ deprecated( note = "use `encode` or other specialized functions instead" ) ]
34
36
pub trait ToHex {
35
- /// Encode the hex strict representing `self` into the result. Lower case
36
- /// letters are used (e.g. `f9b4ca`)
37
+ /// Encode the hex strict representing `self` into the result.
38
+ /// Lower case letters are used (e.g. `f9b4ca`).
37
39
fn encode_hex < T : iter:: FromIterator < char > > ( & self ) -> T ;
38
40
39
- /// Encode the hex strict representing `self` into the result. Upper case
40
- /// letters are used (e.g. `F9B4CA`)
41
+ /// Encode the hex strict representing `self` into the result.
42
+ /// Upper case letters are used (e.g. `F9B4CA`).
41
43
fn encode_hex_upper < T : iter:: FromIterator < char > > ( & self ) -> T ;
42
44
}
43
45
46
+ /// Encoding values as hex string with prefix `0x`.
47
+ ///
48
+ /// This trait is implemented for all `T` which implement `AsRef<[u8]>`.
49
+ ///
50
+ /// # Examples
51
+ ///
52
+ /// ```
53
+ /// use const_hex::ToHexExt;
54
+ ///
55
+ /// assert_eq!("Hello world!".encode_hex(), "48656c6c6f20776f726c6421");
56
+ /// assert_eq!("Hello world!".encode_hex_upper(), "48656C6C6F20776F726C6421");
57
+ /// assert_eq!("Hello world!".encode_hex_with_prefix(), "0x48656c6c6f20776f726c6421");
58
+ /// assert_eq!("Hello world!".encode_hex_upper_with_prefix(), "0x48656C6C6F20776F726C6421");
59
+ /// ```
60
+ #[ cfg( feature = "alloc" ) ]
61
+ pub trait ToHexExt {
62
+ /// Encode the hex strict representing `self` into the result.
63
+ /// Lower case letters are used (e.g. `f9b4ca`).
64
+ fn encode_hex ( & self ) -> String ;
65
+
66
+ /// Encode the hex strict representing `self` into the result.
67
+ /// Upper case letters are used (e.g. `F9B4CA`).
68
+ fn encode_hex_upper ( & self ) -> String ;
69
+
70
+ /// Encode the hex strict representing `self` into the result with prefix `0x`.
71
+ /// Lower case letters are used (e.g. `0xf9b4ca`).
72
+ fn encode_hex_with_prefix ( & self ) -> String ;
73
+
74
+ /// Encode the hex strict representing `self` into the result with prefix `0X`.
75
+ /// Upper case letters are used (e.g. `0xF9B4CA`).
76
+ fn encode_hex_upper_with_prefix ( & self ) -> String ;
77
+ }
78
+
44
79
struct BytesToHexChars < ' a , const UPPER : bool > {
45
80
inner : core:: slice:: Iter < ' a , u8 > ,
46
81
next : Option < char > ,
@@ -88,6 +123,29 @@ impl<T: AsRef<[u8]>> ToHex for T {
88
123
}
89
124
}
90
125
126
+ #[ cfg( feature = "alloc" ) ]
127
+ impl < T : AsRef < [ u8 ] > > ToHexExt for T {
128
+ #[ inline]
129
+ fn encode_hex ( & self ) -> String {
130
+ crate :: encode ( self )
131
+ }
132
+
133
+ #[ inline]
134
+ fn encode_hex_upper ( & self ) -> String {
135
+ crate :: encode_upper ( self )
136
+ }
137
+
138
+ #[ inline]
139
+ fn encode_hex_with_prefix ( & self ) -> String {
140
+ crate :: encode_prefixed ( self )
141
+ }
142
+
143
+ #[ inline]
144
+ fn encode_hex_upper_with_prefix ( & self ) -> String {
145
+ crate :: encode_upper_prefixed ( self )
146
+ }
147
+ }
148
+
91
149
/// Types that can be decoded from a hex string.
92
150
///
93
151
/// This trait is implemented for `Vec<u8>` and small `u8`-arrays.
0 commit comments