@@ -75,6 +75,14 @@ impl<'a> BytesStartText<'a> {
7575 self . content . into_inner ( )
7676 }
7777
78+ /// Converts the event into a borrowed event.
79+ #[ inline]
80+ pub fn borrow ( & self ) -> BytesStartText {
81+ BytesStartText {
82+ content : self . content . borrow ( ) ,
83+ }
84+ }
85+
7886 /// Decodes bytes of event, stripping byte order mark (BOM) if it is presented
7987 /// in the event.
8088 ///
@@ -195,15 +203,15 @@ impl<'a> BytesStart<'a> {
195203 /// # fn example(&self) -> Result<(), Error> {
196204 /// # let mut writer = Writer::new(Vec::new());
197205 ///
198- /// writer.write_event(Event::Start(self.attrs.to_borrowed ()))?;
206+ /// writer.write_event(Event::Start(self.attrs.borrow ()))?;
199207 /// // ...
200208 /// writer.write_event(Event::End(self.attrs.to_end()))?;
201209 /// # Ok(())
202210 /// # }}
203211 /// ```
204212 ///
205213 /// [`to_end`]: Self::to_end
206- pub fn to_borrowed ( & self ) -> BytesStart {
214+ pub fn borrow ( & self ) -> BytesStart {
207215 BytesStart :: borrowed ( & self . buf , self . name_len )
208216 }
209217
@@ -343,13 +351,13 @@ impl<'a> Deref for BytesStart<'a> {
343351/// [W3C XML 1.1 Prolog and Document Type Declaration](http://w3.org/TR/xml11/#sec-prolog-dtd)
344352#[ derive( Clone , Debug , Eq , PartialEq ) ]
345353pub struct BytesDecl < ' a > {
346- element : BytesStart < ' a > ,
354+ content : BytesStart < ' a > ,
347355}
348356
349357impl < ' a > BytesDecl < ' a > {
350358 /// Creates a `BytesDecl` from a `BytesStart`
351- pub fn from_start ( start : BytesStart < ' a > ) -> BytesDecl < ' a > {
352- BytesDecl { element : start }
359+ pub fn from_start ( start : BytesStart < ' a > ) -> Self {
360+ Self { content : start }
353361 }
354362
355363 /// Gets xml version, excluding quotes (`'` or `"`).
@@ -407,7 +415,7 @@ impl<'a> BytesDecl<'a> {
407415 /// [grammar]: https://www.w3.org/TR/xml11/#NT-XMLDecl
408416 pub fn version ( & self ) -> Result < Cow < [ u8 ] > > {
409417 // The version *must* be the first thing in the declaration.
410- match self . element . attributes ( ) . with_checks ( false ) . next ( ) {
418+ match self . content . attributes ( ) . with_checks ( false ) . next ( ) {
411419 Some ( Ok ( a) ) if a. key . as_ref ( ) == b"version" => Ok ( a. value ) ,
412420 // first attribute was not "version"
413421 Some ( Ok ( a) ) => {
@@ -457,7 +465,7 @@ impl<'a> BytesDecl<'a> {
457465 ///
458466 /// [grammar]: https://www.w3.org/TR/xml11/#NT-XMLDecl
459467 pub fn encoding ( & self ) -> Option < Result < Cow < [ u8 ] > > > {
460- self . element
468+ self . content
461469 . try_get_attribute ( "encoding" )
462470 . map ( |a| a. map ( |a| a. value ) )
463471 . transpose ( )
@@ -499,7 +507,7 @@ impl<'a> BytesDecl<'a> {
499507 ///
500508 /// [grammar]: https://www.w3.org/TR/xml11/#NT-XMLDecl
501509 pub fn standalone ( & self ) -> Option < Result < Cow < [ u8 ] > > > {
502- self . element
510+ self . content
503511 . try_get_attribute ( "standalone" )
504512 . map ( |a| a. map ( |a| a. value ) )
505513 . transpose ( )
@@ -548,7 +556,7 @@ impl<'a> BytesDecl<'a> {
548556 buf. push ( b'"' ) ;
549557
550558 BytesDecl {
551- element : BytesStart :: owned ( buf, 3 ) ,
559+ content : BytesStart :: owned ( buf, 3 ) ,
552560 }
553561 }
554562
@@ -563,7 +571,15 @@ impl<'a> BytesDecl<'a> {
563571 /// Converts the event into an owned event.
564572 pub fn into_owned ( self ) -> BytesDecl < ' static > {
565573 BytesDecl {
566- element : self . element . into_owned ( ) ,
574+ content : self . content . into_owned ( ) ,
575+ }
576+ }
577+
578+ /// Converts the event into a borrowed event.
579+ #[ inline]
580+ pub fn borrow ( & self ) -> BytesDecl {
581+ BytesDecl {
582+ content : self . content . borrow ( ) ,
567583 }
568584 }
569585}
@@ -572,7 +588,7 @@ impl<'a> Deref for BytesDecl<'a> {
572588 type Target = [ u8 ] ;
573589
574590 fn deref ( & self ) -> & [ u8 ] {
575- & * self . element
591+ & * self . content
576592 }
577593}
578594
@@ -608,6 +624,14 @@ impl<'a> BytesEnd<'a> {
608624 }
609625 }
610626
627+ /// Converts the event into a borrowed event.
628+ #[ inline]
629+ pub fn borrow ( & self ) -> BytesEnd {
630+ BytesEnd {
631+ name : Cow :: Borrowed ( & self . name ) ,
632+ }
633+ }
634+
611635 /// Gets the undecoded raw tag name, as present in the input stream.
612636 #[ inline]
613637 pub fn name ( & self ) -> QName {
@@ -699,6 +723,14 @@ impl<'a> BytesText<'a> {
699723 self . content
700724 }
701725
726+ /// Converts the event into a borrowed event.
727+ #[ inline]
728+ pub fn borrow ( & self ) -> BytesText {
729+ BytesText {
730+ content : Cow :: Borrowed ( & self . content ) ,
731+ }
732+ }
733+
702734 /// Returns unescaped version of the text content, that can be written
703735 /// as CDATA in XML
704736 #[ cfg( feature = "serialize" ) ]
@@ -853,6 +885,14 @@ impl<'a> BytesCData<'a> {
853885 self . content
854886 }
855887
888+ /// Converts the event into a borrowed event.
889+ #[ inline]
890+ pub fn borrow ( & self ) -> BytesCData {
891+ BytesCData {
892+ content : Cow :: Borrowed ( & self . content ) ,
893+ }
894+ }
895+
856896 /// Converts this CDATA content to an escaped version, that can be written
857897 /// as an usual text in XML.
858898 ///
@@ -1015,6 +1055,24 @@ impl<'a> Event<'a> {
10151055 Event :: Eof => Event :: Eof ,
10161056 }
10171057 }
1058+
1059+ /// Converts the event into a borrowed event.
1060+ #[ inline]
1061+ pub fn borrow ( & self ) -> Event {
1062+ match self {
1063+ Event :: StartText ( e) => Event :: StartText ( e. borrow ( ) ) ,
1064+ Event :: Start ( e) => Event :: Start ( e. borrow ( ) ) ,
1065+ Event :: End ( e) => Event :: End ( e. borrow ( ) ) ,
1066+ Event :: Empty ( e) => Event :: Empty ( e. borrow ( ) ) ,
1067+ Event :: Text ( e) => Event :: Text ( e. borrow ( ) ) ,
1068+ Event :: Comment ( e) => Event :: Comment ( e. borrow ( ) ) ,
1069+ Event :: CData ( e) => Event :: CData ( e. borrow ( ) ) ,
1070+ Event :: Decl ( e) => Event :: Decl ( e. borrow ( ) ) ,
1071+ Event :: PI ( e) => Event :: PI ( e. borrow ( ) ) ,
1072+ Event :: DocType ( e) => Event :: DocType ( e. borrow ( ) ) ,
1073+ Event :: Eof => Event :: Eof ,
1074+ }
1075+ }
10181076}
10191077
10201078impl < ' a > Deref for Event < ' a > {
0 commit comments