Skip to main content

forest/db/
either.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use super::*;
5use spire_enum::prelude::delegated_enum;
6
7#[delegated_enum]
8pub enum Either<A: Blockstore, B: Blockstore> {
9    Left(A),
10    Right(B),
11}
12
13impl<A: Blockstore, B: Blockstore> Blockstore for Either<A, B> {
14    fn get(&self, k: &Cid) -> anyhow::Result<Option<Vec<u8>>> {
15        delegate_either!(self.get(k))
16    }
17
18    fn put_keyed(&self, k: &Cid, block: &[u8]) -> anyhow::Result<()> {
19        delegate_either!(self.put_keyed(k, block))
20    }
21
22    fn has(&self, k: &Cid) -> anyhow::Result<bool> {
23        delegate_either!(self.has(k))
24    }
25
26    #[allow(clippy::disallowed_types)]
27    fn put<D>(
28        &self,
29        mh_code: multihash_codetable::Code,
30        block: &fvm_ipld_blockstore::Block<D>,
31    ) -> anyhow::Result<Cid>
32    where
33        Self: Sized,
34        D: AsRef<[u8]>,
35    {
36        delegate_either!(self.put(mh_code, block))
37    }
38
39    #[allow(clippy::disallowed_types)]
40    fn put_many<D, I>(&self, blocks: I) -> anyhow::Result<()>
41    where
42        Self: Sized,
43        D: AsRef<[u8]>,
44        I: IntoIterator<Item = (multihash_codetable::Code, fvm_ipld_blockstore::Block<D>)>,
45    {
46        delegate_either!(self.put_many(blocks))
47    }
48
49    fn put_many_keyed<D, I>(&self, blocks: I) -> anyhow::Result<()>
50    where
51        Self: Sized,
52        D: AsRef<[u8]>,
53        I: IntoIterator<Item = (Cid, D)>,
54    {
55        delegate_either!(self.put_many_keyed(blocks))
56    }
57}