bee_tangle/
solid_entry_point.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4//! A SolidEntryPoint is a [`MessageId`](bee_message::MessageId) of a message that is solid even if we do not have them
5//! or their past in the database. They often come from a snapshot file and allow a node to solidify
6//! without needing the full tangle history.
7
8use bee_common::packable::{Packable, Read, Write};
9use bee_message::MessageId;
10
11use ref_cast::RefCast;
12
13use core::{convert::AsRef, ops::Deref};
14
15/// A SolidEntryPoint is a [`MessageId`] of a message that is solid even if we do not have them
16/// or their past in the database. They often come from a snapshot file and allow a node to solidify
17/// without needing the full tangle history.
18#[derive(RefCast)]
19#[repr(transparent)]
20#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
21pub struct SolidEntryPoint(MessageId);
22
23impl SolidEntryPoint {
24    /// Create a `SolidEntryPoint` from an existing `MessageId`.
25    pub fn new(message_id: MessageId) -> Self {
26        message_id.into()
27    }
28
29    /// Create a null `SolidEntryPoint` (the zero-message).
30    pub fn null() -> Self {
31        Self(MessageId::null())
32    }
33
34    /// Returns the underlying `MessageId`.
35    pub fn message_id(&self) -> &MessageId {
36        &self.0
37    }
38}
39
40impl From<MessageId> for SolidEntryPoint {
41    fn from(message_id: MessageId) -> Self {
42        Self(message_id)
43    }
44}
45
46impl AsRef<MessageId> for SolidEntryPoint {
47    fn as_ref(&self) -> &MessageId {
48        &self.0
49    }
50}
51
52impl Deref for SolidEntryPoint {
53    type Target = MessageId;
54
55    fn deref(&self) -> &Self::Target {
56        &self.0
57    }
58}
59
60impl Packable for SolidEntryPoint {
61    type Error = <MessageId as Packable>::Error;
62
63    fn packed_len(&self) -> usize {
64        self.0.packed_len()
65    }
66
67    fn pack<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
68        self.0.pack(writer)
69    }
70
71    fn unpack_inner<R: Read + ?Sized, const CHECK: bool>(reader: &mut R) -> Result<Self, Self::Error> {
72        Ok(Self(MessageId::unpack_inner::<R, CHECK>(reader)?))
73    }
74}