Skip to content

Commit

Permalink
no_std import simplification.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kpreid committed Sep 16, 2023
1 parent b88fd5b commit 2ee1a00
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
10 changes: 5 additions & 5 deletions src/chunked_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = ();

Expand Down
8 changes: 4 additions & 4 deletions src/decode.rs
Original file line number Diff line number Diff line change
@@ -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))]
Expand Down Expand Up @@ -83,7 +83,7 @@ impl From<DecodeError> 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<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, DecodeError> {
STANDARD.decode(input)
}
Expand All @@ -93,7 +93,7 @@ pub fn decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>, DecodeError> {
/// See [Engine::decode].
///Returns a `Result` containing a `Vec<u8>`.
#[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<E: Engine, T: AsRef<[u8]>>(
input: T,
engine: &E,
Expand All @@ -104,7 +104,7 @@ pub fn decode_engine<E: Engine, T: AsRef<[u8]>>(
/// 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<E: Engine, T: AsRef<[u8]>>(
input: T,
Expand Down
10 changes: 5 additions & 5 deletions src/encode.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<T: AsRef<[u8]>>(input: T) -> String {
STANDARD.encode(input)
}
Expand All @@ -24,7 +24,7 @@ pub fn encode<T: AsRef<[u8]>>(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<E: Engine, T: AsRef<[u8]>>(input: T, engine: &E) -> String {
engine.encode(input)
}
Expand All @@ -34,7 +34,7 @@ pub fn encode_engine<E: Engine, T: AsRef<[u8]>>(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<E: Engine, T: AsRef<[u8]>>(
input: T,
output_buf: &mut String,
Expand Down
14 changes: 7 additions & 7 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<T: AsRef<[u8]>>(&self, input: T) -> String {
fn inner<E>(engine: &E, input_bytes: &[u8]) -> String
Expand Down Expand Up @@ -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<T: AsRef<[u8]>>(&self, input: T, output_buf: &mut String) {
fn inner<E>(engine: &E, input_bytes: &[u8], output_buf: &mut String)
Expand Down Expand Up @@ -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<T: AsRef<[u8]>>(&self, input: T) -> Result<Vec<u8>, DecodeError> {
fn inner<E>(engine: &E, input_bytes: &[u8]) -> Result<Vec<u8>, DecodeError>
Expand Down Expand Up @@ -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<T: AsRef<[u8]>>(
&self,
Expand Down
3 changes: 1 addition & 2 deletions src/engine/naive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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};
Expand Down

0 comments on commit 2ee1a00

Please sign in to comment.