From 2ee1a00030a34a4149f3ea42d350f105c6d405df Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Sat, 16 Sep 2023 09:13:41 -0700 Subject: [PATCH] `no_std` import simplification. It's always OK to use `core` and `alloc` if you're using `std`, and `core` if you're using `alloc`. Therefore, code can be simplified by using `alloc` even if `std` is also being used, and so on. * Make `std` feature depend on `alloc` feature. * Simplify `cfg()`s by making that assumption. * Don't import `std` aliased as `alloc`; use `alloc` directly. These changes do not affect the public API of the crate in any way. --- Cargo.toml | 2 +- src/chunked_encoder.rs | 10 +++++----- src/decode.rs | 8 ++++---- src/encode.rs | 10 +++++----- src/engine/mod.rs | 14 +++++++------- src/engine/naive.rs | 3 +-- src/lib.rs | 8 +++----- 7 files changed, 26 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b5f3c6a..381f615 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ lazy_static = "1.4.0" [features] default = ["std"] alloc = [] -std = [] +std = ["alloc"] [profile.bench] # Useful for better disassembly when using `perf record` and `perf annotate` diff --git a/src/chunked_encoder.rs b/src/chunked_encoder.rs index 69bc745..817b339 100644 --- a/src/chunked_encoder.rs +++ b/src/chunked_encoder.rs @@ -2,9 +2,9 @@ use crate::{ encode::add_padding, engine::{Config, Engine}, }; -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] use alloc::string::String; -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] use core::str; /// The output mechanism for ChunkedEncoder's encoded bytes. @@ -47,19 +47,19 @@ impl<'e, E: Engine + ?Sized> ChunkedEncoder<'e, E> { } // A really simple sink that just appends to a string -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] pub(crate) struct StringSink<'a> { string: &'a mut String, } -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] impl<'a> StringSink<'a> { pub(crate) fn new(s: &mut String) -> StringSink { StringSink { string: s } } } -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] impl<'a> Sink for StringSink<'a> { type Error = (); diff --git a/src/decode.rs b/src/decode.rs index f590cbd..5230fd3 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -1,5 +1,5 @@ use crate::engine::{general_purpose::STANDARD, DecodeEstimate, Engine}; -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] use alloc::vec::Vec; use core::fmt; #[cfg(any(feature = "std", test))] @@ -83,7 +83,7 @@ impl From for DecodeSliceError { /// /// See [Engine::decode]. #[deprecated(since = "0.21.0", note = "Use Engine::decode")] -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] pub fn decode>(input: T) -> Result, DecodeError> { STANDARD.decode(input) } @@ -93,7 +93,7 @@ pub fn decode>(input: T) -> Result, DecodeError> { /// See [Engine::decode]. ///Returns a `Result` containing a `Vec`. #[deprecated(since = "0.21.0", note = "Use Engine::decode")] -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] pub fn decode_engine>( input: T, engine: &E, @@ -104,7 +104,7 @@ pub fn decode_engine>( /// Decode from string reference as octets. /// /// See [Engine::decode_vec]. -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] #[deprecated(since = "0.21.0", note = "Use Engine::decode_vec")] pub fn decode_engine_vec>( input: T, diff --git a/src/encode.rs b/src/encode.rs index 2162d9e..88e6499 100644 --- a/src/encode.rs +++ b/src/encode.rs @@ -1,10 +1,10 @@ -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] use alloc::string::String; use core::fmt; #[cfg(any(feature = "std", test))] use std::error; -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] use crate::engine::general_purpose::STANDARD; use crate::engine::{Config, Engine}; use crate::PAD_BYTE; @@ -14,7 +14,7 @@ use crate::PAD_BYTE; /// See [Engine::encode]. #[allow(unused)] #[deprecated(since = "0.21.0", note = "Use Engine::encode")] -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] pub fn encode>(input: T) -> String { STANDARD.encode(input) } @@ -24,7 +24,7 @@ pub fn encode>(input: T) -> String { /// See [Engine::encode]. #[allow(unused)] #[deprecated(since = "0.21.0", note = "Use Engine::encode")] -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] pub fn encode_engine>(input: T, engine: &E) -> String { engine.encode(input) } @@ -34,7 +34,7 @@ pub fn encode_engine>(input: T, engine: &E) -> String /// See [Engine::encode_string]. #[allow(unused)] #[deprecated(since = "0.21.0", note = "Use Engine::encode_string")] -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] pub fn encode_engine_string>( input: T, output_buf: &mut String, diff --git a/src/engine/mod.rs b/src/engine/mod.rs index e10d66b..0c2d912 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -1,14 +1,14 @@ //! Provides the [Engine] abstraction and out of the box implementations. -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] use crate::chunked_encoder; use crate::{ encode::{encode_with_padding, EncodeSliceError}, encoded_len, DecodeError, DecodeSliceError, }; -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] use alloc::vec::Vec; -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] use alloc::{string::String, vec}; pub mod general_purpose; @@ -113,7 +113,7 @@ pub trait Engine: Send + Sync { /// engine::GeneralPurpose::new(&alphabet::URL_SAFE, general_purpose::NO_PAD); /// /// let b64_url = CUSTOM_ENGINE.encode(b"hello internet~"); - #[cfg(any(feature = "alloc", feature = "std", test))] + #[cfg(any(feature = "alloc", test))] #[inline] fn encode>(&self, input: T) -> String { fn inner(engine: &E, input_bytes: &[u8]) -> String @@ -153,7 +153,7 @@ pub trait Engine: Send + Sync { /// println!("{}", buf); /// } /// ``` - #[cfg(any(feature = "alloc", feature = "std", test))] + #[cfg(any(feature = "alloc", test))] #[inline] fn encode_string>(&self, input: T, output_buf: &mut String) { fn inner(engine: &E, input_bytes: &[u8], output_buf: &mut String) @@ -241,7 +241,7 @@ pub trait Engine: Send + Sync { /// .decode("aGVsbG8gaW50ZXJuZXR-Cg").unwrap(); /// println!("{:?}", bytes_url); /// ``` - #[cfg(any(feature = "alloc", feature = "std", test))] + #[cfg(any(feature = "alloc", test))] #[inline] fn decode>(&self, input: T) -> Result, DecodeError> { fn inner(engine: &E, input_bytes: &[u8]) -> Result, DecodeError> @@ -293,7 +293,7 @@ pub trait Engine: Send + Sync { /// println!("{:?}", buffer); /// } /// ``` - #[cfg(any(feature = "alloc", feature = "std", test))] + #[cfg(any(feature = "alloc", test))] #[inline] fn decode_vec>( &self, diff --git a/src/engine/naive.rs b/src/engine/naive.rs index 42b6085..6a50cbe 100644 --- a/src/engine/naive.rs +++ b/src/engine/naive.rs @@ -6,8 +6,7 @@ use crate::{ }, DecodeError, PAD_BYTE, }; -use alloc::ops::BitOr; -use std::ops::{BitAnd, Shl, Shr}; +use std::ops::{BitAnd, BitOr, Shl, Shr}; /// Comparatively simple implementation that can be used as something to compare against in tests pub struct Naive { diff --git a/src/lib.rs b/src/lib.rs index cc9d628..60e9a72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -136,10 +136,8 @@ #![allow(clippy::single_component_path_imports)] #![cfg_attr(not(any(feature = "std", test)), no_std)] -#[cfg(all(feature = "alloc", not(any(feature = "std", test))))] +#[cfg(any(feature = "alloc", test))] extern crate alloc; -#[cfg(any(feature = "std", test))] -extern crate std as alloc; // has to be included at top level because of the way rstest_reuse defines its macros #[cfg(test)] @@ -159,14 +157,14 @@ pub mod alphabet; mod encode; #[allow(deprecated)] -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] pub use crate::encode::{encode, encode_engine, encode_engine_string}; #[allow(deprecated)] pub use crate::encode::{encode_engine_slice, encoded_len, EncodeSliceError}; mod decode; #[allow(deprecated)] -#[cfg(any(feature = "alloc", feature = "std", test))] +#[cfg(any(feature = "alloc", test))] pub use crate::decode::{decode, decode_engine, decode_engine_vec}; #[allow(deprecated)] pub use crate::decode::{decode_engine_slice, decoded_len_estimate, DecodeError, DecodeSliceError};