Trait DocAllocator

Source
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§

Source

type Doc: DocPtr<'a>

Required Methods§

Source

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

Provided Methods§

Source

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

Source

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

Allocate an empty document.

Source

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

Fails document rendering immediately.

Primarily used to abort rendering inside the left side of Union

Source

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

Allocate a single hardline.

Source

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

Source

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.

Source

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)");
Source

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

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

Source

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

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

Source

fn if_group_flat(&'a self, doc: impl Pretty<'a, Self>) -> DocBuilder<'a, Self>

Equivalent to self.nil().flat_alt(doc.pretty(self))

Source

fn if_group_break(&'a self, doc: impl Pretty<'a, Self>) -> DocBuilder<'a, Self>

Equivalent to doc.pretty(self).flat_alt(self.nil())

Source

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");
Source

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.

Source

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

Allocate a document containing the given text.

The given text must not contain line breaks.

Source

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

Allocate a document containing the given text, which is assumed to be ASCII.

This can avoid checking for unicode width.

Source

fn spaces(&'a self, n: usize) -> DocBuilder<'a, Self>

Allocate a document containing n spaces.

Source

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");
Source

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

Allocate a document concatenating the given documents.

Source

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,

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 reflow(&'a self, text: &'a str) -> DocBuilder<'a, Self>
where Self: Sized, Self::Doc: 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> DocAllocator<'a> for &'a D
where D: DocAllocator<'a> + ?Sized,

Source§

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

Source§

fn alloc( &'a self, doc: Doc<'a, <&'a D as DocAllocator<'a>>::Doc>, ) -> <&'a D as DocAllocator<'a>>::Doc

Implementors§

Source§

impl<'a> DocAllocator<'a> for BoxAllocator

Source§

type Doc = BoxDoc<'a>

Source§

impl<'a> DocAllocator<'a> for RcAllocator

Source§

type Doc = RcDoc<'a>

Source§

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

Source§

type Doc = RefDoc<'a>