Make code generic over allocator
This way, I can use the same code with any allocator I want. More importantly, I can also use the nice DocBuilder-exclusive helper functions!
This commit is contained in:
parent
1a3772e6f7
commit
2ba56f0c92
4 changed files with 45 additions and 43 deletions
|
|
@ -1,37 +1,36 @@
|
|||
use pretty::RcDoc;
|
||||
use pretty::{DocAllocator, DocBuilder};
|
||||
|
||||
use crate::ast::Separated;
|
||||
|
||||
impl<E, S1, S2> Separated<E, S1, S2> {
|
||||
pub fn to_doc<FE, FS1, FS2>(
|
||||
&self,
|
||||
pub fn pretty<'a, D, FE, FS1, FS2>(
|
||||
self,
|
||||
allocator: &'a D,
|
||||
elem_to_doc: FE,
|
||||
separator_to_doc: FS1,
|
||||
trailing_separator_to_doc: FS2,
|
||||
) -> RcDoc
|
||||
) -> DocBuilder<'a, D>
|
||||
where
|
||||
FE: Fn(&E) -> RcDoc,
|
||||
FS1: Fn(&S1) -> RcDoc,
|
||||
FS2: Fn(&S2) -> RcDoc,
|
||||
D: DocAllocator<'a>,
|
||||
FE: Fn(&'a D, E) -> DocBuilder<'a, D>,
|
||||
FS1: Fn(&'a D, S1) -> DocBuilder<'a, D>,
|
||||
FS2: Fn(&'a D, S2) -> DocBuilder<'a, D>,
|
||||
{
|
||||
match self {
|
||||
Separated::Empty(_) => RcDoc::nil(),
|
||||
Separated::Empty(_) => allocator.nil(),
|
||||
Separated::NonEmpty {
|
||||
first_elem,
|
||||
last_elems,
|
||||
trailing,
|
||||
span: _span,
|
||||
} => elem_to_doc(first_elem)
|
||||
.append(RcDoc::concat(
|
||||
last_elems
|
||||
.iter()
|
||||
.map(|(s, e)| separator_to_doc(s).append(elem_to_doc(e))),
|
||||
))
|
||||
} => elem_to_doc(allocator, first_elem)
|
||||
.append(allocator.concat(last_elems.into_iter().map(|(s, e)| {
|
||||
separator_to_doc(allocator, s).append(elem_to_doc(allocator, e))
|
||||
})))
|
||||
.append(
|
||||
trailing
|
||||
.as_ref()
|
||||
.map(trailing_separator_to_doc)
|
||||
.unwrap_or_else(RcDoc::nil),
|
||||
.map(|s| trailing_separator_to_doc(allocator, s))
|
||||
.unwrap_or_else(|| allocator.nil()),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue