@@ -7,7 +7,7 @@ use std::fmt;
77
88use unicode_segmentation:: UnicodeSegmentation ;
99
10- #[ inline( always ) ]
10+ #[ inline]
1111fn escape ( s : & str , mut w : impl fmt:: Write , escape_quotes : bool ) -> fmt:: Result {
1212 // Because the internet is always right, turns out there's not that many
1313 // characters to escape: http://stackoverflow.com/questions/7381974
@@ -35,13 +35,30 @@ fn escape(s: &str, mut w: impl fmt::Write, escape_quotes: bool) -> fmt::Result {
3535 Ok ( ( ) )
3636}
3737
38+ struct WriteEscaped < W : fmt:: Write > {
39+ writer : W ,
40+ escape_quotes : bool ,
41+ }
42+
43+ impl < W : fmt:: Write > fmt:: Write for WriteEscaped < W > {
44+ #[ inline]
45+ fn write_str ( & mut self , s : & str ) -> fmt:: Result {
46+ escape ( s, & mut self . writer , self . escape_quotes )
47+ }
48+ }
49+
3850/// Wrapper struct which will emit the HTML-escaped version of the contained
3951/// string when passed to a format string.
40- pub ( crate ) struct Escape < ' a > ( pub & ' a str ) ;
52+ pub ( crate ) struct Escape < T > ( pub T ) ;
4153
42- impl fmt:: Display for Escape < ' _ > {
54+ impl < T : fmt:: Display > fmt:: Display for Escape < T > {
55+ #[ inline]
4356 fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
44- escape ( self . 0 , fmt, true )
57+ self . 0 . fmt (
58+ & mut fmt
59+ . options ( )
60+ . create_formatter ( & mut WriteEscaped { writer : fmt, escape_quotes : true } ) ,
61+ )
4562 }
4663}
4764
@@ -51,11 +68,15 @@ impl fmt::Display for Escape<'_> {
5168/// This is only safe to use for text nodes. If you need your output to be
5269/// safely contained in an attribute, use [`Escape`]. If you don't know the
5370/// difference, use [`Escape`].
54- pub ( crate ) struct EscapeBodyText < ' a > ( pub & ' a str ) ;
71+ pub ( crate ) struct EscapeBodyText < T > ( pub T ) ;
5572
56- impl fmt:: Display for EscapeBodyText < ' _ > {
73+ impl < T : fmt:: Display > fmt :: Display for EscapeBodyText < T > {
5774 fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
58- escape ( self . 0 , fmt, false )
75+ self . 0 . fmt (
76+ & mut fmt
77+ . options ( )
78+ . create_formatter ( & mut WriteEscaped { writer : fmt, escape_quotes : false } ) ,
79+ )
5980 }
6081}
6182
0 commit comments