1 //! Render utilities which don't belong anywhere else.
2 use std
::fmt
::{Display, Formatter, Result}
;
4 pub fn as_display
<F
: Fn(&mut Formatter
<'_
>) -> Result
>(f
: F
) -> impl Display
{
5 struct ClosureDisplay
<F
: Fn(&mut Formatter
<'_
>) -> Result
>(F
);
7 impl<F
: Fn(&mut Formatter
<'_
>) -> Result
> Display
for ClosureDisplay
<F
> {
8 fn fmt(&self, f
: &mut Formatter
<'_
>) -> Result
{
16 macro_rules
! write_joined_non_empty_list
{
17 ($f
:expr
,$template
:tt
,$list
:expr
,$sep
:expr
) => {{
18 let mut x
= $list
.into_iter().peekable();
19 if x
.peek().is_some() {
20 write
!($f
, $template
, x
.format($sep
))
27 /// Processes a name given by an [`Interner`][chalk_ir::interner::Interner] debug
28 /// method into something usable by the `display` module.
30 /// This is specifically useful when implementing
31 /// [`RustIrDatabase`][crate::RustIrDatabase] `name_*` methods.
32 pub fn sanitize_debug_name(func
: impl Fn(&mut Formatter
<'_
>) -> Option
<Result
>) -> String
{
35 // First, write the debug method contents to a String.
36 let mut debug_out
= String
::new();
37 // ignore if the result is `None`, as we can just as easily tell by looking
38 // to see if anything was written to `debug_out`.
42 as_display(|fmt
| { func(fmt).unwrap_or(Ok(())) }
)
44 .expect("expected writing to a String to succeed");
45 if debug_out
.is_empty() {
46 return "Unknown".to_owned();
49 // now the actual sanitization
50 debug_out
.replace(|c
: char| !c
.is_ascii_alphanumeric(), "_")