@@ -59,23 +59,28 @@ use attributes::{Attribute, Attributes};
5959/// The name can be accessed using the [`name`] or [`local_name`] methods.
6060/// An iterator over the attributes is returned by the [`attributes`] method.
6161///
62+ /// If this was created from a reader, its location in the data can be accessed
63+ /// using the [`span`] method.
64+ ///
6265/// [`name`]: Self::name
6366/// [`local_name`]: Self::local_name
6467/// [`attributes`]: Self::attributes
65- #[ derive( Clone , Eq , PartialEq ) ]
68+ /// [`span`]: Self::span
69+ #[ derive( Clone , Eq ) ]
6670pub struct BytesStart < ' a > {
6771 /// content of the element, before any utf8 conversion
6872 pub ( crate ) buf : Cow < ' a , [ u8 ] > ,
6973 /// end of the element name, the name starts at that the start of `buf`
7074 pub ( crate ) name_len : usize ,
71- /// the position of the element in the input, this does not reflect updates to the struct
72- pub ( crate ) span : Span ,
75+ /// the position of the element in the input, this does not reflect updates to the struct and
76+ /// is not included in equality checking
77+ pub ( crate ) span : Option < Span > ,
7378}
7479
7580impl < ' a > BytesStart < ' a > {
7681 /// Internal constructor, used by `Reader`. Supplies data in reader's encoding
7782 #[ inline]
78- pub ( crate ) fn wrap ( content : & ' a [ u8 ] , name_len : usize , span : Span ) -> Self {
83+ pub ( crate ) fn wrap ( content : & ' a [ u8 ] , name_len : usize , span : Option < Span > ) -> Self {
7984 BytesStart {
8085 buf : Cow :: Borrowed ( content) ,
8186 name_len,
@@ -93,7 +98,7 @@ impl<'a> BytesStart<'a> {
9398 let buf = str_cow_to_bytes ( name) ;
9499 BytesStart {
95100 name_len : buf. len ( ) ,
96- span : 0 ..buf . len ( ) ,
101+ span : None ,
97102 buf,
98103 }
99104 }
@@ -110,7 +115,7 @@ impl<'a> BytesStart<'a> {
110115 let buf = str_cow_to_bytes ( content) ;
111116
112117 BytesStart {
113- span : 0 ..buf . len ( ) ,
118+ span : None ,
114119 buf,
115120 name_len,
116121 }
@@ -187,14 +192,15 @@ impl<'a> BytesStart<'a> {
187192 self . name ( ) . into ( )
188193 }
189194
190- /// Gets the range of bytes this element spans in the input stream.
195+ /// Gets the range of bytes this element spans in the input stream, if it
196+ /// came from one.
191197 ///
192- /// This does not reflect updates to the struct. I.e. if [`set_name`] is called, this will not
193- /// update the span, as the underlying source is not altered by this operation.
198+ /// This does not reflect updates to the struct. For example, if [`set_name`] is called, it
199+ /// not update the span, as the underlying source is not altered by the operation.
194200 ///
195201 /// [`set_name`]: Self::set_name
196202 #[ inline]
197- pub fn span ( & self ) -> Span {
203+ pub fn span ( & self ) -> Option < Span > {
198204 self . span . clone ( )
199205 }
200206
@@ -307,6 +313,14 @@ impl<'a> Deref for BytesStart<'a> {
307313 }
308314}
309315
316+ // Manually implemented to prevent equality changes between items returned from reader versus
317+ // items created via public API.
318+ impl < ' a > PartialEq for BytesStart < ' a > {
319+ fn eq ( & self , other : & Self ) -> bool {
320+ self . buf == other. buf && self . name_len == other. name_len
321+ }
322+ }
323+
310324////////////////////////////////////////////////////////////////////////////////////////////////////
311325
312326/// An XML declaration (`Event::Decl`).
0 commit comments