Skip to content

Commit 589585e

Browse files
committed
Move Reader::from_str() constructor alongside Reader::from_reader()
1 parent 410b65b commit 589585e

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
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};
@@ -471,6 +471,31 @@ impl<R: Read> Reader<Utf8BytesReader<R>> {
471471
}
472472
}
473473

474+
/// Builder methods
475+
impl<'a> Reader<&'a [u8]> {
476+
/// Creates an XML reader from a string slice.
477+
pub fn from_str(s: &'a str) -> Self {
478+
// Rust strings are guaranteed to be UTF-8, so lock the encoding
479+
#[cfg(feature = "encoding")]
480+
{
481+
let mut parser = Parser::default();
482+
parser.encoding = EncodingRef::Explicit(UTF_8);
483+
Self {
484+
reader: s.as_bytes(),
485+
parser: parser,
486+
}
487+
}
488+
489+
#[cfg(not(feature = "encoding"))]
490+
{
491+
Self {
492+
reader: s.as_bytes(),
493+
parser: Parser::default(),
494+
}
495+
}
496+
}
497+
}
498+
474499
/// Public implementation-independent functionality
475500
impl<R> Reader<R> {
476501
// Configuration setters

src/reader/slice_reader.rs

Lines changed: 1 addition & 27 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,34 +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-
pub fn from_str(s: &'a str) -> Self {
27-
// Rust strings are guaranteed to be UTF-8, so lock the encoding
28-
#[cfg(feature = "encoding")]
29-
{
30-
let mut parser = Parser::default();
31-
parser.encoding = EncodingRef::Explicit(UTF_8);
32-
Self {
33-
reader: s.as_bytes(),
34-
parser: parser,
35-
}
36-
}
37-
38-
#[cfg(not(feature = "encoding"))]
39-
{
40-
Self {
41-
reader: s.as_bytes(),
42-
parser: Parser::default(),
43-
}
44-
}
45-
}
46-
4721
/// Read an event that borrows from the input rather than a buffer.
4822
///
4923
/// There is no asynchronous `read_event_async()` version of this function,

0 commit comments

Comments
 (0)