Skip to content

Commit fd34ccb

Browse files
committed
Add alloc dependencies behind an optional alloc feature
1 parent 5fac16f commit fd34ccb

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

percent_encoding/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ description = "Percent encoding and decoding"
66
repository = "https://github.com/servo/rust-url/"
77
license = "MIT/Apache-2.0"
88
edition = "2018"
9+
10+
[features]
11+
default = ["alloc"]
12+
alloc = []

percent_encoding/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@
3838
//! ```
3939
4040
#![no_std]
41+
#[cfg(feature = "alloc")]
4142
extern crate alloc;
4243

44+
use core::{fmt, mem, slice, str};
45+
#[cfg(feature = "alloc")]
4346
use alloc::{
4447
borrow::{Cow, ToOwned},
4548
string::String,
4649
vec::Vec,
4750
};
48-
use core::{fmt, mem, slice, str};
4951

5052
/// Represents a set of characters or bytes in the ASCII range.
5153
///
@@ -294,6 +296,7 @@ impl<'a> fmt::Display for PercentEncode<'a> {
294296
}
295297
}
296298

299+
#[cfg(feature = "alloc")]
297300
impl<'a> From<PercentEncode<'a>> for Cow<'a, str> {
298301
fn from(mut iter: PercentEncode<'a>) -> Self {
299302
match iter.next() {
@@ -379,6 +382,7 @@ impl<'a> Iterator for PercentDecode<'a> {
379382
}
380383
}
381384

385+
#[cfg(feature = "alloc")]
382386
impl<'a> From<PercentDecode<'a>> for Cow<'a, [u8]> {
383387
fn from(iter: PercentDecode<'a>) -> Self {
384388
match iter.if_any() {
@@ -390,6 +394,7 @@ impl<'a> From<PercentDecode<'a>> for Cow<'a, [u8]> {
390394

391395
impl<'a> PercentDecode<'a> {
392396
/// If the percent-decoding is different from the input, return it as a new bytes vector.
397+
#[cfg(feature = "alloc")]
393398
fn if_any(&self) -> Option<Vec<u8>> {
394399
let mut bytes_iter = self.bytes.clone();
395400
while bytes_iter.any(|&b| b == b'%') {
@@ -409,6 +414,7 @@ impl<'a> PercentDecode<'a> {
409414
/// Decode the result of percent-decoding as UTF-8.
410415
///
411416
/// This is return `Err` when the percent-decoded bytes are not well-formed in UTF-8.
417+
#[cfg(feature = "alloc")]
412418
pub fn decode_utf8(self) -> Result<Cow<'a, str>, str::Utf8Error> {
413419
match self.clone().into() {
414420
Cow::Borrowed(bytes) => match str::from_utf8(bytes) {
@@ -426,11 +432,13 @@ impl<'a> PercentDecode<'a> {
426432
///
427433
/// Invalid UTF-8 percent-encoded byte sequences will be replaced � U+FFFD,
428434
/// the replacement character.
435+
#[cfg(feature = "alloc")]
429436
pub fn decode_utf8_lossy(self) -> Cow<'a, str> {
430437
decode_utf8_lossy(self.clone().into())
431438
}
432439
}
433440

441+
#[cfg(feature = "alloc")]
434442
fn decode_utf8_lossy(input: Cow<'_, [u8]>) -> Cow<'_, str> {
435443
// Note: This function is duplicated in `form_urlencoded/src/query_encoding.rs`.
436444
match input {

0 commit comments

Comments
 (0)