Skip to content

Commit

Permalink
Turn whatever possible to a const fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingun authored and dralley committed Aug 26, 2022
1 parent 0bd16cf commit 9b0caff
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 27 deletions.
17 changes: 17 additions & 0 deletions Changelog.md
Expand Up @@ -41,6 +41,23 @@
- [#455]: Change return type of all `read_to_end*` methods to return a span between tags
- [#455]: Added `Reader::read_text` method to return a raw content (including markup) between tags
- [#459]: Added a `Writer::write_bom()` method for inserting a Byte-Order-Mark into the document.
- [#467]: The following functions made `const`:
- `Attr::key`
- `Attr::value`
- `Attributes::html`
- `Attributes::new`
- `BytesDecl::from_start`
- `Decoder::encoding`
- `LocalName::into_inner`
- `Namespace::into_inner`
- `Prefix::into_inner`
- `QName::into_inner`
- `Reader::buffer_position`
- `Reader::decoder`
- `Reader::get_ref`
- `Serializer::new`
- `Serializer::with_root`
- `Writer::new`

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion src/de/escape.rs
Expand Up @@ -25,7 +25,7 @@ pub struct EscapedDeserializer<'a> {
}

impl<'a> EscapedDeserializer<'a> {
pub fn new(escaped_value: Cow<'a, [u8]>, decoder: Decoder, escaped: bool) -> Self {
pub const fn new(escaped_value: Cow<'a, [u8]>, decoder: Decoder, escaped: bool) -> Self {
EscapedDeserializer {
decoder,
escaped_value,
Expand Down
2 changes: 1 addition & 1 deletion src/de/simple_type.rs
Expand Up @@ -546,7 +546,7 @@ impl<'de, 'a> SimpleTypeDeserializer<'de, 'a> {

/// Constructor for tests
#[inline]
fn new(content: CowRef<'de, 'a>, escaped: bool, decoder: Decoder) -> Self {
const fn new(content: CowRef<'de, 'a>, escaped: bool, decoder: Decoder) -> Self {
Self {
content,
escaped,
Expand Down
2 changes: 1 addition & 1 deletion src/encoding.rs
Expand Up @@ -90,7 +90,7 @@ impl Decoder {
/// This encoding will be used by [`decode`].
///
/// [`decode`]: Self::decode
pub fn encoding(&self) -> &'static Encoding {
pub const fn encoding(&self) -> &'static Encoding {
self.encoding
}

Expand Down
12 changes: 6 additions & 6 deletions src/events/attributes.rs
Expand Up @@ -191,20 +191,20 @@ pub struct Attributes<'a> {
impl<'a> Attributes<'a> {
/// Internal constructor, used by `BytesStart`. Supplies data in reader's encoding
#[inline]
pub(crate) fn wrap(buf: &'a [u8], pos: usize, html: bool) -> Self {
pub(crate) const fn wrap(buf: &'a [u8], pos: usize, html: bool) -> Self {
Self {
bytes: buf,
state: IterState::new(pos, html),
}
}

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

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

Expand Down Expand Up @@ -412,7 +412,7 @@ impl<T> Attr<T> {
impl<'a> Attr<&'a [u8]> {
/// Returns the key value
#[inline]
pub fn key(&self) -> QName<'a> {
pub const fn key(&self) -> QName<'a> {
QName(match self {
Attr::DoubleQ(key, _) => key,
Attr::SingleQ(key, _) => key,
Expand All @@ -425,7 +425,7 @@ impl<'a> Attr<&'a [u8]> {
///
/// [HTML specification]: https://www.w3.org/TR/2012/WD-html-markup-20120329/syntax.html#syntax-attr-empty
#[inline]
pub fn value(&self) -> &'a [u8] {
pub const fn value(&self) -> &'a [u8] {
match self {
Attr::DoubleQ(_, value) => value,
Attr::SingleQ(_, value) => value,
Expand Down Expand Up @@ -514,7 +514,7 @@ pub(crate) struct IterState {
}

impl IterState {
pub fn new(offset: usize, html: bool) -> Self {
pub const fn new(offset: usize, html: bool) -> Self {
Self {
state: State::Next(offset),
html,
Expand Down
6 changes: 3 additions & 3 deletions src/events/mod.rs
Expand Up @@ -71,7 +71,7 @@ pub struct BytesStart<'a> {
impl<'a> BytesStart<'a> {
/// Internal constructor, used by `Reader`. Supplies data in reader's encoding
#[inline]
pub(crate) fn wrap(content: &'a [u8], name_len: usize) -> Self {
pub(crate) const fn wrap(content: &'a [u8], name_len: usize) -> Self {
BytesStart {
buf: Cow::Borrowed(content),
name_len,
Expand Down Expand Up @@ -343,7 +343,7 @@ impl<'a> BytesDecl<'a> {
}

/// Creates a `BytesDecl` from a `BytesStart`
pub fn from_start(start: BytesStart<'a>) -> Self {
pub const fn from_start(start: BytesStart<'a>) -> Self {
Self { content: start }
}

Expand Down Expand Up @@ -543,7 +543,7 @@ pub struct BytesEnd<'a> {
impl<'a> BytesEnd<'a> {
/// Internal constructor, used by `Reader`. Supplies data in reader's encoding
#[inline]
pub(crate) fn wrap(name: Cow<'a, [u8]>) -> Self {
pub(crate) const fn wrap(name: Cow<'a, [u8]>) -> Self {
BytesEnd { name }
}

Expand Down
8 changes: 4 additions & 4 deletions src/name.rs
Expand Up @@ -21,7 +21,7 @@ pub struct QName<'a>(pub &'a [u8]);
impl<'a> QName<'a> {
/// Converts this name to an internal slice representation.
#[inline(always)]
pub fn into_inner(self) -> &'a [u8] {
pub const fn into_inner(self) -> &'a [u8] {
self.0
}

Expand Down Expand Up @@ -138,7 +138,7 @@ pub struct LocalName<'a>(&'a [u8]);
impl<'a> LocalName<'a> {
/// Converts this name to an internal slice representation.
#[inline(always)]
pub fn into_inner(self) -> &'a [u8] {
pub const fn into_inner(self) -> &'a [u8] {
self.0
}
}
Expand Down Expand Up @@ -188,7 +188,7 @@ pub struct Prefix<'a>(&'a [u8]);
impl<'a> Prefix<'a> {
/// Extracts internal slice
#[inline(always)]
pub fn into_inner(self) -> &'a [u8] {
pub const fn into_inner(self) -> &'a [u8] {
self.0
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ impl<'a> Namespace<'a> {
/// [non-normalized]: https://www.w3.org/TR/REC-xml/#AVNormalize
/// [IRI reference]: https://datatracker.ietf.org/doc/html/rfc3987
#[inline(always)]
pub fn into_inner(self) -> &'a [u8] {
pub const fn into_inner(self) -> &'a [u8] {
self.0
}
//TODO: implement value normalization and use it when comparing namespaces
Expand Down
12 changes: 6 additions & 6 deletions src/reader/mod.rs
Expand Up @@ -351,7 +351,7 @@ enum EncodingRef {
#[cfg(feature = "encoding")]
impl EncodingRef {
#[inline]
fn encoding(&self) -> &'static Encoding {
const fn encoding(&self) -> &'static Encoding {
match self {
Self::Implicit(e) => e,
Self::Explicit(e) => e,
Expand All @@ -360,7 +360,7 @@ impl EncodingRef {
}
}
#[inline]
fn can_be_refined(&self) -> bool {
const fn can_be_refined(&self) -> bool {
match self {
Self::Implicit(_) | Self::BomDetected(_) => true,
Self::Explicit(_) | Self::XmlDetected(_) => false,
Expand Down Expand Up @@ -505,7 +505,7 @@ impl<R> Reader<R> {
}

/// Gets a reference to the underlying reader.
pub fn get_ref(&self) -> &R {
pub const fn get_ref(&self) -> &R {
&self.reader
}

Expand All @@ -517,7 +517,7 @@ impl<R> Reader<R> {
/// Gets the current byte position in the input data.
///
/// Useful when debugging errors.
pub fn buffer_position(&self) -> usize {
pub const fn buffer_position(&self) -> usize {
// when internal state is OpenedTag, we have actually read until '<',
// which we don't want to show
if let ParseState::OpenedTag = self.parser.state {
Expand All @@ -535,7 +535,7 @@ impl<R> Reader<R> {
/// If `encoding` feature is enabled and no encoding is specified in declaration,
/// defaults to UTF-8.
#[inline]
pub fn decoder(&self) -> Decoder {
pub const fn decoder(&self) -> Decoder {
self.parser.decoder()
}
}
Expand Down Expand Up @@ -789,7 +789,7 @@ impl ReadElementState {

/// A function to check whether the byte is a whitespace (blank, new line, carriage return or tab)
#[inline]
pub(crate) fn is_whitespace(b: u8) -> bool {
pub(crate) const fn is_whitespace(b: u8) -> bool {
match b {
b' ' | b'\r' | b'\n' | b'\t' => true,
_ => false,
Expand Down
2 changes: 1 addition & 1 deletion src/reader/parser.rs
Expand Up @@ -247,7 +247,7 @@ impl Parser {
///
/// If `encoding` feature is enabled and no encoding is specified in declaration,
/// defaults to UTF-8.
pub fn decoder(&self) -> Decoder {
pub const fn decoder(&self) -> Decoder {
Decoder {
#[cfg(feature = "encoding")]
encoding: self.encoding.encoding(),
Expand Down
2 changes: 1 addition & 1 deletion src/reader/slice_reader.rs
Expand Up @@ -334,7 +334,7 @@ mod test {
use crate::reader::XmlSource;

/// Default buffer constructor just pass the byte array from the test
fn identity<T>(input: T) -> T {
const fn identity<T>(input: T) -> T {
input
}

Expand Down
4 changes: 2 additions & 2 deletions src/se/mod.rs
Expand Up @@ -39,7 +39,7 @@ impl<'r, W: Write> Serializer<'r, W> {
/// Note, that attempt to serialize a non-struct (including unit structs
/// and newtype structs) will end up to an error. Use `with_root` to create
/// serializer with explicitly defined root element name
pub fn new(writer: W) -> Self {
pub const fn new(writer: W) -> Self {
Self::with_root(Writer::new(writer), None)
}

Expand Down Expand Up @@ -91,7 +91,7 @@ impl<'r, W: Write> Serializer<'r, W> {
/// r#"<root question="The Ultimate Question of Life, the Universe, and Everything" answer="42"/>"#
/// );
/// ```
pub fn with_root(writer: Writer<W>, root_tag: Option<&'r str>) -> Self {
pub const fn with_root(writer: Writer<W>, root_tag: Option<&'r str>) -> Self {
Self { writer, root_tag }
}

Expand Down
2 changes: 1 addition & 1 deletion src/writer.rs
Expand Up @@ -63,7 +63,7 @@ pub struct Writer<W: Write> {

impl<W: Write> Writer<W> {
/// Creates a Writer from a generic Write
pub fn new(inner: W) -> Writer<W> {
pub const fn new(inner: W) -> Writer<W> {
Writer {
writer: inner,
indent: None,
Expand Down

0 comments on commit 9b0caff

Please sign in to comment.