@@ -59,23 +59,28 @@ use attributes::{Attribute, Attributes};
59
59
/// The name can be accessed using the [`name`] or [`local_name`] methods.
60
60
/// An iterator over the attributes is returned by the [`attributes`] method.
61
61
///
62
+ /// If this was created from a reader, its location in the data can be accessed
63
+ /// using the [`span`] method.
64
+ ///
62
65
/// [`name`]: Self::name
63
66
/// [`local_name`]: Self::local_name
64
67
/// [`attributes`]: Self::attributes
65
- #[ derive( Clone , Eq , PartialEq ) ]
68
+ /// [`span`]: Self::span
69
+ #[ derive( Clone , Eq ) ]
66
70
pub struct BytesStart < ' a > {
67
71
/// content of the element, before any utf8 conversion
68
72
pub ( crate ) buf : Cow < ' a , [ u8 ] > ,
69
73
/// end of the element name, the name starts at that the start of `buf`
70
74
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 > ,
73
78
}
74
79
75
80
impl < ' a > BytesStart < ' a > {
76
81
/// Internal constructor, used by `Reader`. Supplies data in reader's encoding
77
82
#[ 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 {
79
84
BytesStart {
80
85
buf : Cow :: Borrowed ( content) ,
81
86
name_len,
@@ -93,7 +98,7 @@ impl<'a> BytesStart<'a> {
93
98
let buf = str_cow_to_bytes ( name) ;
94
99
BytesStart {
95
100
name_len : buf. len ( ) ,
96
- span : 0 ..buf . len ( ) ,
101
+ span : None ,
97
102
buf,
98
103
}
99
104
}
@@ -110,7 +115,7 @@ impl<'a> BytesStart<'a> {
110
115
let buf = str_cow_to_bytes ( content) ;
111
116
112
117
BytesStart {
113
- span : 0 ..buf . len ( ) ,
118
+ span : None ,
114
119
buf,
115
120
name_len,
116
121
}
@@ -187,14 +192,15 @@ impl<'a> BytesStart<'a> {
187
192
self . name ( ) . into ( )
188
193
}
189
194
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.
191
197
///
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.
194
200
///
195
201
/// [`set_name`]: Self::set_name
196
202
#[ inline]
197
- pub fn span ( & self ) -> Span {
203
+ pub fn span ( & self ) -> Option < Span > {
198
204
self . span . clone ( )
199
205
}
200
206
@@ -307,6 +313,14 @@ impl<'a> Deref for BytesStart<'a> {
307
313
}
308
314
}
309
315
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
+
310
324
////////////////////////////////////////////////////////////////////////////////////////////////////
311
325
312
326
/// An XML declaration (`Event::Decl`).
0 commit comments