@@ -105,10 +105,10 @@ impl Context<'_> {
105105 self . dst . join ( & filename)
106106 }
107107
108- fn write_shared < C : AsRef < [ u8 ] > > (
108+ fn write_shared (
109109 & self ,
110110 resource : SharedResource < ' _ > ,
111- contents : C ,
111+ contents : impl ' static + Send + AsRef < [ u8 ] > ,
112112 emit : & [ EmitType ] ,
113113 ) -> Result < ( ) , Error > {
114114 if resource. should_emit ( emit) {
@@ -121,25 +121,23 @@ impl Context<'_> {
121121 fn write_minify (
122122 & self ,
123123 resource : SharedResource < ' _ > ,
124- contents : & str ,
124+ contents : impl ' static + Send + AsRef < str > + AsRef < [ u8 ] > ,
125125 minify : bool ,
126126 emit : & [ EmitType ] ,
127127 ) -> Result < ( ) , Error > {
128- let tmp ;
129- let contents = if minify {
130- tmp = if resource. extension ( ) == Some ( & OsStr :: new ( "css" ) ) {
128+ if minify {
129+ let contents = contents . as_ref ( ) ;
130+ let contents = if resource. extension ( ) == Some ( & OsStr :: new ( "css" ) ) {
131131 minifier:: css:: minify ( contents) . map_err ( |e| {
132132 Error :: new ( format ! ( "failed to minify CSS file: {}" , e) , resource. path ( self ) )
133133 } ) ?
134134 } else {
135135 minifier:: js:: minify ( contents)
136136 } ;
137- tmp . as_bytes ( )
137+ self . write_shared ( resource , contents , emit )
138138 } else {
139- contents. as_bytes ( )
140- } ;
141-
142- self . write_shared ( resource, contents, emit)
139+ self . write_shared ( resource, contents, emit)
140+ }
143141 }
144142}
145143
@@ -155,15 +153,21 @@ pub(super) fn write_shared(
155153 let lock_file = cx. dst . join ( ".lock" ) ;
156154 let _lock = try_err ! ( flock:: Lock :: new( & lock_file, true , true , true ) , & lock_file) ;
157155
158- // The weird `: &_` is to work around a borrowck bug: https://github.com/rust-lang/rust/issues/41078#issuecomment-293646723
159- let write_minify = |p, c : & _ | {
156+ // Minified resources are usually toolchain resources. If they're not, they should use `cx.write_minify` directly.
157+ fn write_minify (
158+ basename : & ' static str ,
159+ contents : impl ' static + Send + AsRef < str > + AsRef < [ u8 ] > ,
160+ cx : & Context < ' _ > ,
161+ options : & RenderOptions ,
162+ ) -> Result < ( ) , Error > {
160163 cx. write_minify (
161- SharedResource :: ToolchainSpecific { basename : p } ,
162- c ,
164+ SharedResource :: ToolchainSpecific { basename } ,
165+ contents ,
163166 options. enable_minification ,
164167 & options. emit ,
165168 )
166- } ;
169+ }
170+
167171 // Toolchain resources should never be dynamic.
168172 let write_toolchain = |p : & ' static _ , c : & ' static _ | {
169173 cx. write_shared ( SharedResource :: ToolchainSpecific { basename : p } , c, & options. emit )
@@ -210,12 +214,12 @@ pub(super) fn write_shared(
210214 "details.undocumented > summary::before, details.rustdoc-toggle > summary::before" ,
211215 "toggle-plus.svg" ,
212216 ) ;
213- write_minify ( "rustdoc.css" , & rustdoc_css) ?;
217+ write_minify ( "rustdoc.css" , rustdoc_css, cx , options ) ?;
214218
215219 // Add all the static files. These may already exist, but we just
216220 // overwrite them anyway to make sure that they're fresh and up-to-date.
217- write_minify ( "settings.css" , static_files:: SETTINGS_CSS ) ?;
218- write_minify ( "noscript.css" , static_files:: NOSCRIPT_CSS ) ?;
221+ write_minify ( "settings.css" , static_files:: SETTINGS_CSS , cx , options ) ?;
222+ write_minify ( "noscript.css" , static_files:: NOSCRIPT_CSS , cx , options ) ?;
219223
220224 // To avoid "light.css" to be overwritten, we'll first run over the received themes and only
221225 // then we'll run over the "official" styles.
@@ -228,9 +232,9 @@ pub(super) fn write_shared(
228232
229233 // Handle the official themes
230234 match theme {
231- "light" => write_minify ( "light.css" , static_files:: themes:: LIGHT ) ?,
232- "dark" => write_minify ( "dark.css" , static_files:: themes:: DARK ) ?,
233- "ayu" => write_minify ( "ayu.css" , static_files:: themes:: AYU ) ?,
235+ "light" => write_minify ( "light.css" , static_files:: themes:: LIGHT , cx , options ) ?,
236+ "dark" => write_minify ( "dark.css" , static_files:: themes:: DARK , cx , options ) ?,
237+ "ayu" => write_minify ( "ayu.css" , static_files:: themes:: AYU , cx , options ) ?,
234238 _ => {
235239 // Handle added third-party themes
236240 let filename = format ! ( "{}.{}" , theme, extension) ;
@@ -264,26 +268,30 @@ pub(super) fn write_shared(
264268 // Maybe we can change the representation to move this out of main.js?
265269 write_minify (
266270 "main.js" ,
267- & static_files:: MAIN_JS . replace (
271+ static_files:: MAIN_JS . replace (
268272 "/* INSERT THEMES HERE */" ,
269273 & format ! ( " = {}" , serde_json:: to_string( & themes) . unwrap( ) ) ,
270274 ) ,
275+ cx,
276+ options,
271277 ) ?;
272- write_minify ( "search.js" , static_files:: SEARCH_JS ) ?;
273- write_minify ( "settings.js" , static_files:: SETTINGS_JS ) ?;
278+ write_minify ( "search.js" , static_files:: SEARCH_JS , cx , options ) ?;
279+ write_minify ( "settings.js" , static_files:: SETTINGS_JS , cx , options ) ?;
274280
275281 if cx. include_sources {
276- write_minify ( "source-script.js" , static_files:: sidebar:: SOURCE_SCRIPT ) ?;
282+ write_minify ( "source-script.js" , static_files:: sidebar:: SOURCE_SCRIPT , cx , options ) ?;
277283 }
278284
279285 {
280286 write_minify (
281287 "storage.js" ,
282- & format ! (
288+ format ! (
283289 "var resourcesSuffix = \" {}\" ;{}" ,
284290 cx. shared. resource_suffix,
285291 static_files:: STORAGE_JS
286292 ) ,
293+ cx,
294+ options,
287295 ) ?;
288296 }
289297
@@ -292,12 +300,12 @@ pub(super) fn write_shared(
292300 // This varies based on the invocation, so it can't go through the write_minify wrapper.
293301 cx. write_minify (
294302 SharedResource :: InvocationSpecific { basename : "theme.css" } ,
295- & buffer,
303+ buffer,
296304 options. enable_minification ,
297305 & options. emit ,
298306 ) ?;
299307 }
300- write_minify ( "normalize.css" , static_files:: NORMALIZE_CSS ) ?;
308+ write_minify ( "normalize.css" , static_files:: NORMALIZE_CSS , cx , options ) ?;
301309 for ( name, contents) in & * FILES_UNVERSIONED {
302310 cx. write_shared ( SharedResource :: Unversioned { name } , contents, & options. emit ) ?;
303311 }
@@ -512,7 +520,7 @@ pub(super) fn write_shared(
512520 content,
513521 & cx. shared . style_files ,
514522 ) ;
515- cx. shared . fs . write ( & dst, v. as_bytes ( ) ) ?;
523+ cx. shared . fs . write ( dst, v) ?;
516524 }
517525 }
518526
@@ -602,7 +610,7 @@ pub(super) fn write_shared(
602610 }",
603611 ) ;
604612 v. push_str ( "})()" ) ;
605- cx. shared . fs . write ( & mydst, & v) ?;
613+ cx. shared . fs . write ( mydst, v) ?;
606614 }
607615 Ok ( ( ) )
608616}
0 commit comments