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::{Code, Error, Multihash, MultihashDigest};
31}
32pub mod cid {
33 pub use cid::{multihash::Multihash, Cid, CidGeneric, Error, Result, Version};
34}
35
36pub mod protocol;
37
38/// Substream ID.
39#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
40pub struct SubstreamId(usize);
41
42impl Default for SubstreamId {
43 fn default() -> Self {
44 Self::new()
45 }
46}
47
48impl SubstreamId {
49 /// Create new [`SubstreamId`].
50 pub fn new() -> Self {
51 SubstreamId(0usize)
52 }
53
54 /// Get [`SubstreamId`] from a number that can be converted into a `usize`.
55 pub fn from<T: Into<usize>>(value: T) -> Self {
56 SubstreamId(value.into())
57 }
58}
59
60/// Request ID.
61#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
62#[cfg_attr(feature = "fuzz", derive(serde::Serialize, serde::Deserialize))]
63pub struct RequestId(usize);
64
65impl RequestId {
66 /// Get [`RequestId`] from a number that can be converted into a `usize`.
67 pub fn from<T: Into<usize>>(value: T) -> Self {
68 RequestId(value.into())
69 }
70}
71
72/// Connection ID.
73#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
74pub struct ConnectionId(usize);
75
76impl ConnectionId {
77 /// Create new [`ConnectionId`].
78 pub fn new() -> Self {
79 ConnectionId(0usize)
80 }
81
82 /// Generate random `ConnectionId`.
83 pub fn random() -> Self {
84 ConnectionId(rand::thread_rng().gen::<usize>())
85 }
86}
87
88impl Default for ConnectionId {
89 fn default() -> Self {
90 Self::new()
91 }
92}
93
94impl From<usize> for ConnectionId {
95 fn from(value: usize) -> Self {
96 ConnectionId(value)
97 }
98}