Skip to content

Commit 4543ba6

Browse files
committed
Make const as many functions as possible
- Attr::key() - Attr::value() - Attributes::html() - Attributes::new() - BytesDecl::from_start() - Decoder::encoding() - Deserializer::get_ref() - IoReader::get_ref() - LocalName::into_inner() - Namespace::into_inner() - NsReader::config() - NsReader::prefixes() - Prefix::into_inner() - QName::into_inner() - Reader::buffer_position() - Reader::config() - Reader::decoder() - Reader::error_position() - Reader::get_ref() - resolve_html5_entity() - resolve_predefined_entity() - resolve_xml_entity() - SliceReader::get_ref() - Writer::get_ref() - Writer::new()
1 parent f5996ff commit 4543ba6

12 files changed

+66
-39
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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub struct BytesStart<'a> {
7777
impl<'a> BytesStart<'a> {
7878
/// Internal constructor, used by `Reader`. Supplies data in reader's encoding
7979
#[inline]
80-
pub(crate) fn wrap(content: &'a [u8], name_len: usize) -> Self {
80+
pub(crate) const fn wrap(content: &'a [u8], name_len: usize) -> Self {
8181
BytesStart {
8282
buf: Cow::Borrowed(content),
8383
name_len,
@@ -405,7 +405,7 @@ impl<'a> BytesDecl<'a> {
405405
}
406406

407407
/// Creates a `BytesDecl` from a `BytesStart`
408-
pub fn from_start(start: BytesStart<'a>) -> Self {
408+
pub const fn from_start(start: BytesStart<'a>) -> Self {
409409
Self { content: start }
410410
}
411411

@@ -620,7 +620,7 @@ pub struct BytesEnd<'a> {
620620
impl<'a> BytesEnd<'a> {
621621
/// Internal constructor, used by `Reader`. Supplies data in reader's encoding
622622
#[inline]
623-
pub(crate) fn wrap(name: Cow<'a, [u8]>) -> Self {
623+
pub(crate) const fn wrap(name: Cow<'a, [u8]>) -> Self {
624624
BytesEnd { name }
625625
}
626626

@@ -1018,7 +1018,7 @@ pub struct BytesPI<'a> {
10181018
impl<'a> BytesPI<'a> {
10191019
/// Creates a new `BytesPI` from a byte sequence in the specified encoding.
10201020
#[inline]
1021-
pub(crate) fn wrap(content: &'a [u8], target_len: usize) -> Self {
1021+
pub(crate) const fn wrap(content: &'a [u8], target_len: usize) -> Self {
10221022
Self {
10231023
content: BytesStart::wrap(content, target_len),
10241024
}

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/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ enum EncodingRef {
495495
#[cfg(feature = "encoding")]
496496
impl EncodingRef {
497497
#[inline]
498-
fn encoding(&self) -> &'static Encoding {
498+
const fn encoding(&self) -> &'static Encoding {
499499
match self {
500500
Self::Implicit(e) => e,
501501
Self::Explicit(e) => e,
@@ -504,7 +504,7 @@ impl EncodingRef {
504504
}
505505
}
506506
#[inline]
507-
fn can_be_refined(&self) -> bool {
507+
const fn can_be_refined(&self) -> bool {
508508
match self {
509509
Self::Implicit(_) | Self::BomDetected(_) => true,
510510
Self::Explicit(_) | Self::XmlDetected(_) => false,
@@ -587,7 +587,7 @@ impl<R> Reader<R> {
587587
}
588588

589589
/// Returns reference to the parser configuration
590-
pub fn config(&self) -> &Config {
590+
pub const fn config(&self) -> &Config {
591591
&self.state.config
592592
}
593593

@@ -657,7 +657,7 @@ impl<R> Reader<R> {
657657
}
658658

659659
/// Gets a reference to the underlying reader.
660-
pub fn get_ref(&self) -> &R {
660+
pub const fn get_ref(&self) -> &R {
661661
&self.reader
662662
}
663663

@@ -667,7 +667,7 @@ impl<R> Reader<R> {
667667
}
668668

669669
/// Gets the current byte position in the input data.
670-
pub fn buffer_position(&self) -> usize {
670+
pub const fn buffer_position(&self) -> usize {
671671
// when internal state is InsideMarkup, we have actually read until '<',
672672
// which we don't want to show
673673
if let ParseState::InsideMarkup = self.state.state {
@@ -688,7 +688,7 @@ impl<R> Reader<R> {
688688
/// markup element (i. e. to the `<` character).
689689
///
690690
/// This position is always `<= buffer_position()`.
691-
pub fn error_position(&self) -> usize {
691+
pub const fn error_position(&self) -> usize {
692692
self.state.last_error_offset
693693
}
694694

@@ -702,7 +702,7 @@ impl<R> Reader<R> {
702702
///
703703
/// [`encoding`]: ../index.html#encoding
704704
#[inline]
705-
pub fn decoder(&self) -> Decoder {
705+
pub const fn decoder(&self) -> Decoder {
706706
self.state.decoder()
707707
}
708708
}
@@ -912,7 +912,7 @@ enum BangType {
912912
}
913913
impl BangType {
914914
#[inline(always)]
915-
fn new(byte: Option<u8>) -> Result<Self> {
915+
const fn new(byte: Option<u8>) -> Result<Self> {
916916
Ok(match byte {
917917
Some(b'[') => Self::CData,
918918
Some(b'-') => Self::Comment,
@@ -983,7 +983,7 @@ impl BangType {
983983
None
984984
}
985985
#[inline]
986-
fn to_err(&self) -> Error {
986+
const fn to_err(&self) -> Error {
987987
match self {
988988
Self::CData => Error::Syntax(SyntaxError::UnclosedCData),
989989
Self::Comment => Error::Syntax(SyntaxError::UnclosedComment),

src/reader/ns_reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<R> NsReader<R> {
3939

4040
/// Returns reference to the parser configuration
4141
#[inline]
42-
pub fn config(&self) -> &Config {
42+
pub const fn config(&self) -> &Config {
4343
self.reader.config()
4444
}
4545

@@ -129,7 +129,7 @@ impl<R> NsReader<R> {
129129
/// # quick_xml::Result::Ok(())
130130
/// ```
131131
#[inline]
132-
pub fn prefixes(&self) -> PrefixIter {
132+
pub const fn prefixes(&self) -> PrefixIter {
133133
self.ns_resolver.iter()
134134
}
135135
}

src/reader/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl ReaderState {
292292
/// defaults to UTF-8.
293293
///
294294
/// [`encoding`]: ../../index.html#encoding
295-
pub fn decoder(&self) -> Decoder {
295+
pub const fn decoder(&self) -> Decoder {
296296
Decoder {
297297
#[cfg(feature = "encoding")]
298298
encoding: self.encoding.encoding(),

src/writer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub struct Writer<W> {
7070

7171
impl<W> Writer<W> {
7272
/// Creates a `Writer` from a generic writer.
73-
pub fn new(inner: W) -> Writer<W> {
73+
pub const fn new(inner: W) -> Writer<W> {
7474
Writer {
7575
writer: inner,
7676
indent: None,
@@ -96,7 +96,7 @@ impl<W> Writer<W> {
9696
}
9797

9898
/// Get a reference to the underlying writer.
99-
pub fn get_ref(&self) -> &W {
99+
pub const fn get_ref(&self) -> &W {
100100
&self.writer
101101
}
102102

0 commit comments

Comments
 (0)