Skip to content

Commit 888596e

Browse files
committed
Make AsciiSet consts and fields values
Refs take 8 bytes, whereas the values are only 16 bytes, so there is not a huge benefit to using references rather than values. PercentEncoding is changed to store the AsciiSet as a value, and the functions that take AsciiSet now take Into<AsciiSet> instead of &'static AsciiSet. This allows existing code to continue to work without modification. The AsciiSet consts (CONTROLS and NON_ALPHANUMERIC) are also changed to be values, which is a breaking change, but will only affect code that attempts to dereference them. Discussion about the rationale for this is change is at <servo#970 (comment)>
1 parent 710e1e7 commit 888596e

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

percent_encoding/src/ascii_set.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use core::{mem, ops};
2424
/// /// https://url.spec.whatwg.org/#fragment-percent-encode-set
2525
/// const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
2626
/// ```
27-
#[derive(Debug, PartialEq, Eq)]
27+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2828
pub struct AsciiSet {
2929
mask: [Chunk; ASCII_RANGE_LEN / BITS_PER_CHUNK],
3030
}
@@ -83,6 +83,12 @@ impl AsciiSet {
8383
}
8484
}
8585

86+
impl From<&AsciiSet> for AsciiSet {
87+
fn from(set: &AsciiSet) -> Self {
88+
*set
89+
}
90+
}
91+
8692
impl ops::Add for AsciiSet {
8793
type Output = Self;
8894

@@ -104,7 +110,7 @@ impl ops::Not for AsciiSet {
104110
/// Note that this includes the newline and tab characters, but not the space 0x20.
105111
///
106112
/// <https://url.spec.whatwg.org/#c0-control-percent-encode-set>
107-
pub const CONTROLS: &AsciiSet = &AsciiSet {
113+
pub const CONTROLS: AsciiSet = AsciiSet {
108114
mask: [
109115
!0_u32, // C0: 0x00 to 0x1F (32 bits set)
110116
0,
@@ -134,7 +140,7 @@ static_assert! {
134140
/// Everything that is not an ASCII letter or digit.
135141
///
136142
/// This is probably more eager than necessary in any context.
137-
pub const NON_ALPHANUMERIC: &AsciiSet = &CONTROLS
143+
pub const NON_ALPHANUMERIC: AsciiSet = CONTROLS
138144
.add(b' ')
139145
.add(b'!')
140146
.add(b'"')

percent_encoding/src/lib.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
//! use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
3333
//!
3434
//! /// https://url.spec.whatwg.org/#fragment-percent-encode-set
35-
//! const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
35+
//! const FRAGMENT: AsciiSet = CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
3636
//!
3737
//! assert_eq!(utf8_percent_encode("foo <bar>", FRAGMENT).to_string(), "foo%20%3Cbar%3E");
3838
//! ```
@@ -114,10 +114,10 @@ pub fn percent_encode_byte(byte: u8) -> &'static str {
114114
/// assert_eq!(percent_encode(b"foo bar?", NON_ALPHANUMERIC).to_string(), "foo%20bar%3F");
115115
/// ```
116116
#[inline]
117-
pub fn percent_encode<'a>(input: &'a [u8], ascii_set: &'static AsciiSet) -> PercentEncode<'a> {
117+
pub fn percent_encode<T: Into<AsciiSet>>(input: &[u8], ascii_set: T) -> PercentEncode<'_> {
118118
PercentEncode {
119119
bytes: input,
120-
ascii_set,
120+
ascii_set: ascii_set.into(),
121121
}
122122
}
123123

@@ -133,15 +133,15 @@ pub fn percent_encode<'a>(input: &'a [u8], ascii_set: &'static AsciiSet) -> Perc
133133
/// assert_eq!(utf8_percent_encode("foo bar?", NON_ALPHANUMERIC).to_string(), "foo%20bar%3F");
134134
/// ```
135135
#[inline]
136-
pub fn utf8_percent_encode<'a>(input: &'a str, ascii_set: &'static AsciiSet) -> PercentEncode<'a> {
136+
pub fn utf8_percent_encode<T: Into<AsciiSet>>(input: &str, ascii_set: T) -> PercentEncode<'_> {
137137
percent_encode(input.as_bytes(), ascii_set)
138138
}
139139

140140
/// The return type of [`percent_encode`] and [`utf8_percent_encode`].
141141
#[derive(Debug, Clone, PartialEq, Eq)]
142142
pub struct PercentEncode<'a> {
143143
bytes: &'a [u8],
144-
ascii_set: &'static AsciiSet,
144+
ascii_set: AsciiSet,
145145
}
146146

147147
impl<'a> Iterator for PercentEncode<'a> {
@@ -379,10 +379,17 @@ mod tests {
379379

380380
#[test]
381381
fn percent_encode_accepts_ascii_set_ref() {
382+
#[allow(clippy::needless_borrows_for_generic_args)] // tests prior behavior
382383
let encoded = percent_encode(b"foo bar?", &AsciiSet::EMPTY);
383384
assert_eq!(encoded.collect::<String>(), "foo bar?");
384385
}
385386

387+
#[test]
388+
fn percent_encode_accepts_value() {
389+
let encoded = percent_encode(b"foo bar?", AsciiSet::EMPTY);
390+
assert_eq!(encoded.collect::<String>(), "foo bar?");
391+
}
392+
386393
#[test]
387394
fn percent_encode_collect() {
388395
let encoded = percent_encode(b"foo bar?", NON_ALPHANUMERIC);
@@ -406,10 +413,17 @@ mod tests {
406413

407414
#[test]
408415
fn utf8_percent_encode_accepts_ascii_set_ref() {
416+
#[allow(clippy::needless_borrows_for_generic_args)] // tests prior behavior
409417
let encoded = super::utf8_percent_encode("foo bar?", &AsciiSet::EMPTY);
410418
assert_eq!(encoded.collect::<String>(), "foo bar?");
411419
}
412420

421+
#[test]
422+
fn utf8_percent_encode_accepts_value() {
423+
let encoded = super::utf8_percent_encode("foo bar?", AsciiSet::EMPTY);
424+
assert_eq!(encoded.collect::<String>(), "foo bar?");
425+
}
426+
413427
#[test]
414428
fn utf8_percent_encode() {
415429
assert_eq!(

0 commit comments

Comments
 (0)