Enum pretty::Doc

source ·
pub enum Doc<'a, T, A = ()> {
    Nil,
    Append(T, T),
    Group(T),
    Nest(usize, T),
    Space,
    Newline,
    Text(Cow<'a, str>),
    Annotated(A, T),
}
Expand description

The concrete document type. This type is not meant to be used directly. Instead use the static functions on Doc or the methods on an DocAllocator.

The T parameter is used to abstract over pointers to Doc. See RefDoc and BoxDoc for how it is used

Variants§

§

Nil

§

Append(T, T)

§

Group(T)

§

Nest(usize, T)

§

Space

§

Newline

§

Text(Cow<'a, str>)

§

Annotated(A, T)

Implementations§

source§

impl<'a, T, A> Doc<'a, T, A>

source

pub fn nil() -> Doc<'a, T, A>

An empty document.

source

pub fn as_string<U: ToString>(data: U) -> Doc<'a, T, A>

The text t.to_string().

The given text must not contain line breaks.

source

pub fn newline() -> Doc<'a, T, A>

A single newline.

source

pub fn text<U: Into<Cow<'a, str>>>(data: U) -> Doc<'a, T, A>

The given text, which must not contain line breaks.

source

pub fn space() -> Doc<'a, T, A>

A space.

source§

impl<'a, A> Doc<'a, BoxDoc<'a, A>, A>

source

pub fn append<D>(self, that: D) -> Doc<'a, BoxDoc<'a, A>, A>
where D: Into<Doc<'a, BoxDoc<'a, A>, A>>,

Append the given document after this document.

source

pub fn concat<I>(docs: I) -> Doc<'a, BoxDoc<'a, A>, A>
where I: IntoIterator, I::Item: Into<Doc<'a, BoxDoc<'a, A>, A>>,

A single document concatenating all the given documents.

source

pub fn intersperse<I, S>(docs: I, separator: S) -> Doc<'a, BoxDoc<'a, A>, A>
where I: IntoIterator, I::Item: Into<Doc<'a, BoxDoc<'a, A>, A>>, S: Into<Doc<'a, BoxDoc<'a, A>, A>> + Clone, A: Clone,

A single document interspersing the given separator S between the given documents. For example, if the documents are [A, B, C, ..., Z], this yields [A, S, B, S, C, S, ..., S, Z].

Compare the intersperse method from the itertools crate.

source

pub fn group(self) -> Doc<'a, BoxDoc<'a, A>, A>

Mark this document as a group.

Groups are layed out on a single line if possible. Within a group, all basic documents with several possible layouts are assigned the same layout, that is, they are all layed out horizontally and combined into a one single line, or they are each layed out on their own line.

source

pub fn nest(self, offset: usize) -> Doc<'a, BoxDoc<'a, A>, A>

Increase the indentation level of this document.

source

pub fn annotate(self, ann: A) -> Doc<'a, BoxDoc<'a, A>, A>

source§

impl<'a, T, A> Doc<'a, T, A>

source

pub fn render<'b, W>(&'b self, width: usize, out: &mut W) -> Result<()>
where T: Deref<Target = Doc<'b, T, A>>, W: ?Sized + Write,

Writes a rendered document to a std::io::Write object.

source

pub fn render_fmt<'b, W>(&'b self, width: usize, out: &mut W) -> Result
where T: Deref<Target = Doc<'b, T, A>>, W: ?Sized + Write,

Writes a rendered document to a std::fmt::Write object.

source

pub fn render_raw<'b, W>( &'b self, width: usize, out: &mut W ) -> Result<(), W::Error>
where T: Deref<Target = Doc<'b, T, A>>, W: ?Sized + RenderAnnotated<A>,

Writes a rendered document to a RenderAnnotated<A> object.

source

pub fn pretty<'b>(&'b self, width: usize) -> Pretty<'b, T, A>
where T: Deref<Target = Doc<'b, T, A>>,

Returns a value which implements std::fmt::Display

use pretty::Doc;
let doc = Doc::<_>::group(
    Doc::text("hello").append(Doc::space()).append(Doc::text("world"))
);
assert_eq!(format!("{}", doc.pretty(80)), "hello world");

Trait Implementations§

source§

impl<'a, T: Clone, A: Clone> Clone for Doc<'a, T, A>

source§

fn clone(&self) -> Doc<'a, T, A>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a, T: Debug, A: Debug> Debug for Doc<'a, T, A>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, T, A, S> From<S> for Doc<'a, T, A>
where S: Into<Cow<'a, str>>,

source§

fn from(s: S) -> Doc<'a, T, A>

Converts to this type from the input type.
source§

impl<'a, D, A> Into<Doc<'a, <D as DocAllocator<'a, A>>::Doc, A>> for DocBuilder<'a, D, A>
where D: ?Sized + DocAllocator<'a, A>,

source§

fn into(self) -> Doc<'a, D::Doc, A>

Converts this type into the (usually inferred) input type.
source§

impl<'a, T: Ord, A: Ord> Ord for Doc<'a, T, A>

source§

fn cmp(&self, other: &Doc<'a, T, A>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<'a, T: PartialEq, A: PartialEq> PartialEq for Doc<'a, T, A>

source§

fn eq(&self, other: &Doc<'a, T, A>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, T: PartialOrd, A: PartialOrd> PartialOrd for Doc<'a, T, A>

source§

fn partial_cmp(&self, other: &Doc<'a, T, A>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a, T: Eq, A: Eq> Eq for Doc<'a, T, A>

source§

impl<'a, T, A> StructuralPartialEq for Doc<'a, T, A>

Auto Trait Implementations§

§

impl<'a, T, A> Freeze for Doc<'a, T, A>
where T: Freeze, A: Freeze,

§

impl<'a, T, A> RefUnwindSafe for Doc<'a, T, A>

§

impl<'a, T, A> Send for Doc<'a, T, A>
where T: Send, A: Send,

§

impl<'a, T, A> Sync for Doc<'a, T, A>
where T: Sync, A: Sync,

§

impl<'a, T, A> Unpin for Doc<'a, T, A>
where T: Unpin, A: Unpin,

§

impl<'a, T, A> UnwindSafe for Doc<'a, T, A>
where T: UnwindSafe, A: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.