Skip to content

Commit d8b6919

Browse files
committed
std::fmt: prepare to convert the formatting traits to methods, and work
around the lack of UFCS. The further work is pending a snapshot, to avoid putting #[cfg(stage0)] attributes on all the traits and duplicating them.
1 parent 003ce50 commit d8b6919

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/libstd/fmt/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,41 @@ pub trait LowerExp { fn fmt(&Self, &mut Formatter); }
606606
#[allow(missing_doc)]
607607
pub trait UpperExp { fn fmt(&Self, &mut Formatter); }
608608

609+
// FIXME #11938 - UFCS would make us able call the above methods
610+
// directly Show::show(x, fmt).
611+
612+
// FIXME(huonw's WIP): this is a intermediate state waiting for a
613+
// snapshot (at the time of writing we're at 2014-01-20 b6400f9), to
614+
// be able to make the `fmt` functions into normal methods and have
615+
// `format!()` still work.
616+
macro_rules! uniform_fn_call_workaround {
617+
($( $name: ident, $trait_: ident; )*) => {
618+
$(
619+
#[doc(hidden)]
620+
pub fn $name<T: $trait_>(x: &T, fmt: &mut Formatter) {
621+
$trait_::fmt(x, fmt)
622+
}
623+
)*
624+
}
625+
}
626+
uniform_fn_call_workaround! {
627+
secret_show, Show;
628+
secret_bool, Bool;
629+
secret_char, Char;
630+
secret_signed, Signed;
631+
secret_unsigned, Unsigned;
632+
secret_octal, Octal;
633+
secret_binary, Binary;
634+
secret_lower_hex, LowerHex;
635+
secret_upper_hex, UpperHex;
636+
secret_string, String;
637+
secret_poly, Poly;
638+
secret_pointer, Pointer;
639+
secret_float, Float;
640+
secret_lower_exp, LowerExp;
641+
secret_upper_exp, UpperExp;
642+
}
643+
609644
/// The `write` function takes an output stream, a precompiled format string,
610645
/// and a list of arguments. The arguments will be formatted according to the
611646
/// specified format string into the output stream provided.

src/libsyntax/ext/format.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ impl<'a> Context<'a> {
702702
Named(ref s) => self.name_types.get(s)
703703
};
704704

705-
let fmt_trait = match *ty {
705+
let fmt_fn = match *ty {
706706
Known(ref tyname) => {
707707
match tyname.as_slice() {
708708
"" => "secret_show",
@@ -721,10 +721,9 @@ impl<'a> Context<'a> {
721721
"x" => "secret_lower_hex",
722722
"X" => "secret_upper_hex",
723723
_ => {
724-
self.ecx.span_err(sp,
725-
format!("unknown format trait `{}`",
726-
*tyname));
727-
"Dummy"
724+
self.ecx.span_err(sp, format!("unknown format trait `{}`",
725+
*tyname));
726+
"dummy"
728727
}
729728
}
730729
}
@@ -747,8 +746,7 @@ impl<'a> Context<'a> {
747746
let format_fn = self.ecx.path_global(sp, ~[
748747
self.ecx.ident_of("std"),
749748
self.ecx.ident_of("fmt"),
750-
self.ecx.ident_of(fmt_trait),
751-
self.ecx.ident_of("fmt"),
749+
self.ecx.ident_of(fmt_fn),
752750
]);
753751
self.ecx.expr_call_global(sp, ~[
754752
self.ecx.ident_of("std"),

0 commit comments

Comments
 (0)