pub trait DocAllocator<'a> {
type Doc: DocPtr<'a>;
Show 21 methods
// Required method
fn alloc(&'a self, doc: Doc<'a, Self::Doc>) -> Self::Doc;
// Provided methods
fn alloc_cow(&'a self, doc: BuildDoc<'a, Self::Doc>) -> Self::Doc { ... }
fn nil(&'a self) -> DocBuilder<'a, Self> { ... }
fn fail(&'a self) -> DocBuilder<'a, Self> { ... }
fn hardline(&'a self) -> DocBuilder<'a, Self> { ... }
fn space(&'a self) -> DocBuilder<'a, Self> { ... }
fn line(&'a self) -> DocBuilder<'a, Self> { ... }
fn line_(&'a self) -> DocBuilder<'a, Self> { ... }
fn softline(&'a self) -> DocBuilder<'a, Self> { ... }
fn softline_(&'a self) -> DocBuilder<'a, Self> { ... }
fn if_group_flat(
&'a self,
doc: impl Pretty<'a, Self>,
) -> DocBuilder<'a, Self> { ... }
fn if_group_break(
&'a self,
doc: impl Pretty<'a, Self>,
) -> DocBuilder<'a, Self> { ... }
fn expand_parent(&'a self) -> DocBuilder<'a, Self> { ... }
fn as_string<U>(&'a self, data: U) -> DocBuilder<'a, Self>
where U: Display { ... }
fn text<U>(&'a self, data: U) -> DocBuilder<'a, Self>
where U: Into<Cow<'a, str>> { ... }
fn ascii_text<U>(&'a self, data: U) -> DocBuilder<'a, Self>
where U: Into<Cow<'a, str>> { ... }
fn spaces(&'a self, n: usize) -> DocBuilder<'a, Self> { ... }
fn line_suffix(&'a self, doc: impl Pretty<'a, Self>) -> DocBuilder<'a, Self> { ... }
fn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self>
where I: IntoIterator,
<I as IntoIterator>::Item: Pretty<'a, Self> { ... }
fn intersperse<I, S>(
&'a self,
docs: I,
separator: S,
) -> DocBuilder<'a, Self>
where I: IntoIterator,
<I as IntoIterator>::Item: Pretty<'a, Self>,
S: Pretty<'a, Self> + Clone { ... }
fn reflow(&'a self, text: &'a str) -> DocBuilder<'a, Self>
where Self: Sized,
Self::Doc: Clone { ... }
}
Expand description
The DocAllocator
trait abstracts over a type which can allocate (pointers to) Doc
.
Required Associated Types§
Required Methods§
Provided Methods§
fn alloc_cow(&'a self, doc: BuildDoc<'a, Self::Doc>) -> Self::Doc
Sourcefn nil(&'a self) -> DocBuilder<'a, Self>
fn nil(&'a self) -> DocBuilder<'a, Self>
Allocate an empty document.
Sourcefn fail(&'a self) -> DocBuilder<'a, Self>
fn fail(&'a self) -> DocBuilder<'a, Self>
Fails document rendering immediately.
Primarily used to abort rendering inside the left side of Union
Sourcefn hardline(&'a self) -> DocBuilder<'a, Self>
fn hardline(&'a self) -> DocBuilder<'a, Self>
Allocate a single hardline.
fn space(&'a self) -> DocBuilder<'a, Self>
Sourcefn line(&'a self) -> DocBuilder<'a, Self>
fn line(&'a self) -> DocBuilder<'a, Self>
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>
fn line_(&'a self) -> DocBuilder<'a, Self>
Acts like line
but behaves like nil
if grouped on a single line
use prettyless::{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.print(5).to_string(), "(\n test\n test\n)");
assert_eq!(doc.print(100).to_string(), "(test test)");
Sourcefn softline(&'a self) -> DocBuilder<'a, Self>
fn softline(&'a self) -> DocBuilder<'a, Self>
A softline
acts like space
if the document fits the page, otherwise like line
Sourcefn softline_(&'a self) -> DocBuilder<'a, Self>
fn softline_(&'a self) -> DocBuilder<'a, Self>
A softline_
acts like nil
if the document fits the page, otherwise like line_
Sourcefn if_group_flat(&'a self, doc: impl Pretty<'a, Self>) -> DocBuilder<'a, Self>
fn if_group_flat(&'a self, doc: impl Pretty<'a, Self>) -> DocBuilder<'a, Self>
Equivalent to self.nil().flat_alt(doc.pretty(self))
Sourcefn if_group_break(&'a self, doc: impl Pretty<'a, Self>) -> DocBuilder<'a, Self>
fn if_group_break(&'a self, doc: impl Pretty<'a, Self>) -> DocBuilder<'a, Self>
Equivalent to doc.pretty(self).flat_alt(self.nil())
Sourcefn expand_parent(&'a self) -> DocBuilder<'a, Self>
fn expand_parent(&'a self) -> DocBuilder<'a, Self>
Make the parent group break
use prettyless::DocAllocator;
let arena = prettyless::Arena::new();
let doc = (arena.line() + arena.expand_parent()).group();
assert_eq!(doc.print(80).to_string(), "\n");
Sourcefn as_string<U>(&'a self, data: U) -> DocBuilder<'a, Self>where
U: Display,
fn as_string<U>(&'a self, data: U) -> DocBuilder<'a, Self>where
U: Display,
Allocate a document containing the text t.to_string()
.
The given text must not contain line breaks.
Sourcefn text<U>(&'a self, data: U) -> DocBuilder<'a, Self>
fn text<U>(&'a self, data: U) -> DocBuilder<'a, Self>
Allocate a document containing the given text.
The given text must not contain line breaks.
Sourcefn ascii_text<U>(&'a self, data: U) -> DocBuilder<'a, Self>
fn ascii_text<U>(&'a self, data: U) -> DocBuilder<'a, Self>
Allocate a document containing the given text, which is assumed to be ASCII.
This can avoid checking for unicode width.
Sourcefn spaces(&'a self, n: usize) -> DocBuilder<'a, Self>
fn spaces(&'a self, n: usize) -> DocBuilder<'a, Self>
Allocate a document containing n spaces.
Sourcefn line_suffix(&'a self, doc: impl Pretty<'a, Self>) -> DocBuilder<'a, Self>
fn line_suffix(&'a self, doc: impl Pretty<'a, Self>) -> DocBuilder<'a, Self>
Pushes some content to the end of the current line.
Multiple line_suffix
calls accumulate in order and are all flushed together when a line
break occurs or rendering ends.
use prettyless::{Arena, DocAllocator};
let arena = Arena::new();
let doc = (arena.text("a")
+ arena.line_suffix(" // 1")
+ arena.line()
+ arena.line_suffix(" // 2")
+ arena.text("b"))
.group();
assert_eq!(doc.print(5).to_string(), "a b // 1 // 2");
let doc = arena.text("a")
+ arena.line_suffix(" // 1")
+ arena.hardline()
+ arena.line_suffix(" // 2")
+ arena.text("b");
assert_eq!(doc.print(5).to_string(), "a // 1\nb // 2");
Sourcefn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self>
fn concat<I>(&'a self, docs: I) -> DocBuilder<'a, Self>
Allocate a document concatenating the given documents.
Sourcefn intersperse<I, S>(&'a self, docs: I, separator: S) -> DocBuilder<'a, Self>
fn intersperse<I, S>(&'a self, docs: I, separator: S) -> DocBuilder<'a, Self>
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
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.