Skip to content

Commit 2fc9dfb

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 previously accepted a reference now accept a value. This is a breaking change for users who were passing a reference to AsciiSet to the functions in the public API. The AsciiSet consts (CONTROLS, NON_ALPHANUMERIC, etc.) are also changed to be values. This is an alternative to the non-breaking change in <servo#976> Discussion about the rationale for this is change is at <servo#970 (comment)>
1 parent 710e1e7 commit 2fc9dfb

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

percent_encoding/src/ascii_set.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use core::{mem, ops};
2222
/// use percent_encoding::{AsciiSet, CONTROLS};
2323
///
2424
/// /// https://url.spec.whatwg.org/#fragment-percent-encode-set
25-
/// const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
25+
/// 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
}
@@ -104,7 +104,7 @@ impl ops::Not for AsciiSet {
104104
/// Note that this includes the newline and tab characters, but not the space 0x20.
105105
///
106106
/// <https://url.spec.whatwg.org/#c0-control-percent-encode-set>
107-
pub const CONTROLS: &AsciiSet = &AsciiSet {
107+
pub const CONTROLS: AsciiSet = AsciiSet {
108108
mask: [
109109
!0_u32, // C0: 0x00 to 0x1F (32 bits set)
110110
0,
@@ -134,7 +134,7 @@ static_assert! {
134134
/// Everything that is not an ASCII letter or digit.
135135
///
136136
/// This is probably more eager than necessary in any context.
137-
pub const NON_ALPHANUMERIC: &AsciiSet = &CONTROLS
137+
pub const NON_ALPHANUMERIC: AsciiSet = CONTROLS
138138
.add(b' ')
139139
.add(b'!')
140140
.add(b'"')

percent_encoding/src/lib.rs

+8-8
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,7 +114,7 @@ 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(input: &[u8], ascii_set: AsciiSet) -> PercentEncode<'_> {
118118
PercentEncode {
119119
bytes: input,
120120
ascii_set,
@@ -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(input: &str, ascii_set: AsciiSet) -> 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> {
@@ -378,8 +378,8 @@ mod tests {
378378
}
379379

380380
#[test]
381-
fn percent_encode_accepts_ascii_set_ref() {
382-
let encoded = percent_encode(b"foo bar?", &AsciiSet::EMPTY);
381+
fn percent_encode_accepts_value() {
382+
let encoded = percent_encode(b"foo bar?", AsciiSet::EMPTY);
383383
assert_eq!(encoded.collect::<String>(), "foo bar?");
384384
}
385385

@@ -405,8 +405,8 @@ mod tests {
405405
}
406406

407407
#[test]
408-
fn utf8_percent_encode_accepts_ascii_set_ref() {
409-
let encoded = super::utf8_percent_encode("foo bar?", &AsciiSet::EMPTY);
408+
fn utf8_percent_encode_accepts_value() {
409+
let encoded = super::utf8_percent_encode("foo bar?", AsciiSet::EMPTY);
410410
assert_eq!(encoded.collect::<String>(), "foo bar?");
411411
}
412412

url/src/parser.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ use form_urlencoded::EncodingOverride;
1616
use percent_encoding::{percent_encode, utf8_percent_encode, AsciiSet, CONTROLS};
1717

1818
/// https://url.spec.whatwg.org/#fragment-percent-encode-set
19-
const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
19+
const FRAGMENT: AsciiSet = CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
2020

2121
/// https://url.spec.whatwg.org/#path-percent-encode-set
22-
const PATH: &AsciiSet = &FRAGMENT.add(b'#').add(b'?').add(b'{').add(b'}');
22+
const PATH: AsciiSet = FRAGMENT.add(b'#').add(b'?').add(b'{').add(b'}');
2323

2424
/// https://url.spec.whatwg.org/#userinfo-percent-encode-set
25-
pub(crate) const USERINFO: &AsciiSet = &PATH
25+
pub(crate) const USERINFO: AsciiSet = PATH
2626
.add(b'/')
2727
.add(b':')
2828
.add(b';')
@@ -34,15 +34,15 @@ pub(crate) const USERINFO: &AsciiSet = &PATH
3434
.add(b'^')
3535
.add(b'|');
3636

37-
pub(crate) const PATH_SEGMENT: &AsciiSet = &PATH.add(b'/').add(b'%');
37+
pub(crate) const PATH_SEGMENT: AsciiSet = PATH.add(b'/').add(b'%');
3838

3939
// The backslash (\) character is treated as a path separator in special URLs
4040
// so it needs to be additionally escaped in that case.
41-
pub(crate) const SPECIAL_PATH_SEGMENT: &AsciiSet = &PATH_SEGMENT.add(b'\\');
41+
pub(crate) const SPECIAL_PATH_SEGMENT: AsciiSet = PATH_SEGMENT.add(b'\\');
4242

4343
// https://url.spec.whatwg.org/#query-state
44-
const QUERY: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'#').add(b'<').add(b'>');
45-
const SPECIAL_QUERY: &AsciiSet = &QUERY.add(b'\'');
44+
const QUERY: AsciiSet = CONTROLS.add(b' ').add(b'"').add(b'#').add(b'<').add(b'>');
45+
const SPECIAL_QUERY: AsciiSet = QUERY.add(b'\'');
4646

4747
pub type ParseResult<T> = Result<T, ParseError>;
4848

0 commit comments

Comments
 (0)