Skip to content

Commit bb820c7

Browse files
committed
shortcuts
Related: + #28 + marshallpierce/rust-base64#205
1 parent 7d1245f commit bb820c7

File tree

8 files changed

+119
-50
lines changed

8 files changed

+119
-50
lines changed

benches/base32.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn bench_decode(c: &mut Criterion) {
4444
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
4545

4646
let cases = [16, 32, 64, 256, 1024, 4096, 65536];
47-
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base32_simd::BASE32.encode_type(&rand_bytes(n)));
47+
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base32_simd::BASE32.encode_type(rand_bytes(n)));
4848

4949
#[allow(clippy::type_complexity)]
5050
let functions: &FnGroup<fn(&[u8], &mut [u8])> = &[
@@ -79,7 +79,7 @@ pub fn bench_check(c: &mut Criterion) {
7979
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
8080

8181
let cases = [16, 32, 64, 256, 1024, 4096, 65536];
82-
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base32_simd::BASE32.encode_type(&rand_bytes(n)));
82+
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base32_simd::BASE32.encode_type(rand_bytes(n)));
8383

8484
#[allow(clippy::type_complexity)]
8585
let functions: &FnGroup<fn(&[u8])> = &[

benches/base64.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn bench_decode(c: &mut Criterion) {
6060
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
6161

6262
let cases = [16, 32, 64, 256, 1024, 4096, 65536];
63-
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(&rand_bytes(n)));
63+
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(rand_bytes(n)));
6464

6565
#[allow(clippy::type_complexity)]
6666
let functions: &FnGroup<fn(&[u8], &mut [u8])> = &[
@@ -102,7 +102,7 @@ pub fn bench_check(c: &mut Criterion) {
102102
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
103103

104104
let cases = [16, 32, 64, 256, 1024, 4096, 65536];
105-
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(&rand_bytes(n)));
105+
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(rand_bytes(n)));
106106

107107
#[allow(clippy::type_complexity)]
108108
let functions: &FnGroup<fn(&[u8])> = &[
@@ -126,7 +126,7 @@ pub fn bench_forgiving_decode(c: &mut Criterion) {
126126
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
127127

128128
let cases = [16, 32, 64, 256, 1024, 4096, 65536];
129-
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(&rand_bytes(n)));
129+
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(rand_bytes(n)));
130130

131131
#[allow(clippy::type_complexity)]
132132
let functions: &FnGroup<fn(&[u8], &mut [u8])> = &[

crates/base32-simd/src/lib.rs

+35-12
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
//! let bytes = b"hello world";
99
//! let base32 = base32_simd::BASE32;
1010
//!
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======");
1313
//!
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);
1616
//! # }
1717
//! ```
1818
//!
@@ -70,6 +70,9 @@ use crate::encode::encoded_length_unchecked;
7070

7171
use vsimd::tools::{slice_mut, slice_parts};
7272

73+
#[cfg(feature = "alloc")]
74+
use alloc::{string::String, vec::Vec};
75+
7376
const BASE32_CHARSET: &[u8; 32] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
7477
const BASE32HEX_CHARSET: &[u8; 32] = b"0123456789ABCDEFGHIJKLMNOPQRSTUV";
7578

@@ -244,32 +247,52 @@ impl Base32 {
244247
/// Encodes bytes to a base32 string and returns a specified type.
245248
#[inline]
246249
#[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())
249252
}
250253

251254
/// Decodes a base32 string to bytes and returns a specified type.
252255
///
253256
/// # Errors
254257
/// This function returns `Err` if the content of `data` is invalid.
255258
#[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())
258261
}
259262

260263
/// Encodes bytes to a base32 string and appends to a specified type.
261264
#[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);
264267
}
265268

266269
/// Decodes a base32 string to bytes and appends to a specified type.
267270
///
268271
/// # Errors
269272
/// This function returns `Err` if the content of `src` is invalid.
270273
#[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())
273296
}
274297
}
275298

