bee_ledger/types/
consumed_output.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::types::error::Error;
5
6use bee_common::packable::{Packable, Read, Write};
7use bee_message::{milestone::MilestoneIndex, payload::transaction::TransactionId};
8
9/// Represents a newly consumed output.
10#[derive(Clone, Debug, Eq, PartialEq)]
11pub struct ConsumedOutput {
12    target: TransactionId,
13    index: MilestoneIndex,
14}
15
16impl ConsumedOutput {
17    /// Creates a new `ConsumedOutput`.
18    pub fn new(target: TransactionId, index: MilestoneIndex) -> Self {
19        Self { target, index }
20    }
21
22    /// Returns the target transaction of the `ConsumedOutput`.
23    pub fn target(&self) -> &TransactionId {
24        &self.target
25    }
26
27    /// Returns the milestone index of the `ConsumedOutput`.
28    pub fn index(&self) -> MilestoneIndex {
29        self.index
30    }
31}
32
33impl Packable for ConsumedOutput {
34    type Error = Error;
35
36    fn packed_len(&self) -> usize {
37        self.target.packed_len() + self.index.packed_len()
38    }
39
40    fn pack<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
41        self.target.pack(writer)?;
42        self.index.pack(writer)?;
43
44        Ok(())
45    }
46
47    fn unpack_inner<R: Read + ?Sized, const CHECK: bool>(reader: &mut R) -> Result<Self, Self::Error> {
48        let target = TransactionId::unpack_inner::<R, CHECK>(reader)?;
49        let index = MilestoneIndex::unpack_inner::<R, CHECK>(reader)?;
50
51        Ok(Self { target, index })
52    }
53}