Skip to content

Commit 8a14c1a

Browse files
committed
Move Reader::from_str() constructor alongside Reader::from_reader()
1 parent 4b848b7 commit 8a14c1a

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

src/reader/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::Read;
44
use std::ops::Range;
55

66
#[cfg(feature = "encoding")]
7-
use encoding_rs::Encoding;
7+
use encoding_rs::{Encoding, UTF_8};
88

99
use crate::encoding::{Decoder, Utf8BytesReader};
1010
use crate::errors::{Error, Result};
@@ -540,6 +540,31 @@ impl<R: Read> Reader<Utf8BytesReader<R>> {
540540
}
541541
}
542542

543+
/// Builder methods
544+
impl<'a> Reader<&'a [u8]> {
545+
/// Creates an XML reader from a string slice.
546+
pub fn from_str(s: &'a str) -> Self {
547+
// Rust strings are guaranteed to be UTF-8, so lock the encoding
548+
#[cfg(feature = "encoding")]
549+
{
550+
let mut parser = Parser::default();
551+
parser.encoding = EncodingRef::Explicit(UTF_8);
552+
Self {
553+
reader: s.as_bytes(),
554+
parser: parser,
555+
}
556+
}
557+
558+
#[cfg(not(feature = "encoding"))]
559+
{
560+
Self {
561+
reader: s.as_bytes(),
562+
parser: Parser::default(),
563+
}
564+
}
565+
}
566+
}
567+
543568
/// Public implementation-independent functionality
544569
impl<R> Reader<R> {
545570
// Configuration setters

src/reader/slice_reader.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use std::borrow::Cow;
66

77
#[cfg(feature = "encoding")]
8-
use crate::reader::EncodingRef;
9-
#[cfg(feature = "encoding")]
10-
use encoding_rs::{Encoding, UTF_8};
8+
use encoding_rs::Encoding;
119

1210
use crate::errors::{Error, Result};
1311
use crate::events::Event;
@@ -16,35 +14,10 @@ use crate::reader::{is_whitespace, BangType, ReadElementState, Reader, Span, Xml
1614

1715
use memchr;
1816

19-
use super::parser::Parser;
20-
2117
/// This is an implementation of [`Reader`] for reading from a `&[u8]` as
2218
/// underlying byte stream. This implementation supports not using an
2319
/// intermediate buffer as the byte slice itself can be used to borrow from.
2420
impl<'a> Reader<&'a [u8]> {
25-
/// Creates an XML reader from a string slice.
26-
#[allow(clippy::should_implement_trait)]
27-
pub fn from_str(s: &'a str) -> Self {
28-
// Rust strings are guaranteed to be UTF-8, so lock the encoding
29-
#[cfg(feature = "encoding")]
30-
{
31-
let mut parser = Parser::default();
32-
parser.encoding = EncodingRef::Explicit(UTF_8);
33-
Self {
34-
reader: s.as_bytes(),
35-
parser: parser,
36-
}
37-
}
38-
39-
#[cfg(not(feature = "encoding"))]
40-
{
41-
Self {
42-
reader: s.as_bytes(),
43-
parser: Parser::default(),
44-
}
45-
}
46-
}
47-
4821
/// Read an event that borrows from the input rather than a buffer.
4922
///
5023
/// There is no asynchronous `read_event_async()` version of this function,

0 commit comments

Comments
 (0)