litep2p/types.rs
1// Copyright 2023 litep2p developers
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! Types used by [`Litep2p`](`crate::Litep2p`) protocols/transport.
22
23use rand::Rng;
24
25// Re-export the types used in public interfaces.
26pub mod multiaddr {
27 pub use multiaddr::{Error, Iter, Multiaddr, Onion3Addr, Protocol};
28}
29pub mod multihash {
30 pub use multihash::{Error, Multihash};
31 pub use multihash_codetable::{Code, MultihashDigest};
32}
33pub mod cid {
34 pub use cid::{multihash::Multihash, Cid, CidGeneric, Error, Result, Version};
35}
36
37pub mod protocol;
38
39/// Substream ID.
40#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
41pub struct SubstreamId(usize);
42
43impl Default for SubstreamId {
44 fn default() -> Self {
45 Self::new()
46 }
47}
48
49impl SubstreamId {
50 /// Create new [`SubstreamId`].
51 pub fn new() -> Self {
52 SubstreamId(0usize)
53 }
54
55 /// Get [`SubstreamId`] from a number that can be converted into a `usize`.
56 pub fn from<T: Into<usize>>(value: T) -> Self {
57 SubstreamId(value.into())
58 }
59}
60
61/// Request ID.
62#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
63#[cfg_attr(feature = "fuzz", derive(serde::Serialize, serde::Deserialize))]
64pub struct RequestId(usize);
65
66impl RequestId {
67 /// Get [`RequestId`] from a number that can be converted into a `usize`.
68 pub fn from<T: Into<usize>>(value: T) -> Self {
69 RequestId(value.into())
70 }
71}
72
73/// Connection ID.
74#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
75pub struct ConnectionId(usize);
76
77impl ConnectionId {
78 /// Create new [`ConnectionId`].
79 pub fn new() -> Self {
80 ConnectionId(0usize)
81 }
82
83 /// Generate random `ConnectionId`.
84 pub fn random() -> Self {
85 ConnectionId(rand::thread_rng().gen::<usize>())
86 }
87}
88
89impl Default for ConnectionId {
90 fn default() -> Self {
91 Self::new()
92 }
93}
94
95impl From<usize> for ConnectionId {
96 fn from(value: usize) -> Self {
97 ConnectionId(value)
98 }
99}