hotshot_query_service/
resolvable.rs

1// Copyright (c) 2022 Espresso Systems (espressosys.com)
2// This file is part of the HotShot Query Service library.
3//
4// This program is free software: you can redistribute it and/or modify it under the terms of the GNU
5// General Public License as published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9// General Public License for more details.
10// You should have received a copy of the GNU General Public License along with this program. If not,
11// see <https://www.gnu.org/licenses/>.
12
13use committable::{Commitment, Committable};
14use either::Either;
15
16/// A reference to a `T` which can be resolved into a whole `T`.
17pub trait Resolvable<T: Committable>: Sized {
18    /// Get the underlying object if it is available without blocking.
19    fn try_resolve(self) -> Result<T, Self>;
20    /// Get a commitment to the underlying object.
21    fn commitment(&self) -> Commitment<T>;
22}
23
24impl<T: Committable> Resolvable<T> for T {
25    fn try_resolve(self) -> Result<T, Self> {
26        Ok(self)
27    }
28
29    fn commitment(&self) -> Commitment<T> {
30        self.commit()
31    }
32}
33
34impl<T: Committable> Resolvable<T> for Either<T, Commitment<T>> {
35    fn try_resolve(self) -> Result<T, Self> {
36        match self {
37            Either::Left(t) => Ok(t),
38            Either::Right(c) => Err(Either::Right(c)),
39        }
40    }
41
42    fn commitment(&self) -> Commitment<T> {
43        match self {
44            Either::Left(t) => t.commit(),
45            Either::Right(c) => *c,
46        }
47    }
48}