mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-07 17:18:32 +03:00
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.
This commit is contained in:
@@ -606,6 +606,41 @@ pub struct Arguments<'a> {
|
||||
#[allow(missing_doc)]
|
||||
pub trait UpperExp { fn fmt(&Self, &mut Formatter); }
|
||||
|
||||
// FIXME #11938 - UFCS would make us able call the above methods
|
||||
// directly Show::show(x, fmt).
|
||||
|
||||
// FIXME(huonw's WIP): this is a intermediate state waiting for a
|
||||
// snapshot (at the time of writing we're at 2014-01-20 b6400f9), to
|
||||
// be able to make the `fmt` functions into normal methods and have
|
||||
// `format!()` still work.
|
||||
macro_rules! uniform_fn_call_workaround {
|
||||
($( $name: ident, $trait_: ident; )*) => {
|
||||
$(
|
||||
#[doc(hidden)]
|
||||
pub fn $name<T: $trait_>(x: &T, fmt: &mut Formatter) {
|
||||
$trait_::fmt(x, fmt)
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
uniform_fn_call_workaround! {
|
||||
secret_show, Show;
|
||||
secret_bool, Bool;
|
||||
secret_char, Char;
|
||||
secret_signed, Signed;
|
||||
secret_unsigned, Unsigned;
|
||||
secret_octal, Octal;
|
||||
secret_binary, Binary;
|
||||
secret_lower_hex, LowerHex;
|
||||
secret_upper_hex, UpperHex;
|
||||
secret_string, String;
|
||||
secret_poly, Poly;
|
||||
secret_pointer, Pointer;
|
||||
secret_float, Float;
|
||||
secret_lower_exp, LowerExp;
|
||||
secret_upper_exp, UpperExp;
|
||||
}
|
||||
|
||||
/// The `write` function takes an output stream, a precompiled format string,
|
||||
/// and a list of arguments. The arguments will be formatted according to the
|
||||
/// specified format string into the output stream provided.
|
||||
|
||||
@@ -702,7 +702,7 @@ fn format_arg(&self, sp: Span, argno: Position, arg: @ast::Expr)
|
||||
Named(ref s) => self.name_types.get(s)
|
||||
};
|
||||
|
||||
let fmt_trait = match *ty {
|
||||
let fmt_fn = match *ty {
|
||||
Known(ref tyname) => {
|
||||
match tyname.as_slice() {
|
||||
"" => "secret_show",
|
||||
@@ -721,10 +721,9 @@ fn format_arg(&self, sp: Span, argno: Position, arg: @ast::Expr)
|
||||
"x" => "secret_lower_hex",
|
||||
"X" => "secret_upper_hex",
|
||||
_ => {
|
||||
self.ecx.span_err(sp,
|
||||
format!("unknown format trait `{}`",
|
||||
*tyname));
|
||||
"Dummy"
|
||||
self.ecx.span_err(sp, format!("unknown format trait `{}`",
|
||||
*tyname));
|
||||
"dummy"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -747,8 +746,7 @@ fn format_arg(&self, sp: Span, argno: Position, arg: @ast::Expr)
|
||||
let format_fn = self.ecx.path_global(sp, ~[
|
||||
self.ecx.ident_of("std"),
|
||||
self.ecx.ident_of("fmt"),
|
||||
self.ecx.ident_of(fmt_trait),
|
||||
self.ecx.ident_of("fmt"),
|
||||
self.ecx.ident_of(fmt_fn),
|
||||
]);
|
||||
self.ecx.expr_call_global(sp, ~[
|
||||
self.ecx.ident_of("std"),
|
||||
|
||||
Reference in New Issue
Block a user