Skip to content

Commit

Permalink
support maybe_insert for Extensions/Context
Browse files Browse the repository at this point in the history
  • Loading branch information
GlenDC committed Aug 22, 2024
1 parent 5bc6e35 commit 23372fc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
6 changes: 2 additions & 4 deletions examples/http_connect_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,8 @@ impl UsernameLabelParser for PriorityUsernameLabelParser {
UsernameLabelState::Used
}

fn build(mut self, ext: &mut Extensions) -> Result<(), Self::Error> {
if let Some(priority) = self.priority.take() {
ext.insert(priority);
}
fn build(self, ext: &mut Extensions) -> Result<(), Self::Error> {
ext.maybe_insert(self.priority);
Ok(())
}
}
20 changes: 5 additions & 15 deletions src/proxy/proxydb/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,9 +1046,7 @@ mod tests {
),
] {
let mut ctx = Context::default();
if let Some(filter) = filter {
ctx.insert(filter);
}
ctx.maybe_insert(filter);

let maybe_proxy_address = service.serve(ctx, req).await.unwrap();

Expand Down Expand Up @@ -1085,9 +1083,7 @@ mod tests {
let mut seen_addresses = Vec::new();
for _ in 0..5000 {
let mut ctx = Context::default();
if let Some(filter) = filter.clone() {
ctx.insert(filter);
}
ctx.maybe_insert(filter.clone());

let req = Request::builder()
.version(req_info.0)
Expand Down Expand Up @@ -1142,9 +1138,7 @@ mod tests {
let mut seen_addresses = Vec::new();
for _ in 0..5000 {
let mut ctx = Context::default();
if let Some(filter) = filter.clone() {
ctx.insert(filter);
}
ctx.maybe_insert(filter.clone());

let req = Request::builder()
.version(req_info.0)
Expand Down Expand Up @@ -1231,9 +1225,7 @@ mod tests {
),
] {
let mut ctx = Context::default();
if let Some(filter) = filter {
ctx.insert(filter);
}
ctx.maybe_insert(filter.clone());

let proxy_address_result = service.serve(ctx, req).await;
match expected_address {
Expand Down Expand Up @@ -1332,9 +1324,7 @@ mod tests {
),
] {
let mut ctx = Context::default();
if let Some(filter) = filter {
ctx.insert(filter);
}
ctx.maybe_insert(filter);

let proxy_result = service.serve(ctx, req).await;
match expected {
Expand Down
10 changes: 10 additions & 0 deletions src/service/context/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ impl Extensions {
.and_then(|boxed| boxed.into_any().downcast().ok().map(|boxed| *boxed))
}

/// Insert a type only into this `Extensions`, if the value is `Some(T)`.
///
/// See [`Self::insert`] for more information.
pub fn maybe_insert<T: Clone + Send + Sync + 'static>(
&mut self,
mut val: Option<T>,
) -> Option<T> {
val.take().and_then(|val| self.insert(val))
}

/// Extend these extensions with another Extensions.
pub fn extend(&mut self, other: Extensions) {
if let Some(other_map) = other.map {
Expand Down
10 changes: 10 additions & 0 deletions src/service/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,16 @@ impl<S> Context<S> {
self.extensions.insert(extension)
}

/// Insert a type only into this [`Context`], if the extension is `Some(T)`.
///
/// See [`Self::insert`] for more information.
pub fn maybe_insert<T: Clone + Send + Sync + 'static>(
&mut self,
extension: Option<T>,
) -> Option<T> {
self.extensions.maybe_insert(extension)
}

/// Return the entire dynamic state of the [`Context`] by reference.
///
/// Useful only in case you have a function which works with [`Extensions`] rather
Expand Down

0 comments on commit 23372fc

Please sign in to comment.