crates/base32-simd/tests/it.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ fn allocation() {
9999
let prefix = "data:;base32,";
100100

101101
let mut encode_buf = prefix.to_owned();
102-
BASE32.encode_append(src.as_bytes(), &mut encode_buf);
102+
BASE32.encode_append(src, &mut encode_buf);
103103

104104
assert_eq!(encode_buf, format!("{prefix}NBSWY3DPO5XXE3DE"));
105105

106106
let mut decode_buf = b"123".to_vec();
107-
let src = encode_buf[prefix.len()..].as_bytes();
107+
let src = &encode_buf[prefix.len()..];
108108
BASE32.decode_append(src, &mut decode_buf).unwrap();
109109

110110
assert_eq!(decode_buf, b"123helloworld");

crates/base64-simd/src/lib.rs

+35-12
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
//! let bytes = b"hello world";
99
//! let base64 = base64_simd::STANDARD;
1010
//!
11-
//! let encoded = base64.encode_type::<String>(bytes);
12-
//! assert_eq!(&*encoded, "aGVsbG8gd29ybGQ=");
11+
//! let encoded = base64.encode_to_string(bytes);
12+
//! assert_eq!(encoded, "aGVsbG8gd29ybGQ=");
1313
//!
14-
//! let decoded = base64.decode_type::<Vec<u8>>(encoded.as_bytes()).unwrap();
15-
//! assert_eq!(&*decoded, bytes);
14+
//! let decoded = base64.decode_to_vec(encoded).unwrap();
15+
//! assert_eq!(decoded, bytes);
1616
//! # }
1717
//! ```
1818
//!
@@ -75,6 +75,9 @@ use crate::encode::encoded_length_unchecked;
7575

7676
use vsimd::tools::{slice_mut, slice_parts};
7777

78+
#[cfg(feature = "alloc")]
79+
use alloc::{string::String, vec::Vec};
80+
7881
const STANDARD_CHARSET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
7982
const URL_SAFE_CHARSET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
8083

@@ -311,32 +314,52 @@ impl Base64 {
311314
/// Encodes bytes to a base64 string and returns a specified type.
312315
#[inline]
313316
#[must_use]
314-
pub fn encode_type<T: FromBase64Encode>(&self, data: &[u8]) -> T {
315-
T::from_base64_encode(self, data)
317+
pub fn encode_type<T: FromBase64Encode>(&self, data: impl AsRef<[u8]>) -> T {
318+
T::from_base64_encode(self, data.as_ref())
316319
}
317320

318321
/// Decodes a base64 string to bytes and returns a specified type.
319322
///
320323
/// # Errors
321324
/// This function returns `Err` if the content of `data` is invalid.
322325
#[inline]
323-
pub fn decode_type<T: FromBase64Decode>(&self, data: &[u8]) -> Result<T, Error> {
324-
T::from_base64_decode(self, data)
326+
pub fn decode_type<T: FromBase64Decode>(&self, data: impl AsRef<[u8]>) -> Result<T, Error> {
327+
T::from_base64_decode(self, data.as_ref())
325328
}
326329

327330
/// Encodes bytes to a base64 string and appends to a specified type.
328331
#[inline]
329-
pub fn encode_append<T: AppendBase64Encode>(&self, src: &[u8], dst: &mut T) {
330-
T::append_base64_encode(self, src, dst);
332+
pub fn encode_append<T: AppendBase64Encode>(&self, src: impl AsRef<[u8]>, dst: &mut T) {
333+
T::append_base64_encode(self, src.as_ref(), dst);
331334
}
332335

333336
/// Decodes a base64 string to bytes and appends to a specified type.
334337
///
335338
/// # Errors
336339
/// This function returns `Err` if the content of `src` is invalid.
337340
#[inline]
338-
pub fn decode_append<T: AppendBase64Decode>(&self, src: &[u8], dst: &mut T) -> Result<(), Error> {
339-
T::append_base64_decode(self, src, dst)
341+
pub fn decode_append<T: AppendBase64Decode>(&self, src: impl AsRef<[u8]>, dst: &mut T) -> Result<(), Error> {
342+
T::append_base64_decode(self, src.as_ref(), dst)
343+
}
344+
345+
/// Encodes bytes to a base64 string.
346+
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
347+
#[cfg(feature = "alloc")]
348+
#[inline]
349+
#[must_use]
350+
pub fn encode_to_string(&self, data: impl AsRef<[u8]>) -> String {
351+
self.encode_type(data)
352+
}
353+
354+
/// Decodes a base64 string to bytes.
355+
///
356+
/// # Errors
357+
/// This function returns `Err` if the content of `data` is invalid.
358+
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
359+
#[cfg(feature = "alloc")]
360+
#[inline]
361+
pub fn decode_to_vec(&self, data: impl AsRef<[u8]>) -> Result<Vec<u8>, Error> {
362+
self.decode_type(data)
340363
}
341364
}
342365

crates/base64-simd/tests/it.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ fn allocation() {
5656
let prefix = "data:;base64,";
5757

5858
let mut encode_buf = prefix.to_owned();
59-
STANDARD.encode_append(src.as_bytes(), &mut encode_buf);
59+
STANDARD.encode_append(src, &mut encode_buf);
6060

6161
assert_eq!(encode_buf, format!("{prefix}aGVsbG93b3JsZA=="));
6262

6363
let mut decode_buf = b"123".to_vec();
64-
let src = encode_buf[prefix.len()..].as_bytes();
64+
let src = &encode_buf[prefix.len()..];
6565
STANDARD.decode_append(src, &mut decode_buf).unwrap();
6666

6767
assert_eq!(decode_buf, b"123helloworld");

crates/hex-simd/src/lib.rs

+35-12
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
//!
1010
//! let bytes = b"Hello world!";
1111
//!
12-
//! let encoded = hex_simd::encode_type::<String>(bytes, AsciiCase::Lower);
13-
//! assert_eq!(&*encoded, "48656c6c6f20776f726c6421");
12+
//! let encoded = hex_simd::encode_to_string(bytes, AsciiCase::Lower);
13+
//! assert_eq!(encoded, "48656c6c6f20776f726c6421");
1414
//!
15-
//! let decoded = hex_simd::decode_type::<Vec<u8>>(encoded.as_bytes()).unwrap();
16-
//! assert_eq!(&*decoded, bytes);
15+
//! let decoded = hex_simd::decode_to_vec(encoded).unwrap();
16+
//! assert_eq!(decoded, bytes);
1717
//! # }
1818
//! ```
1919
//!
@@ -65,6 +65,9 @@ pub use vsimd::ascii::AsciiCase;
6565

6666
use vsimd::tools::{slice_mut, slice_parts};
6767

68+
#[cfg(feature = "alloc")]
69+
use alloc::{string::String, vec::Vec};
70+
6871
/// Calculates the encoded length.
6972
///
7073
/// # Panics
@@ -182,17 +185,17 @@ pub trait FromHexEncode: Sized {
182185
/// Encodes bytes to a hex string and returns a specified type.
183186
#[inline]
184187
#[must_use]
185-
pub fn encode_type<T: FromHexEncode>(data: &[u8], case: AsciiCase) -> T {
186-
T::from_hex_encode(data, case)
188+
pub fn encode_type<T: FromHexEncode>(data: impl AsRef<[u8]>, case: AsciiCase) -> T {
189+
T::from_hex_encode(data.as_ref(), case)
187190
}
188191

189192
/// Decodes a hex string to bytes case-insensitively and returns a specified type.
190193
///
191194
/// # Errors
192195
/// This function returns `Err` if the content of `data` is invalid.
193196
#[inline]
194-
pub fn decode_type<T: FromHexDecode>(data: &[u8]) -> Result<T, Error> {
195-
T::from_hex_decode(data)
197+
pub fn decode_type<T: FromHexDecode>(data: impl AsRef<[u8]>) -> Result<T, Error> {
198+
T::from_hex_decode(data.as_ref())
196199
}
197200

198201
/// Types that can append a hex string.
@@ -212,15 +215,35 @@ pub trait AppendHexDecode: FromHexDecode {
212215

213216
/// Encodes bytes to a hex string and appends to a specified type.
214217
#[inline]
215-
pub fn encode_append<T: AppendHexEncode>(src: &[u8], dst: &mut T, case: AsciiCase) {
216-
T::append_hex_encode(src, dst, case);
218+
pub fn encode_append<T: AppendHexEncode>(src: impl AsRef<[u8]>, dst: &mut T, case: AsciiCase) {
219+
T::append_hex_encode(src.as_ref(), dst, case);
217220
}
218221

219222
/// Decodes a hex string to bytes case-insensitively and appends to a specified type.
220223
///
221224
/// # Errors
222225
/// This function returns `Err` if the content of `src` is invalid.
223226
#[inline]
224-
pub fn decode_append<T: AppendHexDecode>(src: &[u8], dst: &mut T) -> Result<(), Error> {
225-
T::append_hex_decode(src, dst)
227+
pub fn decode_append<T: AppendHexDecode>(src: impl AsRef<[u8]>, dst: &mut T) -> Result<(), Error> {
228+
T::append_hex_decode(src.as_ref(), dst)
229+
}
230+
231+
/// Encodes bytes to a hex string.
232+
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
233+
#[cfg(feature = "alloc")]
234+
#[inline]
235+
#[must_use]
236+
pub fn encode_to_string(data: impl AsRef<[u8]>, case: AsciiCase) -> String {
237+
encode_type(data, case)
238+
}
239+
240+
/// Decodes a hex string to bytes case-insensitively.
241+
///
242+
/// # Errors
243+
/// This function returns `Err` if the content of `data` is invalid.
244+
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
245+
#[cfg(feature = "alloc")]
246+
#[inline]
247+
pub fn decode_to_vec(data: impl AsRef<[u8]>) -> Result<Vec<u8>, Error> {
248+
decode_type(data)
226249
}

crates/hex-simd/tests/tests.rs crates/hex-simd/tests/it.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,26 @@ fn as_str() {
3434
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
3535
fn allocation() {
3636
{
37-
let src = "hello".as_bytes();
37+
let src = "hello";
3838

3939
let ans: String = hex_simd::encode_type(src, AsciiCase::Lower);
4040
assert_eq!(&*ans, "68656c6c6f");
4141

42-
let ans: Vec<u8> = hex_simd::decode_type(ans.as_bytes()).unwrap();
43-
assert_eq!(&*ans, src);
42+
let ans: Vec<u8> = hex_simd::decode_type(ans).unwrap();
43+
assert_eq!(&*ans, src.as_bytes());
4444
}
4545

4646
{
4747
let src = [1, 2, 3];
4848
let prefix = "0x";
4949

5050
let mut encode_buf = prefix.to_owned();
51-
hex_simd::encode_append(&src, &mut encode_buf, AsciiCase::Lower);
51+
hex_simd::encode_append(src, &mut encode_buf, AsciiCase::Lower);
5252

5353
assert_eq!(encode_buf, format!("{prefix}010203"));
5454

5555
let mut decode_buf = b"123".to_vec();
56-
let src = encode_buf[prefix.len()..].as_bytes();
56+
let src = &encode_buf[prefix.len()..];
5757
hex_simd::decode_append(src, &mut decode_buf).unwrap();
5858

5959
assert_eq!(decode_buf, b"123\x01\x02\x03");

0 commit comments

Comments
 (0)