Trait pretty_trait::Pretty [] [src]

pub trait Pretty {
    fn size(&self) -> Size;
fn pretty_write(&self, context: Context) -> Result<()>; }

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

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

Render this value in a given context.

Implementations on Foreign Types

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

[src]

[src]

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

[src]

[src]

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

[src]

[src]

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

[src]

[src]

impl<'a> Pretty for &'a str
[src]

[src]

[src]

impl Pretty for String
[src]

[src]

[src]

impl<T: Pretty> Pretty for Option<T>
[src]

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:

This example deliberately fails to compile
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",
]);

[src]

[src]

Implementors