Skip to content

Commit 2a3dd4b

Browse files
authored
Fix pretty-print implementation to not use transmute. (#541)
1 parent b056bcf commit 2a3dd4b

File tree

2 files changed

+23
-66
lines changed

2 files changed

+23
-66
lines changed

partiql-ast/src/pretty.rs

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -697,60 +697,32 @@ impl PrettyDoc for Struct {
697697
D::Doc: Clone,
698698
A: Clone,
699699
{
700-
let wrapped = self.fields.iter().map(|p| unsafe {
701-
let x: &'b StructExprPair = std::mem::transmute(p);
702-
x
703-
});
704-
pretty_seq(wrapped, "{", "}", ",", PRETTY_INDENT_MINOR_NEST, arena)
705-
}
706-
}
707-
708-
pub struct StructExprPair(pub ExprPair);
709-
710-
impl PrettyDoc for StructExprPair {
711-
fn pretty_doc<'b, D, A>(&'b self, arena: &'b D) -> DocBuilder<'b, D, A>
712-
where
713-
D: DocAllocator<'b, A>,
714-
D::Doc: Clone,
715-
A: Clone,
716-
{
717-
let k = self.0.first.pretty_doc(arena);
718-
let v = self.0.second.pretty_doc(arena);
719-
let sep = arena.text(": ");
720-
721-
k.append(sep).group().append(v).group()
722-
}
723-
}
700+
let fields = self.fields.iter().map(|expr_pair| {
701+
let k = expr_pair.first.pretty_doc(arena);
702+
let v = expr_pair.second.pretty_doc(arena);
703+
let sep = arena.text(": ");
724704

725-
impl PrettyDoc for StructLit {
726-
fn pretty_doc<'b, D, A>(&'b self, arena: &'b D) -> DocBuilder<'b, D, A>
727-
where
728-
D: DocAllocator<'b, A>,
729-
D::Doc: Clone,
730-
A: Clone,
731-
{
732-
let wrapped = self.fields.iter().map(|p| unsafe {
733-
let x: &'b StructLitField = std::mem::transmute(p);
734-
x
705+
k.append(sep).group().append(v).group()
735706
});
736-
pretty_seq(wrapped, "{", "}", ",", PRETTY_INDENT_MINOR_NEST, arena)
707+
pretty_seq_doc(fields, "{", None, "}", ",", PRETTY_INDENT_MINOR_NEST, arena)
737708
}
738709
}
739710

740-
pub struct StructLitField(pub LitField);
741-
742-
impl PrettyDoc for StructLitField {
711+
impl PrettyDoc for StructLit {
743712
fn pretty_doc<'b, D, A>(&'b self, arena: &'b D) -> DocBuilder<'b, D, A>
744713
where
745714
D: DocAllocator<'b, A>,
746715
D::Doc: Clone,
747716
A: Clone,
748717
{
749-
let k = self.0.first.pretty_doc(arena);
750-
let v = self.0.second.pretty_doc(arena);
751-
let sep = arena.text(": ");
718+
let fields = self.fields.iter().map(|expr_pair| {
719+
let k = expr_pair.first.pretty_doc(arena);
720+
let v = expr_pair.second.pretty_doc(arena);
721+
let sep = arena.text(": ");
752722

753-
k.append(sep).group().append(v).group()
723+
k.append(sep).group().append(v).group()
724+
});
725+
pretty_seq_doc(fields, "{", None, "}", ",", PRETTY_INDENT_MINOR_NEST, arena)
754726
}
755727
}
756728

partiql-value/src/pretty.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{Bag, DateTime, List, Tuple, Value};
22
use partiql_common::pretty::{
3-
pretty_prefixed_doc, pretty_seq, pretty_surrounded, PrettyDoc, PRETTY_INDENT_MINOR_NEST,
3+
pretty_prefixed_doc, pretty_seq, pretty_seq_doc, pretty_surrounded, PrettyDoc,
4+
PRETTY_INDENT_MINOR_NEST,
45
};
56
use pretty::{DocAllocator, DocBuilder};
67

@@ -84,36 +85,20 @@ impl PrettyDoc for Bag {
8485
}
8586

8687
impl PrettyDoc for Tuple {
87-
#[inline]
8888
fn pretty_doc<'b, D, A>(&'b self, arena: &'b D) -> DocBuilder<'b, D, A>
8989
where
9090
D: DocAllocator<'b, A>,
9191
D::Doc: Clone,
9292
A: Clone,
9393
{
94-
let wrapped = self.pairs().map(|p| unsafe {
95-
let x: &'b StructValuePair<'b> = std::mem::transmute(&p);
96-
x
97-
});
98-
pretty_seq(wrapped, "{", "}", ",", PRETTY_INDENT_MINOR_NEST, arena)
99-
}
100-
}
94+
let seq = self.pairs().map(|(k, v)| {
95+
let k = k.pretty_doc(arena);
96+
let v = v.pretty_doc(arena);
97+
let sep = arena.text(": ");
10198

102-
pub struct StructValuePair<'a>((&'a String, &'a Value));
103-
104-
impl PrettyDoc for StructValuePair<'_> {
105-
fn pretty_doc<'b, D, A>(&'b self, arena: &'b D) -> DocBuilder<'b, D, A>
106-
where
107-
D: DocAllocator<'b, A>,
108-
D::Doc: Clone,
109-
A: Clone,
110-
{
111-
let (k, v) = self.0;
112-
let k = k.pretty_doc(arena);
113-
let v = v.pretty_doc(arena);
114-
let sep = arena.text(": ");
115-
116-
k.append(sep).group().append(v).group()
99+
k.append(sep).group().append(v).group()
100+
});
101+
pretty_seq_doc(seq, "{", None, "}", ",", PRETTY_INDENT_MINOR_NEST, arena)
117102
}
118103
}
119104

0 commit comments

Comments
 (0)