]> git.lizzy.rs Git - rust.git/blob - src/libstd/repr.rs
auto merge of #10977 : brson/rust/androidtest, r=brson
[rust.git] / src / libstd / repr.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /*!
12
13 More runtime type reflection
14
15 */
16
17 #[allow(missing_doc)];
18
19 use cast::transmute;
20 use char;
21 use container::Container;
22 use io;
23 use iter::Iterator;
24 use libc::c_void;
25 use option::{Some, None};
26 use ptr;
27 use reflect;
28 use reflect::{MovePtr, align};
29 use str::StrSlice;
30 use to_str::ToStr;
31 use vec::OwnedVector;
32 use unstable::intrinsics::{Disr, Opaque, TyDesc, TyVisitor, get_tydesc, visit_tydesc};
33 use unstable::raw;
34
35 /// Representations
36
37 trait Repr {
38     fn write_repr(&self, writer: &mut io::Writer);
39 }
40
41 impl Repr for () {
42     fn write_repr(&self, writer: &mut io::Writer) {
43         writer.write("()".as_bytes());
44     }
45 }
46
47 impl Repr for bool {
48     fn write_repr(&self, writer: &mut io::Writer) {
49         let s = if *self { "true" } else { "false" };
50         writer.write(s.as_bytes())
51     }
52 }
53
54 impl Repr for int {
55     fn write_repr(&self, writer: &mut io::Writer) {
56         ::int::to_str_bytes(*self, 10u, |bits| {
57             writer.write(bits);
58         })
59     }
60 }
61
62 macro_rules! int_repr(($ty:ident, $suffix:expr) => (impl Repr for $ty {
63     fn write_repr(&self, writer: &mut io::Writer) {
64         ::$ty::to_str_bytes(*self, 10u, |bits| {
65             writer.write(bits);
66             writer.write(bytes!($suffix));
67         })
68     }
69 }))
70
71 int_repr!(i8, "i8")
72 int_repr!(i16, "i16")
73 int_repr!(i32, "i32")
74 int_repr!(i64, "i64")
75 int_repr!(uint, "u")
76 int_repr!(u8, "u8")
77 int_repr!(u16, "u16")
78 int_repr!(u32, "u32")
79 int_repr!(u64, "u64")
80
81 macro_rules! num_repr(($ty:ident, $suffix:expr) => (impl Repr for $ty {
82     fn write_repr(&self, writer: &mut io::Writer) {
83         let s = self.to_str();
84         writer.write(s.as_bytes());
85         writer.write(bytes!($suffix));
86     }
87 }))
88
89 num_repr!(f32, "f32")
90 num_repr!(f64, "f64")
91
92 // New implementation using reflect::MovePtr
93
94 enum VariantState {
95     SearchingFor(Disr),
96     Matched,
97     AlreadyFound
98 }
99
100 pub struct ReprVisitor<'a> {
101     priv ptr: *c_void,
102     priv ptr_stk: ~[*c_void],
103     priv var_stk: ~[VariantState],
104     priv writer: &'a mut io::Writer
105 }
106
107 pub fn ReprVisitor<'a>(ptr: *c_void,
108                        writer: &'a mut io::Writer) -> ReprVisitor<'a> {
109     ReprVisitor {
110         ptr: ptr,
111         ptr_stk: ~[],
112         var_stk: ~[],
113         writer: writer,
114     }
115 }
116
117 impl<'a> MovePtr for ReprVisitor<'a> {
118     #[inline]
119     fn move_ptr(&mut self, adjustment: |*c_void| -> *c_void) {
120         self.ptr = adjustment(self.ptr);
121     }
122     fn push_ptr(&mut self) {
123         self.ptr_stk.push(self.ptr);
124     }
125     fn pop_ptr(&mut self) {
126         self.ptr = self.ptr_stk.pop();
127     }
128 }
129
130 impl<'a> ReprVisitor<'a> {
131     // Various helpers for the TyVisitor impl
132
133     #[inline]
134     pub fn get<T>(&mut self, f: |&mut ReprVisitor, &T|) -> bool {
135         unsafe {
136             f(self, transmute::<*c_void,&T>(self.ptr));
137         }
138         true
139     }
140
141     #[inline]
142     pub fn visit_inner(&mut self, inner: *TyDesc) -> bool {
143         self.visit_ptr_inner(self.ptr, inner)
144     }
145
146     #[inline]
147     pub fn visit_ptr_inner(&mut self, ptr: *c_void, inner: *TyDesc) -> bool {
148         unsafe {
149             // This should call the constructor up above, but due to limiting
150             // issues we have to recreate it here.
151             let u = ReprVisitor {
152                 ptr: ptr,
153                 ptr_stk: ~[],
154                 var_stk: ~[],
155                 writer: ::cast::transmute_copy(&self.writer),
156             };
157             let mut v = reflect::MovePtrAdaptor(u);
158             // Obviously this should not be a thing, but blame #8401 for now
159             visit_tydesc(inner, &mut v as &mut TyVisitor);
160             true
161         }
162     }
163
164     #[inline]
165     pub fn write<T:Repr>(&mut self) -> bool {
166         self.get(|this, v:&T| {
167             v.write_repr(unsafe { ::cast::transmute_copy(&this.writer) });
168         })
169     }
170
171     pub fn write_escaped_slice(&mut self, slice: &str) {
172         self.writer.write(['"' as u8]);
173         for ch in slice.chars() {
174             self.write_escaped_char(ch, true);
175         }
176         self.writer.write(['"' as u8]);
177     }
178
179     pub fn write_mut_qualifier(&mut self, mtbl: uint) {
180         if mtbl == 0 {
181             self.writer.write("mut ".as_bytes());
182         } else if mtbl == 1 {
183             // skip, this is ast::m_imm
184         } else {
185             fail!("invalid mutability value");
186         }
187     }
188
189     pub fn write_vec_range(&mut self, ptr: *(), len: uint, inner: *TyDesc) -> bool {
190         let mut p = ptr as *u8;
191         let (sz, al) = unsafe { ((*inner).size, (*inner).align) };
192         self.writer.write(['[' as u8]);
193         let mut first = true;
194         let mut left = len;
195         // unit structs have 0 size, and don't loop forever.
196         let dec = if sz == 0 {1} else {sz};
197         while left > 0 {
198             if first {
199                 first = false;
200             } else {
201                 self.writer.write(", ".as_bytes());
202             }
203             self.visit_ptr_inner(p as *c_void, inner);
204             p = align(unsafe { ptr::offset(p, sz as int) as uint }, al) as *u8;
205             left -= dec;
206         }
207         self.writer.write([']' as u8]);
208         true
209     }
210
211     pub fn write_unboxed_vec_repr(&mut self, _: uint, v: &raw::Vec<()>, inner: *TyDesc) -> bool {
212         self.write_vec_range(ptr::to_unsafe_ptr(&v.data), v.fill, inner)
213     }
214
215     fn write_escaped_char(&mut self, ch: char, is_str: bool) {
216         match ch {
217             '\t' => self.writer.write("\\t".as_bytes()),
218             '\r' => self.writer.write("\\r".as_bytes()),
219             '\n' => self.writer.write("\\n".as_bytes()),
220             '\\' => self.writer.write("\\\\".as_bytes()),
221             '\'' => {
222                 if is_str {
223                     self.writer.write("'".as_bytes())
224                 } else {
225                     self.writer.write("\\'".as_bytes())
226                 }
227             }
228             '"' => {
229                 if is_str {
230                     self.writer.write("\\\"".as_bytes())
231                 } else {
232                     self.writer.write("\"".as_bytes())
233                 }
234             }
235             '\x20'..'\x7e' => self.writer.write([ch as u8]),
236             _ => {
237                 char::escape_unicode(ch, |c| {
238                     self.writer.write([c as u8]);
239                 })
240             }
241         }
242     }
243 }
244
245 impl<'a> TyVisitor for ReprVisitor<'a> {
246     fn visit_bot(&mut self) -> bool {
247         self.writer.write("!".as_bytes());
248         true
249     }
250     fn visit_nil(&mut self) -> bool { self.write::<()>() }
251     fn visit_bool(&mut self) -> bool { self.write::<bool>() }
252     fn visit_int(&mut self) -> bool { self.write::<int>() }
253     fn visit_i8(&mut self) -> bool { self.write::<i8>() }
254     fn visit_i16(&mut self) -> bool { self.write::<i16>() }
255     fn visit_i32(&mut self) -> bool { self.write::<i32>()  }
256     fn visit_i64(&mut self) -> bool { self.write::<i64>() }
257
258     fn visit_uint(&mut self) -> bool { self.write::<uint>() }
259     fn visit_u8(&mut self) -> bool { self.write::<u8>() }
260     fn visit_u16(&mut self) -> bool { self.write::<u16>() }
261     fn visit_u32(&mut self) -> bool { self.write::<u32>() }
262     fn visit_u64(&mut self) -> bool { self.write::<u64>() }
263
264     fn visit_f32(&mut self) -> bool { self.write::<f32>() }
265     fn visit_f64(&mut self) -> bool { self.write::<f64>() }
266
267     fn visit_char(&mut self) -> bool {
268         self.get::<char>(|this, &ch| {
269             this.writer.write(['\'' as u8]);
270             this.write_escaped_char(ch, false);
271             this.writer.write(['\'' as u8]);
272         })
273     }
274
275     fn visit_estr_box(&mut self) -> bool {
276         self.get::<@str>(|this, s| {
277             this.writer.write(['@' as u8]);
278             this.write_escaped_slice(*s);
279         })
280     }
281
282     fn visit_estr_uniq(&mut self) -> bool {
283         self.get::<~str>(|this, s| {
284             this.writer.write(['~' as u8]);
285             this.write_escaped_slice(*s);
286         })
287     }
288
289     fn visit_estr_slice(&mut self) -> bool {
290         self.get::<&str>(|this, s| this.write_escaped_slice(*s))
291     }
292
293     // Type no longer exists, vestigial function.
294     fn visit_estr_fixed(&mut self, _n: uint, _sz: uint,
295                         _align: uint) -> bool { fail!(); }
296
297     fn visit_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
298         self.writer.write(['@' as u8]);
299         self.write_mut_qualifier(mtbl);
300         self.get::<&raw::Box<()>>(|this, b| {
301             let p = ptr::to_unsafe_ptr(&b.data) as *c_void;
302             this.visit_ptr_inner(p, inner);
303         })
304     }
305
306     fn visit_uniq(&mut self, _mtbl: uint, inner: *TyDesc) -> bool {
307         self.writer.write(['~' as u8]);
308         self.get::<*c_void>(|this, b| {
309             this.visit_ptr_inner(*b, inner);
310         })
311     }
312
313     fn visit_uniq_managed(&mut self, _mtbl: uint, inner: *TyDesc) -> bool {
314         self.writer.write(['~' as u8]);
315         self.get::<&raw::Box<()>>(|this, b| {
316             let p = ptr::to_unsafe_ptr(&b.data) as *c_void;
317             this.visit_ptr_inner(p, inner);
318         })
319     }
320
321     fn visit_ptr(&mut self, mtbl: uint, _inner: *TyDesc) -> bool {
322         self.get::<*c_void>(|this, p| {
323             write!(this.writer, "({} as *", *p);
324             this.write_mut_qualifier(mtbl);
325             this.writer.write("())".as_bytes());
326         })
327     }
328
329     fn visit_rptr(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
330         self.writer.write(['&' as u8]);
331         self.write_mut_qualifier(mtbl);
332         self.get::<*c_void>(|this, p| {
333             this.visit_ptr_inner(*p, inner);
334         })
335     }
336
337     // Type no longer exists, vestigial function.
338     fn visit_vec(&mut self, _mtbl: uint, _inner: *TyDesc) -> bool { fail!(); }
339
340     fn visit_unboxed_vec(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
341         self.get::<raw::Vec<()>>(|this, b| {
342             this.write_unboxed_vec_repr(mtbl, b, inner);
343         })
344     }
345
346     fn visit_evec_box(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
347         self.get::<&raw::Box<raw::Vec<()>>>(|this, b| {
348             this.writer.write(['@' as u8]);
349             this.write_mut_qualifier(mtbl);
350             this.write_unboxed_vec_repr(mtbl, &b.data, inner);
351         })
352     }
353
354     fn visit_evec_uniq(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
355         self.get::<&raw::Vec<()>>(|this, b| {
356             this.writer.write(['~' as u8]);
357             this.write_unboxed_vec_repr(mtbl, *b, inner);
358         })
359     }
360
361     fn visit_evec_uniq_managed(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
362         self.get::<&raw::Box<raw::Vec<()>>>(|this, b| {
363             this.writer.write(['~' as u8]);
364             this.write_unboxed_vec_repr(mtbl, &b.data, inner);
365         })
366     }
367
368     fn visit_evec_slice(&mut self, mtbl: uint, inner: *TyDesc) -> bool {
369         self.get::<raw::Slice<()>>(|this, s| {
370             this.writer.write(['&' as u8]);
371             this.write_mut_qualifier(mtbl);
372             let size = unsafe {
373                 if (*inner).size == 0 { 1 } else { (*inner).size }
374             };
375             this.write_vec_range(s.data, s.len * size, inner);
376         })
377     }
378
379     fn visit_evec_fixed(&mut self, n: uint, sz: uint, _align: uint,
380                         _: uint, inner: *TyDesc) -> bool {
381         let assumed_size = if sz == 0 { n } else { sz };
382         self.get::<()>(|this, b| {
383             this.write_vec_range(ptr::to_unsafe_ptr(b), assumed_size, inner);
384         })
385     }
386
387     fn visit_enter_rec(&mut self, _n_fields: uint,
388                        _sz: uint, _align: uint) -> bool {
389         self.writer.write(['{' as u8]);
390         true
391     }
392
393     fn visit_rec_field(&mut self, i: uint, name: &str,
394                        mtbl: uint, inner: *TyDesc) -> bool {
395         if i != 0 {
396             self.writer.write(", ".as_bytes());
397         }
398         self.write_mut_qualifier(mtbl);
399         self.writer.write(name.as_bytes());
400         self.writer.write(": ".as_bytes());
401         self.visit_inner(inner);
402         true
403     }
404
405     fn visit_leave_rec(&mut self, _n_fields: uint,
406                        _sz: uint, _align: uint) -> bool {
407         self.writer.write(['}' as u8]);
408         true
409     }
410
411     fn visit_enter_class(&mut self, name: &str, named_fields: bool, n_fields: uint,
412                          _sz: uint, _align: uint) -> bool {
413         self.writer.write(name.as_bytes());
414         if n_fields != 0 {
415             if named_fields {
416                 self.writer.write(['{' as u8]);
417             } else {
418                 self.writer.write(['(' as u8]);
419             }
420         }
421         true
422     }
423
424     fn visit_class_field(&mut self, i: uint, name: &str, named: bool,
425                          _mtbl: uint, inner: *TyDesc) -> bool {
426         if i != 0 {
427             self.writer.write(", ".as_bytes());
428         }
429         if named {
430             self.writer.write(name.as_bytes());
431             self.writer.write(": ".as_bytes());
432         }
433         self.visit_inner(inner);
434         true
435     }
436
437     fn visit_leave_class(&mut self, _name: &str, named_fields: bool, n_fields: uint,
438                          _sz: uint, _align: uint) -> bool {
439         if n_fields != 0 {
440             if named_fields {
441                 self.writer.write(['}' as u8]);
442             } else {
443                 self.writer.write([')' as u8]);
444             }
445         }
446         true
447     }
448
449     fn visit_enter_tup(&mut self, _n_fields: uint,
450                        _sz: uint, _align: uint) -> bool {
451         self.writer.write(['(' as u8]);
452         true
453     }
454
455     fn visit_tup_field(&mut self, i: uint, inner: *TyDesc) -> bool {
456         if i != 0 {
457             self.writer.write(", ".as_bytes());
458         }
459         self.visit_inner(inner);
460         true
461     }
462
463     fn visit_leave_tup(&mut self, _n_fields: uint,
464                        _sz: uint, _align: uint) -> bool {
465         if _n_fields == 1 {
466             self.writer.write([',' as u8]);
467         }
468         self.writer.write([')' as u8]);
469         true
470     }
471
472     fn visit_enter_enum(&mut self,
473                         _n_variants: uint,
474                         get_disr: extern unsafe fn(ptr: *Opaque) -> Disr,
475                         _sz: uint,
476                         _align: uint) -> bool {
477         let disr = unsafe {
478             get_disr(transmute(self.ptr))
479         };
480         self.var_stk.push(SearchingFor(disr));
481         true
482     }
483
484     fn visit_enter_enum_variant(&mut self, _variant: uint,
485                                 disr_val: Disr,
486                                 n_fields: uint,
487                                 name: &str) -> bool {
488         let mut write = false;
489         match self.var_stk.pop() {
490             SearchingFor(sought) => {
491                 if disr_val == sought {
492                     self.var_stk.push(Matched);
493                     write = true;
494                 } else {
495                     self.var_stk.push(SearchingFor(sought));
496                 }
497             }
498             Matched | AlreadyFound => {
499                 self.var_stk.push(AlreadyFound);
500             }
501         }
502
503         if write {
504             self.writer.write(name.as_bytes());
505             if n_fields > 0 {
506                 self.writer.write(['(' as u8]);
507             }
508         }
509         true
510     }
511
512     fn visit_enum_variant_field(&mut self,
513                                 i: uint,
514                                 _offset: uint,
515                                 inner: *TyDesc)
516                                 -> bool {
517         match self.var_stk[self.var_stk.len() - 1] {
518             Matched => {
519                 if i != 0 {
520                     self.writer.write(", ".as_bytes());
521                 }
522                 if ! self.visit_inner(inner) {
523                     return false;
524                 }
525             }
526             _ => ()
527         }
528         true
529     }
530
531     fn visit_leave_enum_variant(&mut self, _variant: uint,
532                                 _disr_val: Disr,
533                                 n_fields: uint,
534                                 _name: &str) -> bool {
535         match self.var_stk[self.var_stk.len() - 1] {
536             Matched => {
537                 if n_fields > 0 {
538                     self.writer.write([')' as u8]);
539                 }
540             }
541             _ => ()
542         }
543         true
544     }
545
546     fn visit_leave_enum(&mut self,
547                         _n_variants: uint,
548                         _get_disr: extern unsafe fn(ptr: *Opaque) -> Disr,
549                         _sz: uint,
550                         _align: uint)
551                         -> bool {
552         match self.var_stk.pop() {
553             SearchingFor(..) => fail!("enum value matched no variant"),
554             _ => true
555         }
556     }
557
558     fn visit_enter_fn(&mut self, _purity: uint, _proto: uint,
559                       _n_inputs: uint, _retstyle: uint) -> bool {
560         self.writer.write("fn(".as_bytes());
561         true
562     }
563
564     fn visit_fn_input(&mut self, i: uint, _mode: uint, inner: *TyDesc) -> bool {
565         if i != 0 {
566             self.writer.write(", ".as_bytes());
567         }
568         let name = unsafe { (*inner).name };
569         self.writer.write(name.as_bytes());
570         true
571     }
572
573     fn visit_fn_output(&mut self, _retstyle: uint, variadic: bool, inner: *TyDesc) -> bool {
574         if variadic {
575             self.writer.write(", ...".as_bytes());
576         }
577         self.writer.write(")".as_bytes());
578         let name = unsafe { (*inner).name };
579         if name != "()" {
580             self.writer.write(" -> ".as_bytes());
581             self.writer.write(name.as_bytes());
582         }
583         true
584     }
585
586     fn visit_leave_fn(&mut self, _purity: uint, _proto: uint,
587                       _n_inputs: uint, _retstyle: uint) -> bool { true }
588
589
590     fn visit_trait(&mut self, name: &str) -> bool {
591         self.writer.write(name.as_bytes());
592         true
593     }
594
595     fn visit_param(&mut self, _i: uint) -> bool { true }
596     fn visit_self(&mut self) -> bool { true }
597     fn visit_type(&mut self) -> bool { true }
598
599     fn visit_opaque_box(&mut self) -> bool {
600         self.writer.write(['@' as u8]);
601         self.get::<&raw::Box<()>>(|this, b| {
602             let p = ptr::to_unsafe_ptr(&b.data) as *c_void;
603             this.visit_ptr_inner(p, b.type_desc);
604         })
605     }
606
607     fn visit_closure_ptr(&mut self, _ck: uint) -> bool { true }
608 }
609
610 pub fn write_repr<T>(writer: &mut io::Writer, object: &T) {
611     unsafe {
612         let ptr = ptr::to_unsafe_ptr(object) as *c_void;
613         let tydesc = get_tydesc::<T>();
614         let u = ReprVisitor(ptr, writer);
615         let mut v = reflect::MovePtrAdaptor(u);
616         visit_tydesc(tydesc, &mut v as &mut TyVisitor);
617     }
618 }
619
620 pub fn repr_to_str<T>(t: &T) -> ~str {
621     use str;
622     use io;
623     use io::Decorator;
624
625     let mut result = io::mem::MemWriter::new();
626     write_repr(&mut result as &mut io::Writer, t);
627     str::from_utf8_owned(result.inner())
628 }
629
630 #[cfg(test)]
631 struct P {a: int, b: f64}
632
633 #[test]
634 fn test_repr() {
635     use prelude::*;
636     use str;
637     use str::Str;
638     use io::Decorator;
639     use util::swap;
640     use char::is_alphabetic;
641
642     fn exact_test<T>(t: &T, e:&str) {
643         let mut m = io::mem::MemWriter::new();
644         write_repr(&mut m as &mut io::Writer, t);
645         let s = str::from_utf8_owned(m.inner());
646         assert_eq!(s.as_slice(), e);
647     }
648
649     exact_test(&10, "10");
650     exact_test(&true, "true");
651     exact_test(&false, "false");
652     exact_test(&1.234, "1.234f64");
653     exact_test(&(&"hello"), "\"hello\"");
654     exact_test(&(@"hello"), "@\"hello\"");
655     exact_test(&(~"he\u10f3llo"), "~\"he\\u10f3llo\"");
656
657     exact_test(&(@10), "@10");
658     exact_test(&(@mut 10), "@mut 10");
659     exact_test(&((@mut 10, 2)), "(@mut 10, 2)");
660     exact_test(&(~10), "~10");
661     exact_test(&(&10), "&10");
662     let mut x = 10;
663     exact_test(&(&mut x), "&mut 10");
664     exact_test(&(@mut [1, 2]), "@mut [1, 2]");
665
666     exact_test(&(0 as *()), "(0x0 as *())");
667     exact_test(&(0 as *mut ()), "(0x0 as *mut ())");
668
669     exact_test(&(1,), "(1,)");
670     exact_test(&(@[1,2,3,4,5,6,7,8]),
671                "@[1, 2, 3, 4, 5, 6, 7, 8]");
672     exact_test(&(@[1u8,2u8,3u8,4u8]),
673                "@[1u8, 2u8, 3u8, 4u8]");
674     exact_test(&(@["hi", "there"]),
675                "@[\"hi\", \"there\"]");
676     exact_test(&(~["hi", "there"]),
677                "~[\"hi\", \"there\"]");
678     exact_test(&(&["hi", "there"]),
679                "&[\"hi\", \"there\"]");
680     exact_test(&(P{a:10, b:1.234}),
681                "repr::P{a: 10, b: 1.234f64}");
682     exact_test(&(@P{a:10, b:1.234}),
683                "@repr::P{a: 10, b: 1.234f64}");
684     exact_test(&(~P{a:10, b:1.234}),
685                "~repr::P{a: 10, b: 1.234f64}");
686     exact_test(&(10u8, ~"hello"),
687                "(10u8, ~\"hello\")");
688     exact_test(&(10u16, ~"hello"),
689                "(10u16, ~\"hello\")");
690     exact_test(&(10u32, ~"hello"),
691                "(10u32, ~\"hello\")");
692     exact_test(&(10u64, ~"hello"),
693                "(10u64, ~\"hello\")");
694
695     exact_test(&(&[1, 2]), "&[1, 2]");
696     exact_test(&(&mut [1, 2]), "&mut [1, 2]");
697
698     exact_test(&'\'', "'\\''");
699     exact_test(&'"', "'\"'");
700     exact_test(&("'"), "\"'\"");
701     exact_test(&("\""), "\"\\\"\"");
702
703     exact_test(&println, "fn(&str)");
704     exact_test(&swap::<int>, "fn(&mut int, &mut int)");
705     exact_test(&is_alphabetic, "fn(char) -> bool");
706     exact_test(&(~5 as ~ToStr), "~to_str::ToStr:Send");
707
708     struct Foo;
709     exact_test(&(~[Foo, Foo]), "~[repr::test_repr::Foo, repr::test_repr::Foo]");
710
711     struct Bar(int, int);
712     exact_test(&(Bar(2, 2)), "repr::test_repr::Bar(2, 2)");
713 }