Skip to content

Commit

Permalink
refactor: Simplify IntoStreamDependency implementations using macros
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 committed Jan 25, 2025
1 parent 9d9bb4f commit 72c796c
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/http2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub struct Http2Settings {
///
/// - **Structure:** `(stream_dependency, weight, exclusive_flag)`
/// - **Purpose:** Specifies how header frames are prioritized during transmission.
#[builder(default, setter(transform = |input: impl IntoStreamDependency| Some(input.into())))]
#[builder(default, setter(transform = |input: impl IntoStreamDependency| input.into()))]
pub headers_priority: Option<StreamDependency>,

/// The order of pseudo-header fields.
Expand All @@ -102,31 +102,31 @@ pub struct Http2Settings {
pub priority: Option<Cow<'static, [Priority]>>,
}

/// A trait for converting various types into a `StreamDependency`.
/// A trait for converting various types into an optional `StreamDependency`.
///
/// This trait is used to provide a unified way to convert different types
/// into a `StreamDependency` instance.
/// into an optional `StreamDependency` instance.
pub trait IntoStreamDependency {
/// Converts the implementing type into a `StreamDependency`.
fn into(self) -> StreamDependency;
/// Converts the implementing type into an optional `StreamDependency`.
fn into(self) -> Option<StreamDependency>;
}

/// Implements `IntoStreamDependency` for a tuple of `(u32, u8, bool)`.
///
/// This implementation allows a tuple containing a stream ID, weight, and
/// exclusive flag to be converted into a `StreamDependency`.
impl IntoStreamDependency for (u32, u8, bool) {
fn into(self) -> StreamDependency {
StreamDependency::new(StreamId::from(self.0), self.1, self.2)
}
// Macro to implement IntoStreamDependency for various types
macro_rules! impl_into_stream_dependency {
($($t:ty => $body:expr),*) => {
$(
impl IntoStreamDependency for $t {
fn into(self) -> Option<StreamDependency> {
$body(self)
}
}
)*
};
}

/// Implements `IntoStreamDependency` for `StreamDependency`.
///
/// This implementation allows a `StreamDependency` to be converted into
/// itself, which is useful for cases where a generic conversion is needed.
impl IntoStreamDependency for StreamDependency {
fn into(self) -> StreamDependency {
self
}
}
impl_into_stream_dependency!(
(u32, u8, bool) => |(id, weight, exclusive)| Some(StreamDependency::new(StreamId::from(id), weight, exclusive)),
Option<(u32, u8, bool)> => |opt: Option<(u32, u8, bool)>| opt.map(|(id, weight, exclusive)| StreamDependency::new(StreamId::from(id), weight, exclusive)),
StreamDependency => |dep| Some(dep),
Option<StreamDependency> => |opt| opt
);

0 comments on commit 72c796c

Please sign in to comment.