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§
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
sourcefn nil(&'a self) -> DocBuilder<'a, Self, A>
fn nil(&'a self) -> DocBuilder<'a, Self, A>
Allocate an empty document.
sourcefn fail(&'a self) -> DocBuilder<'a, Self, A>
fn fail(&'a self) -> DocBuilder<'a, Self, A>
Fails document rendering immediately.
Primarily used to abort rendering inside the left side of Union
sourcefn hardline(&'a self) -> DocBuilder<'a, Self, A>
fn hardline(&'a self) -> DocBuilder<'a, Self, A>
Allocate a single hardline.
fn space(&'a self) -> DocBuilder<'a, Self, A>
sourcefn line(&'a self) -> DocBuilder<'a, Self, A>
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.
sourcefn line_(&'a self) -> DocBuilder<'a, Self, A>
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)");
sourcefn softline(&'a self) -> DocBuilder<'a, Self, A>
fn softline(&'a self) -> DocBuilder<'a, Self, A>
A softline
acts like space
if the document fits the page, otherwise like line
sourcefn softline_(&'a self) -> DocBuilder<'a, Self, A>
fn softline_(&'a self) -> DocBuilder<'a, Self, A>
A softline_
acts like nil
if the document fits the page, otherwise like line_
sourcefn as_string<U: Display>(&'a self, data: U) -> DocBuilder<'a, Self, A>
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.
sourcefn text<U: Into<Cow<'a, str>>>(&'a self, data: U) -> DocBuilder<'a, Self, A>
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.
sourcefn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self, A>
fn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self, A>
Allocate a document concatenating the given documents.
sourcefn intersperse<I, S>(&'a self, docs: I, separator: S) -> DocBuilder<'a, Self, A>
fn intersperse<I, S>(&'a self, docs: I, separator: S) -> DocBuilder<'a, Self, A>
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
sourcefn column(
&'a self,
f: impl Fn(usize) -> Self::Doc + 'a,
) -> DocBuilder<'a, Self, A>
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");
sourcefn nesting(
&'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>
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]");