Trait pretty::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

Implementations on Foreign Types§

source§

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

§

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>

§

type Doc = RefDoc<'a, A>

source§

impl<'a, A> DocAllocator<'a, A> for BoxAllocatorwhere A: 'a,

§

type Doc = BoxDoc<'a, A>

source§

impl<'a, A> DocAllocator<'a, A> for RcAllocatorwhere A: 'a,

§

type Doc = RcDoc<'a, A>