pretty

Trait DocAllocator

Source
pub trait DocAllocator<'a, A = ()>
where A: 'a,
{ type Doc: DocPtr<'a, A>;
Show 19 methods // Required methods fn alloc(&'a self, doc: Doc<'a, Self::Doc, A>) -> Self::Doc; fn alloc_column_fn( &'a self, f: impl Fn(usize) -> Self::Doc + 'a, ) -> <Self::Doc as DocPtr<'a, A>>::ColumnFn; fn alloc_width_fn( &'a self, f: impl Fn(isize) -> Self::Doc + 'a, ) -> <Self::Doc as DocPtr<'a, A>>::WidthFn; // Provided methods fn alloc_cow(&'a self, doc: BuildDoc<'a, Self::Doc, A>) -> Self::Doc { ... } fn nil(&'a self) -> DocBuilder<'a, Self, A> { ... } fn fail(&'a self) -> DocBuilder<'a, Self, A> { ... } fn hardline(&'a self) -> DocBuilder<'a, Self, A> { ... } fn space(&'a self) -> DocBuilder<'a, Self, A> { ... } fn line(&'a self) -> DocBuilder<'a, Self, A> { ... } fn line_(&'a self) -> DocBuilder<'a, Self, A> { ... } fn softline(&'a self) -> DocBuilder<'a, Self, A> { ... } fn softline_(&'a self) -> DocBuilder<'a, Self, A> { ... } fn as_string<U: Display>(&'a self, data: U) -> DocBuilder<'a, Self, A> { ... } fn text<U: Into<Cow<'a, str>>>(&'a self, data: U) -> DocBuilder<'a, Self, A> { ... } fn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self, A> where I: IntoIterator, I::Item: Pretty<'a, Self, A> { ... } fn intersperse<I, S>( &'a self, docs: I, separator: S, ) -> DocBuilder<'a, Self, A> where I: IntoIterator, I::Item: Pretty<'a, Self, A>, S: Pretty<'a, Self, A> + Clone { ... } fn column( &'a self, f: impl Fn(usize) -> Self::Doc + 'a, ) -> DocBuilder<'a, Self, A> { ... } fn nesting( &'a self, f: impl Fn(usize) -> Self::Doc + 'a, ) -> DocBuilder<'a, Self, A> { ... } fn reflow(&'a self, text: &'a str) -> DocBuilder<'a, Self, A> where Self: Sized, Self::Doc: Clone, A: Clone { ... }
}
Expand description

The DocAllocator trait abstracts over a type which can allocate (pointers to) Doc.

Required Associated Types§

Source

type Doc: DocPtr<'a, A>

Required Methods§

Source

fn alloc(&'a self, doc: Doc<'a, Self::Doc, A>) -> Self::Doc

Source

fn alloc_column_fn( &'a self, f: impl Fn(usize) -> Self::Doc + 'a, ) -> <Self::Doc as DocPtr<'a, A>>::ColumnFn

Source

fn alloc_width_fn( &'a self, f: impl Fn(isize) -> Self::Doc + 'a, ) -> <Self::Doc as DocPtr<'a, A>>::WidthFn

Provided Methods§

Source

fn alloc_cow(&'a self, doc: BuildDoc<'a, Self::Doc, A>) -> Self::Doc

Source

