Skip to content

Commit 5629ac7

Browse files
authored
Merge pull request #759 from Mingun/const
Make `const` as many functions as possible
2 parents d66df32 + 4543ba6 commit 5629ac7

16 files changed

+175
-97
lines changed

Changelog.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,37 @@
2222
### Misc Changes
2323

2424
- [#650]: Change the type of `Event::PI` to a new dedicated `BytesPI` type.
25+
- [#759]: Make `const` as much functions as possible:
26+
- `resolve_html5_entity()`
27+
- `resolve_predefined_entity()`
28+
- `resolve_xml_entity()`
29+
- `Attr::key()`
30+
- `Attr::value()`
31+
- `Attributes::html()`
32+
- `Attributes::new()`
33+
- `BytesDecl::from_start()`
34+
- `Decoder::encoding()`
35+
- `Deserializer::get_ref()`
36+
- `IoReader::get_ref()`
37+
- `LocalName::into_inner()`
38+
- `Namespace::into_inner()`
39+
- `NsReader::config()`
40+
- `NsReader::prefixes()`
41+
- `Prefix::into_inner()`
42+
- `QName::into_inner()`
43+
- `Reader::buffer_position()`
44+
- `Reader::config()`
45+
- `Reader::decoder()`
46+
- `Reader::error_position()`
47+
- `Reader::get_ref()`
48+
- `SliceReader::get_ref()`
49+
- `Writer::get_ref()`
50+
- `Writer::new()`
2551

2652
[#650]: https://github.com/tafia/quick-xml/issues/650
2753
[#755]: https://github.com/tafia/quick-xml/pull/755
2854
[#758]: https://github.com/tafia/quick-xml/pull/758
55+
[#759]: https://github.com/tafia/quick-xml/pull/759
2956

3057

3158
## 0.32.0 -- 2024-06-10

src/de/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,7 +2155,7 @@ impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
21552155
}
21562156

21572157
/// Returns `true` if all events was consumed
2158-
fn is_empty(&self) -> bool {
2158+
const fn is_empty(&self) -> bool {
21592159
matches!(self.lookahead, Ok(PayloadEvent::Eof))
21602160
}
21612161

@@ -2166,7 +2166,7 @@ impl<'i, R: XmlRead<'i>, E: EntityResolver> XmlReader<'i, R, E> {
21662166
}
21672167

21682168
#[inline(always)]
2169-
fn need_trim_end(&self) -> bool {
2169+
const fn need_trim_end(&self) -> bool {
21702170
// If next event is a text or CDATA, we should not trim trailing spaces
21712171
!matches!(
21722172
self.lookahead,
@@ -2464,7 +2464,7 @@ where
24642464
/// assert_eq!(reader.error_position(), 28);
24652465
/// assert_eq!(reader.buffer_position(), 41);
24662466
/// ```
2467-
pub fn get_ref(&self) -> &R {
2467+
pub const fn get_ref(&self) -> &R {
24682468
&self.reader.reader
24692469
}
24702470

@@ -3127,7 +3127,7 @@ impl<R: BufRead> IoReader<R> {
31273127
/// assert_eq!(reader.error_position(), 28);
31283128
/// assert_eq!(reader.buffer_position(), 41);
31293129
/// ```
3130-
pub fn get_ref(&self) -> &Reader<R> {
3130+
pub const fn get_ref(&self) -> &Reader<R> {
31313131
&self.reader
31323132
}
31333133
}
@@ -3194,7 +3194,7 @@ impl<'de> SliceReader<'de> {
31943194
/// assert_eq!(reader.error_position(), 28);
31953195
/// assert_eq!(reader.buffer_position(), 41);
31963196
/// ```
3197-
pub fn get_ref(&self) -> &Reader<&'de [u8]> {
3197+
pub const fn get_ref(&self) -> &Reader<&'de [u8]> {
31983198
&self.reader
31993199
}
32003200
}

src/de/simple_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ impl<'de, 'a> SimpleTypeDeserializer<'de, 'a> {
551551

552552
/// Constructor for tests
553553
#[inline]
554-
fn new(content: CowRef<'de, 'a, [u8]>, escaped: bool, decoder: Decoder) -> Self {
554+
const fn new(content: CowRef<'de, 'a, [u8]>, escaped: bool, decoder: Decoder) -> Self {
555555
Self {
556556
content,
557557
escaped,

src/encoding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Decoder {
6262
///
6363
/// [`decode`]: Self::decode
6464
#[cfg(feature = "encoding")]
65-
pub fn encoding(&self) -> &'static Encoding {
65+
pub const fn encoding(&self) -> &'static Encoding {
6666
self.encoding
6767
}
6868

src/escape.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ where
284284
/// Behaves like [`resolve_xml_entity`] if feature is not enabled and as
285285
/// [`resolve_html5_entity`] if enabled.
286286
#[inline]
287-
pub fn resolve_predefined_entity(entity: &str) -> Option<&'static str> {
287+
pub const fn resolve_predefined_entity(entity: &str) -> Option<&'static str> {
288288
#[cfg(not(feature = "escape-html"))]
289289
{
290290
resolve_xml_entity(entity)
@@ -314,7 +314,7 @@ pub fn resolve_predefined_entity(entity: &str) -> Option<&'static str> {
314314
/// ```
315315
///
316316
/// [specification]: https://www.w3.org/TR/xml11/#sec-predefined-ent
317-
pub fn resolve_xml_entity(entity: &str) -> Option<&'static str> {
317+
pub const fn resolve_xml_entity(entity: &str) -> Option<&'static str> {
318318
// match over strings are not allowed in const functions
319319
let s = match entity.as_bytes() {
320320
b"lt" => "<",
@@ -328,7 +328,7 @@ pub fn resolve_xml_entity(entity: &str) -> Option<&'static str> {
328328
}
329329

330330
/// Resolves all HTML5 entities. For complete list see <https://dev.w3.org/html5/html-author/charref>.
331-
pub fn resolve_html5_entity(entity: &str) -> Option<&'static str> {
331+
pub const fn resolve_html5_entity(entity: &str) -> Option<&'static str> {
332332
// imported from https://dev.w3.org/html5/html-author/charref
333333
// match over strings are not allowed in const functions
334334
//TODO: automate up-to-dating using https://html.spec.whatwg.org/entities.json

src/events/attributes.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use crate::errors::Result as XmlResult;
66
use crate::escape::{escape, resolve_predefined_entity, unescape_with};
77
use crate::name::QName;
8-
use crate::reader::{is_whitespace, Reader};
9-
use crate::utils::{write_byte_string, write_cow_string, Bytes};
8+
use crate::reader::Reader;
9+
use crate::utils::{is_whitespace, write_byte_string, write_cow_string, Bytes};
1010
use std::fmt::{self, Debug, Display, Formatter};
1111
use std::iter::FusedIterator;
1212
use std::{borrow::Cow, ops::Range};
@@ -195,20 +195,20 @@ pub struct Attributes<'a> {
195195
impl<'a> Attributes<'a> {
196196
/// Internal constructor, used by `BytesStart`. Supplies data in reader's encoding
197197
#[inline]
198-
pub(crate) fn wrap(buf: &'a [u8], pos: usize, html: bool) -> Self {
198+
pub(crate) const fn wrap(buf: &'a [u8], pos: usize, html: bool) -> Self {
199199
Self {
200200
bytes: buf,
201201
state: IterState::new(pos, html),
202202
}
203203
}
204204

205205
/// Creates a new attribute iterator from a buffer.
206-
pub fn new(buf: &'a str, pos: usize) -> Self {
206+
pub const fn new(buf: &'a str, pos: usize) -> Self {
207207
Self::wrap(buf.as_bytes(), pos, false)
208208
}
209209

210210
/// Creates a new attribute iterator from a buffer, allowing HTML attribute syntax.
211-
pub fn html(buf: &'a str, pos: usize) -> Self {
211+
pub const fn html(buf: &'a str, pos: usize) -> Self {
212212
Self::wrap(buf.as_bytes(), pos, true)
213213
}
214214

@@ -416,7 +416,7 @@ impl<T> Attr<T> {
416416
impl<'a> Attr<&'a [u8]> {
417417
/// Returns the key value
418418
#[inline]
419-
pub fn key(&self) -> QName<'a> {
419+
pub const fn key(&self) -> QName<'a> {
420420
QName(match self {
421421
Attr::DoubleQ(key, _) => key,
422422
Attr::SingleQ(key, _) => key,
@@ -429,7 +429,7 @@ impl<'a> Attr<&'a [u8]> {
429429
///
430430
/// [HTML specification]: https://www.w3.org/TR/2012/WD-html-markup-20120329/syntax.html#syntax-attr-empty
431431
#[inline]
432-
pub fn value(&self) -> &'a [u8] {
432+
pub const fn value(&self) -> &'a [u8] {
433433
match self {
434434
Attr::DoubleQ(_, value) => value,
435435
Attr::SingleQ(_, value) => value,
@@ -518,7 +518,7 @@ pub(crate) struct IterState {
518518
}
519519

520520
impl IterState {
521-
pub fn new(offset: usize, html: bool) -> Self {
521+
pub const fn new(offset: usize, html: bool) -> Self {
522522
Self {
523523
state: State::Next(offset),
524524
html,

src/events/mod.rs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub mod attributes;
4141
use encoding_rs::Encoding;
4242
use std::borrow::Cow;
4343
use std::fmt::{self, Debug, Formatter};
44+
use std::mem::replace;
4445
use std::ops::Deref;
4546
use std::str::from_utf8;
4647

@@ -50,12 +51,10 @@ use crate::escape::{
5051
escape, minimal_escape, partial_escape, resolve_predefined_entity, unescape_with,
5152
};
5253
use crate::name::{LocalName, QName};
53-
use crate::reader::{is_whitespace, name_len};
54-
use crate::utils::write_cow_string;
5554
#[cfg(feature = "serialize")]
5655
use crate::utils::CowRef;
56+
use crate::utils::{name_len, trim_xml_end, trim_xml_start, write_cow_string};
5757
use attributes::{Attribute, Attributes};
58-
use std::mem::replace;
5958

6059
/// Opening tag data (`Event::Start`), with optional attributes.
6160
///
@@ -78,7 +77,7 @@ pub struct BytesStart<'a> {
7877
impl<'a> BytesStart<'a> {
7978
/// Internal constructor, used by `Reader`. Supplies data in reader's encoding
8079
#[inline]
81-
pub(crate) fn wrap(content: &'a [u8], name_len: usize) -> Self {
80+
pub(crate) const fn wrap(content: &'a [u8], name_len: usize) -> Self {
8281
BytesStart {
8382
buf: Cow::Borrowed(content),
8483
name_len,
@@ -406,7 +405,7 @@ impl<'a> BytesDecl<'a> {
406405
}
407406

408407
/// Creates a `BytesDecl` from a `BytesStart`
409-
pub fn from_start(start: BytesStart<'a>) -> Self {
408+
pub const fn from_start(start: BytesStart<'a>) -> Self {
410409
Self { content: start }
411410
}
412411

@@ -621,7 +620,7 @@ pub struct BytesEnd<'a> {
621620
impl<'a> BytesEnd<'a> {
622621
/// Internal constructor, used by `Reader`. Supplies data in reader's encoding
623622
#[inline]
624-
pub(crate) fn wrap(name: Cow<'a, [u8]>) -> Self {
623+
pub(crate) const fn wrap(name: Cow<'a, [u8]>) -> Self {
625624
BytesEnd { name }
626625
}
627626

@@ -1019,7 +1018,7 @@ pub struct BytesPI<'a> {
10191018
impl<'a> BytesPI<'a> {
10201019
/// Creates a new `BytesPI` from a byte sequence in the specified encoding.
10211020
#[inline]
1022-
pub(crate) fn wrap(content: &'a [u8], target_len: usize) -> Self {
1021+
pub(crate) const fn wrap(content: &'a [u8], target_len: usize) -> Self {
10231022
Self {
10241023
content: BytesStart::wrap(content, target_len),
10251024
}
@@ -1257,38 +1256,6 @@ fn str_cow_to_bytes<'a, C: Into<Cow<'a, str>>>(content: C) -> Cow<'a, [u8]> {
12571256
}
12581257
}
12591258

1260-
/// Returns a byte slice with leading XML whitespace bytes removed.
1261-
///
1262-
/// 'Whitespace' refers to the definition used by [`is_whitespace`].
1263-
const fn trim_xml_start(mut bytes: &[u8]) -> &[u8] {
1264-
// Note: A pattern matching based approach (instead of indexing) allows
1265-
// making the function const.
1266-
while let [first, rest @ ..] = bytes {
1267-
if is_whitespace(*first) {
1268-
bytes = rest;
1269-
} else {
1270-
break;
1271-
}
1272-
}
1273-
bytes
1274-
}
1275-
1276-
/// Returns a byte slice with trailing XML whitespace bytes removed.
1277-
///
1278-
/// 'Whitespace' refers to the definition used by [`is_whitespace`].
1279-
const fn trim_xml_end(mut bytes: &[u8]) -> &[u8] {
1280-
// Note: A pattern matching based approach (instead of indexing) allows
1281-
// making the function const.
1282-
while let [rest @ .., last] = bytes {
1283-
if is_whitespace(*last) {
1284-
bytes = rest;
1285-
} else {
1286-
break;
1287-
}
1288-
}
1289-
bytes
1290-
}
1291-
12921259
fn trim_cow<'a, F>(value: Cow<'a, [u8]>, trim: F) -> Cow<'a, [u8]>
12931260
where
12941261
F: FnOnce(&[u8]) -> &[u8],

src/name.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct QName<'a>(pub &'a [u8]);
2020
impl<'a> QName<'a> {
2121
/// Converts this name to an internal slice representation.
2222
#[inline(always)]
23-
pub fn into_inner(self) -> &'a [u8] {
23+
pub const fn into_inner(self) -> &'a [u8] {
2424
self.0
2525
}
2626

@@ -137,7 +137,7 @@ pub struct LocalName<'a>(&'a [u8]);
137137
impl<'a> LocalName<'a> {
138138
/// Converts this name to an internal slice representation.
139139
#[inline(always)]
140-
pub fn into_inner(self) -> &'a [u8] {
140+
pub const fn into_inner(self) -> &'a [u8] {
141141
self.0
142142
}
143143
}
@@ -187,7 +187,7 @@ pub struct Prefix<'a>(&'a [u8]);
187187
impl<'a> Prefix<'a> {
188188
/// Extracts internal slice
189189
#[inline(always)]
190-
pub fn into_inner(self) -> &'a [u8] {
190+
pub const fn into_inner(self) -> &'a [u8] {
191191
self.0
192192
}
193193
}
@@ -252,7 +252,7 @@ impl<'a> Namespace<'a> {
252252
/// [non-normalized]: https://www.w3.org/TR/xml11/#AVNormalize
253253
/// [IRI reference]: https://datatracker.ietf.org/doc/html/rfc3987
254254
#[inline(always)]
255-
pub fn into_inner(self) -> &'a [u8] {
255+
pub const fn into_inner(self) -> &'a [u8] {
256256
self.0
257257
}
258258
//TODO: implement value normalization and use it when comparing namespaces
@@ -618,7 +618,7 @@ impl NamespaceResolver {
618618
}
619619

620620
#[inline]
621-
pub fn iter(&self) -> PrefixIter {
621+
pub const fn iter(&self) -> PrefixIter {
622622
PrefixIter {
623623
resolver: self,
624624
// We initialize the cursor to 2 to skip the two default namespaces xml: and xmlns:

src/reader/async_tokio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use crate::events::Event;
99
use crate::name::{QName, ResolveResult};
1010
use crate::reader::buffered_reader::impl_buffered_source;
1111
use crate::reader::{
12-
is_whitespace, BangType, ElementParser, NsReader, ParseState, Parser, PiParser, ReadTextResult,
13-
Reader, Span,
12+
BangType, ElementParser, NsReader, ParseState, Parser, PiParser, ReadTextResult, Reader, Span,
1413
};
14+
use crate::utils::is_whitespace;
1515

1616
/// A struct for read XML asynchronously from an [`AsyncBufRead`].
1717
///

src/reader/buffered_reader.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use std::path::Path;
88
use crate::errors::{Error, Result};
99
use crate::events::Event;
1010
use crate::name::QName;
11-
use crate::reader::{is_whitespace, BangType, Parser, ReadTextResult, Reader, Span, XmlSource};
11+
use crate::reader::{BangType, Parser, ReadTextResult, Reader, Span, XmlSource};
12+
use crate::utils::is_whitespace;
1213

1314
macro_rules! impl_buffered_source {
1415
($($lf:lifetime, $reader:tt, $async:ident, $await:ident)?) => {

0 commit comments

Comments
 (0)