Skip to content

Commit

Permalink
Use HeaderValue::try_from instead for less allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
kazk committed Feb 10, 2021
1 parent e5da843 commit 6ee2b6e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions kube/src/service/auth/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ where
mod tests {
use super::*;

use std::{matches, sync::Arc};
use std::{convert::TryFrom, matches, sync::Arc};

use chrono::{Duration, Utc};
use futures::pin_mut;
Expand All @@ -158,7 +158,7 @@ mod tests {
let (request, send) = handle.next_request().await.expect("service not called");
assert_eq!(
request.headers().get(AUTHORIZATION).unwrap(),
HeaderValue::from_str(&format!("Bearer {}", TOKEN)).unwrap()
HeaderValue::try_from(format!("Bearer {}", TOKEN)).unwrap()
);
send.send_response(Response::builder().body(Body::empty()).unwrap());
});
Expand Down
4 changes: 2 additions & 2 deletions kube/src/service/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl RefreshableToken {
}
}

let mut value = HeaderValue::from_str(&format!("Bearer {}", &locked_data.0))
let mut value = HeaderValue::try_from(format!("Bearer {}", &locked_data.0))
.map_err(ConfigError::InvalidBearerToken)?;
value.set_sensitive(true);
Ok(value)
Expand All @@ -77,7 +77,7 @@ impl RefreshableToken {
RefreshableToken::GcpOauth(data) => {
let gcp_oauth = data.lock().await;
let token = (*gcp_oauth).token().await?;
let mut value = HeaderValue::from_str(&format!("Bearer {}", &token.access_token))
let mut value = HeaderValue::try_from(format!("Bearer {}", &token.access_token))
.map_err(ConfigError::InvalidBearerToken)?;
value.set_sensitive(true);
Ok(value)
Expand Down
4 changes: 2 additions & 2 deletions kube/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ impl TryFrom<Config> for Service {
Authentication::None => None,
Authentication::Basic(s) => {
let mut value =
HeaderValue::from_str(&format!("Basic {}", &s)).map_err(ConfigError::InvalidBasicAuth)?;
HeaderValue::try_from(format!("Basic {}", &s)).map_err(ConfigError::InvalidBasicAuth)?;
value.set_sensitive(true);
default_headers.insert(http::header::AUTHORIZATION, value);
None
}
Authentication::Token(s) => {
let mut value = HeaderValue::from_str(&format!("Bearer {}", &s))
let mut value = HeaderValue::try_from(format!("Bearer {}", &s))
.map_err(ConfigError::InvalidBearerToken)?;
value.set_sensitive(true);
default_headers.insert(http::header::AUTHORIZATION, value);
Expand Down

0 comments on commit 6ee2b6e

Please sign in to comment.