fn nil(&'a self) -> DocBuilder<'a, Self, A>

Allocate an empty document.

Source

fn fail(&'a self) -> DocBuilder<'a, Self, A>

Fails document rendering immediately.

Primarily used to abort rendering inside the left side of Union

Source

fn hardline(&'a self) -> DocBuilder<'a, Self, A>

Allocate a single hardline.

Source

fn space(&'a self) -> DocBuilder<'a, Self, A>

Source

fn line(&'a self) -> DocBuilder<'a, Self, A>

A line acts like a \n but behaves like space if it is grouped on a single line.

Source

fn line_(&'a self) -> DocBuilder<'a, Self, A>

Acts like line but behaves like nil if grouped on a single line

use pretty::{Doc, RcDoc};

let doc = RcDoc::<()>::group(
    RcDoc::text("(")
        .append(
            RcDoc::line_()
                .append(Doc::text("test"))
                .append(Doc::line())
                .append(Doc::text("test"))
                .nest(2),
        )
        .append(Doc::line_())
        .append(Doc::text(")")),
);
assert_eq!(doc.pretty(5).to_string(), "(\n  test\n  test\n)");
assert_eq!(doc.pretty(100).to_string(), "(test test)");
Source

fn softline(&'a self) -> DocBuilder<'a, Self, A>

A softline acts like space if the document fits the page, otherwise like line

Source

fn softline_(&'a self) -> DocBuilder<'a, Self, A>

A softline_ acts like nil if the document fits the page, otherwise like line_

Source

fn as_string<U: Display>(&'a self, data: U) -> DocBuilder<'a, Self, A>

Allocate a document containing the text t.to_string().

The given text must not contain line breaks.

Source

fn text<U: Into<Cow<'a, str>>>(&'a self, data: U) -> DocBuilder<'a, Self, A>

Allocate a document containing the given text.

The given text must not contain line breaks.

Source

fn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self, A>
where I: IntoIterator, I::Item: Pretty<'a, Self, A>,

Allocate a document concatenating the given documents.

Source

fn intersperse<I, S>(&'a self, docs: I, separator: S) -> DocBuilder<'a, Self, A>
where I: IntoIterator, I::Item: Pretty<'a, Self, A>, S: Pretty<'a, Self, A> + Clone,

Allocate a document that intersperses the given separator S between the given documents [A, B, C, ..., Z], yielding [A, S, B, S, C, S, ..., S, Z].

Compare the intersperse method from the itertools crate.

NOTE: The separator type, S may need to be cloned. Consider using cheaply cloneable ptr like RefDoc or RcDoc

Source

fn column( &'a self, f: impl Fn(usize) -> Self::Doc + 'a, ) -> DocBuilder<'a, Self, A>

Allocate a document that acts differently based on the position and page layout

use pretty::DocAllocator;

let arena = pretty::Arena::<()>::new();
let doc = arena.text("prefix ")
    .append(arena.column(|l| {
        arena.text("| <- column ").append(arena.as_string(l)).into_doc()
    }));
assert_eq!(doc.1.pretty(80).to_string(), "prefix | <- column 7");
Source

fn nesting( &'a self, f: impl Fn(usize) -> Self::Doc + 'a, ) -> DocBuilder<'a, Self, A>

Allocate a document that acts differently based on the current nesting level

use pretty::DocAllocator;

let arena = pretty::Arena::<()>::new();
let doc = arena.text("prefix ")
    .append(arena.nesting(|l| {
        arena.text("[Nested: ").append(arena.as_string(l)).append("]").into_doc()
    }).nest(4));
assert_eq!(doc.1.pretty(80).to_string(), "prefix [Nested: 4]");
Source

fn reflow(&'a self, text: &'a str) -> DocBuilder<'a, Self, A>
where Self: Sized, Self::Doc: Clone, A: Clone,

Reflows text inserting softline in place of any whitespace

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'a, D, A> DocAllocator<'a, A> for &'a D
where D: ?Sized + DocAllocator<'a, A>, A: 'a,

Source§

type Doc = <D as DocAllocator<'a, A>>::Doc

Source§

fn alloc(&'a self, doc: Doc<'a, Self::Doc, A>) -> Self::Doc

Source§

fn alloc_column_fn( &'a self, f: impl Fn(usize) -> Self::Doc + 'a, ) -> <Self::Doc as DocPtr<'a, A>>::ColumnFn

Source§

fn alloc_width_fn( &'a self, f: impl Fn(isize) -> Self::Doc + 'a, ) -> <Self::Doc as DocPtr<'a, A>>::WidthFn

Implementors§

Source§

impl<'a, A> DocAllocator<'a, A> for Arena<'a, A>

Source§

type Doc = RefDoc<'a, A>

Source§

impl<'a, A> DocAllocator<'a, A> for BoxAllocator
where A: 'a,

Source§

type Doc = BoxDoc<'a, A>

Source§

impl<'a, A> DocAllocator<'a, A> for RcAllocator
where A: 'a,

Source§

type Doc = RcDoc<'a, A>