pub trait Pretty {
// Required methods
fn size(&self) -> Size;
fn pretty_write(&self, context: Context<'_>) -> Result<()>;
}Expand description
Types which can be pretty-printed.
Strings implement Pretty, as do a number of useful built-in composable wrapper types. As
such, you usually don’t need to implement it for your own types, although you can if necessary.
You usually do not need to directly call the methods defined here, unless your are implementing
your own Pretty type. If you just want to render a value to a buffer or an IO handle, use one
of the write, println_simple, or to_string functions instead.
Required Methods§
Sourcefn size(&self) -> Size
fn size(&self) -> Size
Calculate the intrinsic size of this value, if it were to be displayed on a single line.
Sourcefn pretty_write(&self, context: Context<'_>) -> Result<()>
fn pretty_write(&self, context: Context<'_>) -> Result<()>
Render this value in a given context.
Implementations on Foreign Types§
Source§impl<T: Pretty> Pretty for Option<T>
An Option will render its contents if it is Some, or an empty string if it is None.
impl<T: Pretty> Pretty for Option<T>
An Option will render its contents if it is Some, or an empty string if it is None.
This is useful when you need multiple pretty values to have the same type, even though they are not all of exactly the same form when rendered.
§Examples
Basic usage:
use pretty_trait::{JoinExt, Seq, Sep, to_string};
let tab_size = 4;
assert_eq!(
to_string(
&Seq(vec![
"lorem".join(Some(",".join(Sep(1)))),
"ipsum".join(Some(",".join(Sep(1)))),
"dolor".join(Some(",".join(Sep(1)))),
"sit".join(Some(",".join(Sep(1)))),
"amet".join(None),
]),
None,
tab_size,
),
"lorem, ipsum, dolor, sit, amet"
);If the above example were modified so that it did not use Options, it would not compile
because the last item in the Seq would have a mismatched type:
Seq(vec![
"lorem".join(",".join(Some(Sep(1)))),
"ipsum".join(",".join(Some(Sep(1)))),
"dolor".join(",".join(Some(Sep(1)))),
"sit".join(",".join(Some(Sep(1)))),
"amet",
]);