1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use std::{
cell::Cell,
fmt::{self, Display, Formatter},
};
pub struct DisplayFn<F: FnOnce(&mut Formatter) -> fmt::Result>(std::cell::Cell<Option<F>>);
impl<F: FnOnce(&mut Formatter) -> fmt::Result> DisplayFn<F> {
pub fn new(f: F) -> Self {
Self(Cell::new(Some(f)))
}
}
impl<F: FnOnce(&mut Formatter) -> fmt::Result> Display for DisplayFn<F> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.0.take().ok_or(fmt::Error).and_then(|cl| cl(f))
}
}