Pretty

Trait Pretty 

Source
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§

Source

fn size(&self) -> Size

Calculate the intrinsic size of this value, if it were to be displayed on a single line.

Source

fn pretty_write(&self, context: Context<'_>) -> Result<()>

Render this value in a given context.

Implementations on Foreign Types§

Source§

impl Pretty for String

Source§

fn size(&self) -> Size

Source§

fn pretty_write(&self, context: Context<'_>) -> Result<()>

Source§

impl<'a> Pretty for &'a str

Source§

fn size(&self) -> Size

Source§

fn pretty_write(&self, context: Context<'_>) -> Result<()>

Source§

impl<'a, T: Pretty + ?Sized> Pretty for &'a T

Source§

fn size(&self) -> Size

Source§

fn pretty_write(&self, context: Context<'_>) -> Result<()>

Source§

impl<'a, T: Pretty + ?Sized> Pretty for &'a mut T

Source§

fn size(&self) -> Size

Source§

fn pretty_write(&self, context: Context<'_>) -> Result<()>

Source§

impl<'a, T: Pretty + ?Sized> Pretty for Box<T>

Source§

fn size(&self) -> Size

Source§

fn pretty_write(&self, context: Context<'_>) -> Result<()>

Source§

impl<'a, T: Pretty + ?Sized> Pretty for Rc<T>

Source§

fn size(&self) -> Size

Source§

fn pretty_write(&self, context: Context<'_>) -> Result<()>

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.

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",
]);
Source§

fn size(&self) -> Size

Source§

fn pretty_write(&self, context: Context<'_>) -> Result<()>

Implementors§

Source§

impl Pretty for Newline

Source§

impl Pretty for Sep

Source§

impl<T: Pretty> Pretty for Conditional<T>

Source§

impl<T: Pretty> Pretty for Group<T>

Source§

impl<T: Pretty> Pretty for Indent<T>

Source§

impl<T: Pretty> Pretty for Seq<T>

Source§

impl<T: Pretty, U: Pretty> Pretty for Join<T, U>