1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
1516use std::fmt::{Display, Error, Formatter};
1718use timely::progress::frontier::{Antichain, AntichainRef, MutableAntichain};
1920pub trait AntichainExt {
21type Pretty<'a>: Display
22where
23Self: 'a;
2425fn pretty(&self) -> Self::Pretty<'_>;
26}
2728impl<T: Display + 'static> AntichainExt for Antichain<T> {
29type Pretty<'a> = FrontierPrinter<AntichainRef<'a, T>>;
3031fn pretty(&self) -> Self::Pretty<'_> {
32self.borrow().pretty()
33 }
34}
3536impl<T: Display + 'static> AntichainExt for MutableAntichain<T> {
37type Pretty<'a> = FrontierPrinter<AntichainRef<'a, T>>;
3839fn pretty(&self) -> Self::Pretty<'_> {
40self.frontier().pretty()
41 }
42}
4344impl<'a, T: Display> AntichainExt for AntichainRef<'a, T> {
45type Pretty<'b>
46 = FrontierPrinter<Self>
47where
48Self: 'b;
4950fn pretty(&self) -> Self::Pretty<'a> {
51 FrontierPrinter(*self)
52 }
53}
5455pub struct FrontierPrinter<F>(F);
5657impl<'a, T: Display> Display for FrontierPrinter<AntichainRef<'a, T>> {
58fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
59 f.write_str("{")?;
60let mut time_iter = self.0.iter();
61if let Some(t) = time_iter.next() {
62 t.fmt(f)?;
63 }
64for t in time_iter {
65 f.write_str(", ")?;
66 t.fmt(f)?;
67 }
68 f.write_str("}")?;
69Ok(())
70 }
71}