@@ -122,7 +122,7 @@ pub fn print_crate(cm: @CodeMap,
122
122
span_diagnostic : @diagnostic:: SpanHandler ,
123
123
crate : & ast:: Crate ,
124
124
filename : @str ,
125
- input : @ mut io:: Reader ,
125
+ input : & mut io:: Reader ,
126
126
out : ~io:: Writer ,
127
127
ann : @pp_ann ,
128
128
is_expanded : bool ) {
@@ -211,13 +211,8 @@ pub fn fun_to_str(decl: &ast::fn_decl, purity: ast::purity, name: ast::Ident,
211
211
end ( & mut s) ; // Close the head box
212
212
end ( & mut s) ; // Close the outer box
213
213
eof ( & mut s. s ) ;
214
-
215
- // XXX(pcwalton): Need checked downcasts.
216
214
unsafe {
217
- let ( _, wr) : ( uint , ~MemWriter ) = cast:: transmute ( s. s . out ) ;
218
- let result = str:: from_utf8_owned ( wr. inner_ref ( ) . to_owned ( ) ) ;
219
- cast:: forget ( wr) ;
220
- result
215
+ get_mem_writer ( & mut s. s . out )
221
216
}
222
217
}
223
218
@@ -230,13 +225,8 @@ pub fn block_to_str(blk: &ast::Block, intr: @ident_interner) -> ~str {
230
225
ibox ( & mut s, 0 u) ;
231
226
print_block ( & mut s, blk) ;
232
227
eof ( & mut s. s ) ;
233
-
234
- // XXX(pcwalton): Need checked downcasts.
235
228
unsafe {
236
- let ( _, wr) : ( uint , ~MemWriter ) = cast:: transmute ( s. s . out ) ;
237
- let result = str:: from_utf8_owned ( wr. inner_ref ( ) . to_owned ( ) ) ;
238
- cast:: forget ( wr) ;
239
- result
229
+ get_mem_writer ( & mut s. s . out )
240
230
}
241
231
}
242
232
@@ -2318,17 +2308,23 @@ pub fn print_string(s: &mut ps, st: &str, style: ast::StrStyle) {
2318
2308
word ( & mut s. s , st) ;
2319
2309
}
2320
2310
2311
+ // XXX(pcwalton): A nasty function to extract the string from an `io::Writer`
2312
+ // that we "know" to be a `MemWriter` that works around the lack of checked
2313
+ // downcasts.
2314
+ unsafe fn get_mem_writer ( writer : & mut ~io:: Writer ) -> ~str {
2315
+ let ( _, wr) : ( uint , ~MemWriter ) = cast:: transmute_copy ( writer) ;
2316
+ let result = str:: from_utf8_owned ( wr. inner_ref ( ) . to_owned ( ) ) ;
2317
+ cast:: forget ( wr) ;
2318
+ result
2319
+ }
2320
+
2321
2321
pub fn to_str < T > ( t : & T , f : |& mut ps , & T |, intr: @ident_interner ) -> ~str {
2322
2322
let wr = ~MemWriter :: new ( ) ;
2323
2323
let mut s = rust_printer ( wr as ~io:: Writer , intr) ;
2324
2324
f ( & mut s, t) ;
2325
2325
eof ( & mut s. s ) ;
2326
- // XXX(pcwalton): Need checked downcasts.
2327
2326
unsafe {
2328
- let ( _, wr) : ( uint , ~MemWriter ) = cast:: transmute ( s. s . out ) ;
2329
- let result = str:: from_utf8_owned ( wr. inner_ref ( ) . to_owned ( ) ) ;
2330
- cast:: forget ( wr) ;
2331
- result
2327
+ get_mem_writer ( & mut s. s . out )
2332
2328
}
2333
2329
}
2334
2330
0 commit comments