Closed
Description
Issue
There is currently no (obvious) way to create a Formatter
.
Example scenario
In my case, this is a problem because I'd like to display an element that requires informations from external data; I'd thus like to do something like this:
pub trait ContextDependantDisplay {
fn display(&self, f: &mut Formatter, data: &Data) -> fmt::Result;
fn to_string(&self, data: &Data) -> String {
let mut formatter = Formatter::new();
self.display(&mut f, data).expect("...");
format!("{}", formatter)
}
}
However, there is currently no way to create a Formatter
, so I had to use a little hack instead:
fn to_string(&self, data: &Data) -> String {
let fmt = {
struct ManualDisplay<'a>(&'a ContextDependantDisplay, &'a Data);
impl<'a> Display for ManualDisplay<'a> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.0.display(f, self.1)
}
}
ManualDisplay(self, data)
};
format!("{}", fmt)
}
I also noticed there is an existing issue about it, which requests the following thing:
Formatter
should show where it's created, and talk about the details more.
Meta
rustc 1.24.0-nightly (5a2465e2b 2017-12-06)
binary: rustc
commit-hash: 5a2465e2b44ecdd3b5d835c0abe29e9a4c9dcfe4
commit-date: 2017-12-06
host: x86_64-unknown-linux-gnu
release: 1.24.0-nightly
LLVM version: 4.0