Skip to content

Commit 1b2c3dc

Browse files
yjhmelodyDaniPopes
andauthored
feat: add ToHexExt trait (#8)
* feat: add `ToHexExt` trait to support prefix * fmt * improve docs * feat: expand Ext trait * fix: clippy * fix: test * test: improve trait tests * fix: feature gate to alloc * fix: import String --------- Co-authored-by: DaniPopes <[email protected]>
1 parent 288c9be commit 1b2c3dc

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
#![cfg_attr(not(feature = "hex"), doc = "[`hex`]: https://docs.rs/hex")]
1717
#![cfg_attr(not(feature = "std"), no_std)]
1818
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
19-
#![cfg_attr(feature = "nightly", feature(core_intrinsics, inline_const))]
19+
#![cfg_attr(
20+
feature = "nightly",
21+
feature(core_intrinsics, inline_const),
22+
allow(internal_features)
23+
)]
2024
#![cfg_attr(feature = "portable-simd", feature(portable_simd))]
2125
#![warn(
2226
missing_copy_implementations,
@@ -59,6 +63,10 @@ use arch::{generic, imp};
5963

6064
mod impl_core;
6165

66+
pub mod traits;
67+
#[cfg(feature = "alloc")]
68+
pub use traits::ToHexExt;
69+
6270
// If the `hex` feature is enabled, re-export the `hex` crate's traits.
6371
// Otherwise, use our own with the more optimized implementation.
6472
cfg_if! {
@@ -70,7 +78,6 @@ cfg_if! {
7078
mod error;
7179
pub use error::FromHexError;
7280

73-
mod traits;
7481
#[allow(deprecated)]
7582
pub use traits::{FromHex, ToHex};
7683
}

src/traits.rs

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use alloc::{
99
borrow::{Cow, ToOwned},
1010
boxed::Box,
1111
rc::Rc,
12+
string::String,
1213
sync::Arc,
1314
vec::Vec,
1415
};
@@ -18,7 +19,7 @@ use alloc::{
1819
/// This trait is implemented for all `T` which implement `AsRef<[u8]>`. This
1920
/// includes `String`, `str`, `Vec<u8>` and `[u8]`.
2021
///
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`].
2223
///
2324
/// # Examples
2425
///
@@ -27,20 +28,54 @@ use alloc::{
2728
/// use const_hex::ToHex;
2829
///
2930
/// assert_eq!("Hello world!".encode_hex::<String>(), "48656c6c6f20776f726c6421");
31+
/// assert_eq!("Hello world!".encode_hex_upper::<String>(), "48656C6C6F20776F726C6421");
3032
/// ```
3133
#[cfg_attr(feature = "alloc", doc = "\n[`encode`]: crate::encode")]
3234
#[cfg_attr(not(feature = "alloc"), doc = "\n[`encode`]: crate::encode_to_slice")]
3335
#[deprecated(note = "use `encode` or other specialized functions instead")]
3436
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`).
3739
fn encode_hex<T: iter::FromIterator<char>>(&self) -> T;
3840

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`).
4143
fn encode_hex_upper<T: iter::FromIterator<char>>(&self) -> T;
4244
}
4345

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+
4479
struct BytesToHexChars<'a, const UPPER: bool> {
4580
inner: core::slice::Iter<'a, u8>,
4681
next: Option<char>,
@@ -88,6 +123,29 @@ impl<T: AsRef<[u8]>> ToHex for T {
88123
}
89124
}
90125

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+
91149
/// Types that can be decoded from a hex string.
92150
///
93151
/// This trait is implemented for `Vec<u8>` and small `u8`-arrays.

0 commit comments

Comments
 (0)