@@ -7,7 +7,7 @@ use std::fmt;
7
7
8
8
use unicode_segmentation:: UnicodeSegmentation ;
9
9
10
- #[ inline( always ) ]
10
+ #[ inline]
11
11
fn escape ( s : & str , mut w : impl fmt:: Write , escape_quotes : bool ) -> fmt:: Result {
12
12
// Because the internet is always right, turns out there's not that many
13
13
// 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 {
35
35
Ok ( ( ) )
36
36
}
37
37
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
+
38
50
/// Wrapper struct which will emit the HTML-escaped version of the contained
39
51
/// string when passed to a format string.
40
- pub ( crate ) struct Escape < ' a > ( pub & ' a str ) ;
52
+ pub ( crate ) struct Escape < T > ( pub T ) ;
41
53
42
- impl fmt:: Display for Escape < ' _ > {
54
+ impl < T : fmt:: Display > fmt:: Display for Escape < T > {
55
+ #[ inline]
43
56
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
+ )
45
62
}
46
63
}
47
64
@@ -51,11 +68,15 @@ impl fmt::Display for Escape<'_> {
51
68
/// This is only safe to use for text nodes. If you need your output to be
52
69
/// safely contained in an attribute, use [`Escape`]. If you don't know the
53
70
/// difference, use [`Escape`].
54
- pub ( crate ) struct EscapeBodyText < ' a > ( pub & ' a str ) ;
71
+ pub ( crate ) struct EscapeBodyText < T > ( pub T ) ;
55
72
56
- impl fmt:: Display for EscapeBodyText < ' _ > {
73
+ impl < T : fmt:: Display > fmt :: Display for EscapeBodyText < T > {
57
74
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
+ )
59
80
}
60
81
}
61
82
0 commit comments