diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/neqo-common/src/lib.rs | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/neqo-common/src/lib.rs')
-rw-r--r-- | third_party/rust/neqo-common/src/lib.rs | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/third_party/rust/neqo-common/src/lib.rs b/third_party/rust/neqo-common/src/lib.rs new file mode 100644 index 0000000000..853b05705b --- /dev/null +++ b/third_party/rust/neqo-common/src/lib.rs @@ -0,0 +1,109 @@ +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![cfg_attr(feature = "deny-warnings", deny(warnings))] +#![warn(clippy::pedantic)] + +mod codec; +mod datagram; +pub mod event; +pub mod header; +pub mod hrtime; +mod incrdecoder; +pub mod log; +pub mod qlog; +pub mod timer; +pub mod tos; + +use std::fmt::Write; + +use enum_map::Enum; + +pub use self::{ + codec::{Decoder, Encoder}, + datagram::Datagram, + header::Header, + incrdecoder::{IncrementalDecoderBuffer, IncrementalDecoderIgnore, IncrementalDecoderUint}, + tos::{IpTos, IpTosDscp, IpTosEcn}, +}; + +#[must_use] +pub fn hex(buf: impl AsRef<[u8]>) -> String { + let mut ret = String::with_capacity(buf.as_ref().len() * 2); + for b in buf.as_ref() { + write!(&mut ret, "{b:02x}").unwrap(); + } + ret +} + +#[must_use] +pub fn hex_snip_middle(buf: impl AsRef<[u8]>) -> String { + const SHOW_LEN: usize = 8; + let buf = buf.as_ref(); + if buf.len() <= SHOW_LEN * 2 { + hex_with_len(buf) + } else { + let mut ret = String::with_capacity(SHOW_LEN * 2 + 16); + write!(&mut ret, "[{}]: ", buf.len()).unwrap(); + for b in &buf[..SHOW_LEN] { + write!(&mut ret, "{b:02x}").unwrap(); + } + ret.push_str(".."); + for b in &buf[buf.len() - SHOW_LEN..] { + write!(&mut ret, "{b:02x}").unwrap(); + } + ret + } +} + +#[must_use] +pub fn hex_with_len(buf: impl AsRef<[u8]>) -> String { + let buf = buf.as_ref(); + let mut ret = String::with_capacity(10 + buf.len() * 2); + write!(&mut ret, "[{}]: ", buf.len()).unwrap(); + for b in buf { + write!(&mut ret, "{b:02x}").unwrap(); + } + ret +} + +#[must_use] +pub const fn const_max(a: usize, b: usize) -> usize { + [a, b][(a < b) as usize] +} +#[must_use] +pub const fn const_min(a: usize, b: usize) -> usize { + [a, b][(a >= b) as usize] +} + +#[derive(Debug, PartialEq, Eq, Copy, Clone, Enum)] +/// Client or Server. +pub enum Role { + Client, + Server, +} + +impl Role { + #[must_use] + pub fn remote(self) -> Self { + match self { + Self::Client => Self::Server, + Self::Server => Self::Client, + } + } +} + +impl ::std::fmt::Display for Role { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + write!(f, "{self:?}") + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum MessageType { + Request, + Response, +} |