bee_tangle/
unreferenced_message.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use bee_common::packable::{Packable, Read, Write};
5use bee_message::MessageId;
6
7use std::ops::Deref;
8
9/// A type representing an unreferenced message.
10#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
11pub struct UnreferencedMessage(MessageId);
12
13impl From<MessageId> for UnreferencedMessage {
14    fn from(message_id: MessageId) -> Self {
15        Self(message_id)
16    }
17}
18
19impl Deref for UnreferencedMessage {
20    type Target = MessageId;
21
22    fn deref(&self) -> &Self::Target {
23        &self.0
24    }
25}
26
27impl UnreferencedMessage {
28    /// Create a new `UnreferencedMessage`.
29    pub fn new(message_id: MessageId) -> Self {
30        message_id.into()
31    }
32
33    /// Get the message ID of this unreferenced message.
34    pub fn message_id(&self) -> &MessageId {
35        &self.0
36    }
37}
38
39impl Packable for UnreferencedMessage {
40    type Error = <MessageId as Packable>::Error;
41
42    fn packed_len(&self) -> usize {
43        self.0.packed_len()
44    }
45
46    fn pack<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
47        self.0.pack(writer)
48    }
49
50    fn unpack_inner<R: Read + ?Sized, const CHECK: bool>(reader: &mut R) -> Result<Self, Self::Error> {
51        Ok(Self(MessageId::unpack_inner::<R, CHECK>(reader)?))
52    }
53}