|
8 | 8 | //! let bytes = b"hello world";
|
9 | 9 | //! let base32 = base32_simd::BASE32;
|
10 | 10 | //!
|
11 |
| -//! let encoded = base32.encode_type::<String>(bytes); |
12 |
| -//! assert_eq!(&*encoded, "NBSWY3DPEB3W64TMMQ======"); |
| 11 | +//! let encoded = base32.encode_to_string(bytes); |
| 12 | +//! assert_eq!(encoded, "NBSWY3DPEB3W64TMMQ======"); |
13 | 13 | //!
|
14 |
| -//! let decoded = base32.decode_type::<Vec<u8>>(encoded.as_bytes()).unwrap(); |
15 |
| -//! assert_eq!(&*decoded, bytes); |
| 14 | +//! let decoded = base32.decode_to_vec(encoded).unwrap(); |
| 15 | +//! assert_eq!(decoded, bytes); |
16 | 16 | //! # }
|
17 | 17 | //! ```
|
18 | 18 | //!
|
@@ -70,6 +70,9 @@ use crate::encode::encoded_length_unchecked;
|
70 | 70 |
|
71 | 71 | use vsimd::tools::{slice_mut, slice_parts};
|
72 | 72 |
|
| 73 | +#[cfg(feature = "alloc")] |
| 74 | +use alloc::{string::String, vec::Vec}; |
| 75 | + |
73 | 76 | const BASE32_CHARSET: &[u8; 32] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
74 | 77 | const BASE32HEX_CHARSET: &[u8; 32] = b"0123456789ABCDEFGHIJKLMNOPQRSTUV";
|
75 | 78 |
|
@@ -244,32 +247,52 @@ impl Base32 {
|
244 | 247 | /// Encodes bytes to a base32 string and returns a specified type.
|
245 | 248 | #[inline]
|
246 | 249 | #[must_use]
|
247 |
| - pub fn encode_type<T: FromBase32Encode>(&self, data: &[u8]) -> T { |
248 |
| - T::from_base32_encode(self, data) |
| 250 | + pub fn encode_type<T: FromBase32Encode>(&self, data: impl AsRef<[u8]>) -> T { |
| 251 | + T::from_base32_encode(self, data.as_ref()) |
249 | 252 | }
|
250 | 253 |
|
251 | 254 | /// Decodes a base32 string to bytes and returns a specified type.
|
252 | 255 | ///
|
253 | 256 | /// # Errors
|
254 | 257 | /// This function returns `Err` if the content of `data` is invalid.
|
255 | 258 | #[inline]
|
256 |
| - pub fn decode_type<T: FromBase32Decode>(&self, data: &[u8]) -> Result<T, Error> { |
257 |
| - T::from_base32_decode(self, data) |
| 259 | + pub fn decode_type<T: FromBase32Decode>(&self, data: impl AsRef<[u8]>) -> Result<T, Error> { |
| 260 | + T::from_base32_decode(self, data.as_ref()) |
258 | 261 | }
|
259 | 262 |
|
260 | 263 | /// Encodes bytes to a base32 string and appends to a specified type.
|
261 | 264 | #[inline]
|
262 |
| - pub fn encode_append<T: AppendBase32Encode>(&self, src: &[u8], dst: &mut T) { |
263 |
| - T::append_base32_encode(self, src, dst); |
| 265 | + pub fn encode_append<T: AppendBase32Encode>(&self, src: impl AsRef<[u8]>, dst: &mut T) { |
| 266 | + T::append_base32_encode(self, src.as_ref(), dst); |
264 | 267 | }
|
265 | 268 |
|
266 | 269 | /// Decodes a base32 string to bytes and appends to a specified type.
|
267 | 270 | ///
|
268 | 271 | /// # Errors
|
269 | 272 | /// This function returns `Err` if the content of `src` is invalid.
|
270 | 273 | #[inline]
|
271 |
| - pub fn decode_append<T: AppendBase32Decode>(&self, src: &[u8], dst: &mut T) -> Result<(), Error> { |
272 |
| - T::append_base32_decode(self, src, dst) |
| 274 | + pub fn decode_append<T: AppendBase32Decode>(&self, src: impl AsRef<[u8]>, dst: &mut T) -> Result<(), Error> { |
| 275 | + T::append_base32_decode(self, src.as_ref(), dst) |
| 276 | + } |
| 277 | + |
| 278 | + /// Encodes bytes to a base32 string. |
| 279 | + #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] |
| 280 | + #[cfg(feature = "alloc")] |
| 281 | + #[inline] |
| 282 | + #[must_use] |
| 283 | + pub fn encode_to_string(&self, data: impl AsRef<[u8]>) -> String { |
| 284 | + self.encode_type(data.as_ref()) |
| 285 | + } |
| 286 | + |
| 287 | + /// Decodes a base32 string to bytes. |
| 288 | + /// |
| 289 | + /// # Errors |
| 290 | + /// This function returns `Err` if the content of `data` is invalid. |
| 291 | + #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] |
| 292 | + #[cfg(feature = "alloc")] |
| 293 | + #[inline] |
| 294 | + pub fn decode_to_vec(&self, data: impl AsRef<[u8]>) -> Result<Vec<u8>, Error> { |
| 295 | + self.decode_type(data.as_ref()) |
273 | 296 | }
|
274 | 297 | }
|
275 | 298 |
|
|
0 commit comments