|
34 | 34 | //! # } |
35 | 35 | //! ``` |
36 | 36 | //! |
| 37 | +//! ## Stabilization strategy |
| 38 | +//! |
| 39 | +//! Because downstream crates may need to return hex errors in their APIs and they need to be |
| 40 | +//! stabilized soon, this crate only exposes the errors and two basic decoding functions. This |
| 41 | +//! should already help with the vast majority of the cases and we're sufficiently confident that |
| 42 | +//! these errors won't have a breaking change any time soon (possibly never). |
| 43 | +//! |
| 44 | +//! If you're writing a binary you don't need to worry about any of this and just use the unstable |
| 45 | +//! version for now. If you're writing a library you should use these stable errors in the API but |
| 46 | +//! you may internally depend on the unstable crate version to get the advanced features that won't |
| 47 | +//! affect your API. This way your API can stabilize before all features in this crate are fully |
| 48 | +//! stable and you still can use all of them. |
| 49 | +//! |
| 50 | +//! ## Crate feature flags |
| 51 | +//! |
| 52 | +//! * `std` - enables the standard library, on by default. |
| 53 | +//! * `alloc` - enables features that require allocation such as decoding into `Vec<u8>`, implied |
| 54 | +//! by `std`. |
| 55 | +//! * `newer-rust-version` - enables Rust version detection and thus newer features, may add |
| 56 | +//! dependency on a feature detection crate to reduce compile times. This feature is expected to |
| 57 | +//! do nothing once the native detection is in Rust and our MSRV is at least that version. We may |
| 58 | +//! also remove the feature gate in 2.0 with semver trick once that happens. |
| 59 | +//! |
37 | 60 | //! ## MSRV policy |
38 | 61 | //! |
39 | 62 | //! The MSRV of the crate is currently 1.63.0 and we don't intend to bump it until the newer Rust |
@@ -97,29 +120,37 @@ pub use self::{ |
97 | 120 | parse::FromHex, |
98 | 121 | }; |
99 | 122 |
|
100 | | -/// Decodes a hex string into a vector of bytes. |
| 123 | +/// Decodes a hex string with variable length. |
| 124 | +/// |
| 125 | +/// The length of the returned `Vec` is determined by the length of the input, meaning all even |
| 126 | +/// lengths of the input string are allowed. If you know the required length at compile time using |
| 127 | +/// [`decode_to_array`] is most likely a better choice. |
101 | 128 | /// |
102 | 129 | /// # Errors |
103 | 130 | /// |
104 | | -/// Errors if `s` is not a valid hex string. |
| 131 | +/// Returns an error if `hex` contains invalid characters or doesn't have even length. |
105 | 132 | #[cfg(feature = "alloc")] |
106 | | -pub fn decode_to_vec(s: &str) -> Result<Vec<u8>, HexToBytesError> { |
107 | | - Ok(HexToBytesIter::new(s)?.drain_to_vec()?) |
| 133 | +pub fn decode_to_vec(hex: &str) -> Result<Vec<u8>, HexToBytesError> { |
| 134 | + Ok(HexToBytesIter::new(hex)?.drain_to_vec()?) |
108 | 135 | } |
109 | 136 |
|
110 | | -/// Decodes a hex string into an array of bytes. |
| 137 | +/// Decodes a hex string with an expected length kown at compile time. |
| 138 | +/// |
| 139 | +/// If you don't know the required length at compile time you need to use [`decode_to_vec`] |
| 140 | +/// instead. |
111 | 141 | /// |
112 | 142 | /// # Errors |
113 | 143 | /// |
114 | | -/// Errors if `s` is not a valid hex string or the correct length. |
115 | | -pub fn decode_to_array<const N: usize>(s: &str) -> Result<[u8; N], HexToArrayError> { |
116 | | - if s.len() == N * 2 { |
| 144 | +/// Returns an error if `hex` contains invalid characters or has incorrect length. (Should be |
| 145 | +/// `N * 2`.) |
| 146 | +pub fn decode_to_array<const N: usize>(hex: &str) -> Result<[u8; N], HexToArrayError> { |
| 147 | + if hex.len() == N * 2 { |
117 | 148 | let mut ret = [0u8; N]; |
118 | 149 | // checked above |
119 | | - HexToBytesIter::new_unchecked(s).drain_to_slice(&mut ret)?; |
| 150 | + HexToBytesIter::new_unchecked(hex).drain_to_slice(&mut ret)?; |
120 | 151 | Ok(ret) |
121 | 152 | } else { |
122 | | - Err(InvalidLengthError { invalid: s.len(), expected: 2 * N }.into()) |
| 153 | + Err(InvalidLengthError { invalid: hex.len(), expected: 2 * N }.into()) |
123 | 154 | } |
124 | 155 | } |
125 | 156 |
|
|
0 commit comments