Debian Codegraph @

third_party/rust/neqo-common/src/lib.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 mod codec;
8 mod datagram;
9 pub mod event;
10 #[cfg(feature = "build-fuzzing-corpus")]
11 mod fuzz;
12 pub mod header;
13 pub mod hrtime;
14 mod incrdecoder;
15 pub mod log;
16 pub mod qlog;
17 pub mod tos;
18
19 use std::fmt::Write as _;
20
21 use enum_map::Enum;
22 use strum::Display;
23
24 #[cfg(feature = "build-fuzzing-corpus")]
25 pub use self::fuzz::write_item_to_fuzzing_corpus;
26 pub use self::{
27 codec::{Decoder, Encoder, MAX_VARINT},
28 datagram::Datagram,
29 header::Header,
30 incrdecoder::{IncrementalDecoderBuffer, IncrementalDecoderIgnore, IncrementalDecoderUint},
31 tos::{IpTos, IpTosDscp, IpTosEcn},
32 };
33
34 #[must_use]
35 pub fn hex(buf: impl AsRef<[u8]>) -> String {
36 let mut ret = String::with_capacity(buf.as_ref().len() * 2);
37 for b in buf.as_ref() {
38 write!(&mut ret, "{b:02x}").expect("write OK");
39 }
40 ret
41 }
42
43 #[must_use]
44 pub fn hex_snip_middle(buf: impl AsRef<[u8]>) -> String {
45 const SHOW_LEN: usize = 8;
46 let buf = buf.as_ref();
47 if buf.len() <= SHOW_LEN * 2 {
48 hex_with_len(buf)
49 } else {
50 let mut ret = String::with_capacity(SHOW_LEN * 2 + 16);
51 write!(&mut ret, "[{}]: ", buf.len()).expect("write OK");
52 for b in &buf[..SHOW_LEN] {
53 write!(&mut ret, "{b:02x}").expect("write OK");
54 }
55 ret.push_str("..");
56 for b in &buf[buf.len() - SHOW_LEN..] {
57 write!(&mut ret, "{b:02x}").expect("write OK");
58 }
59 ret
60 }
61 }
62
63 #[must_use]
64 pub fn hex_with_len(buf: impl AsRef<[u8]>) -> String {
65 let buf = buf.as_ref();
66 let mut ret = String::with_capacity(10 + buf.len() * 2);
67 write!(&mut ret, "[{}]: ", buf.len()).expect("write OK");
68 for b in buf {
69 write!(&mut ret, "{b:02x}").expect("write OK");
70 }
71 ret
72 }
73
74 #[must_use]
75 pub const fn const_max(a: usize, b: usize) -> usize {
76 [a, b][(a < b) as usize]
77 }
78 #[must_use]
79 pub const fn const_min(a: usize, b: usize) -> usize {
80 [a, b][(a >= b) as usize]
81 }
82
83 #[derive(Debug, PartialEq, Eq, Copy, Clone, Enum, Display)]
84 /// Client or Server.
85 pub enum Role {
86 Client,
87 Server,
88 }
89
90 impl Role {
91 #[must_use]
92 pub const fn remote(self) -> Self {
93 match self {
94 Self::Client => Self::Server,
95 Self::Server => Self::Client,
96 }
97 }
98 }
99
100 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
101 pub enum MessageType {
102 Request,
103 Response,
104 }