mz_timely_util/
antichain.rs

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.
15
16use std::fmt::{Display, Error, Formatter};
17
18use timely::progress::frontier::{Antichain, AntichainRef, MutableAntichain};
19
20pub trait AntichainExt {
21    type Pretty<'a>: Display
22    where
23        Self: 'a;
24
25    fn pretty(&self) -> Self::Pretty<'_>;
26}
27
28impl<T: Display + 'static> AntichainExt for Antichain<T> {
29    type Pretty<'a> = FrontierPrinter<AntichainRef<'a, T>>;
30
31    fn pretty(&self) -> Self::Pretty<'_> {
32        self.borrow().pretty()
33    }
34}
35
36impl<T: Display + 'static> AntichainExt for MutableAntichain<T> {
37    type Pretty<'a> = FrontierPrinter<AntichainRef<'a, T>>;
38
39    fn pretty(&self) -> Self::Pretty<'_> {
40        self.frontier().pretty()
41    }
42}
43
44impl<'a, T: Display> AntichainExt for AntichainRef<'a, T> {
45    type Pretty<'b>
46        = FrontierPrinter<Self>
47    where
48        Self: 'b;
49
50    fn pretty(&self) -> Self::Pretty<'a> {
51        FrontierPrinter(*self)
52    }
53}
54
55pub struct FrontierPrinter<F>(F);
56
57impl<'a, T: Display> Display for FrontierPrinter<AntichainRef<'a, T>> {
58    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
59        f.write_str("{")?;
60        let mut time_iter = self.0.iter();
61        if let Some(t) = time_iter.next() {
62            t.fmt(f)?;
63        }
64        for t in time_iter {
65            f.write_str(", ")?;
66            t.fmt(f)?;
67        }
68        f.write_str("}")?;
69        Ok(())
70    }
71}