Skip to content

Commit b974460

Browse files
Sh4rKdavidbarsky
authored andcommitted
Avoid Unnecessary allocations in lambda-runtime-client. (#28)
1 parent 85a1c06 commit b974460

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

lambda-runtime-client/src/client.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,26 @@ pub enum LambdaHeaders {
3333
CognitoIdentity,
3434
}
3535

36-
impl fmt::Display for LambdaHeaders {
37-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36+
impl LambdaHeaders {
37+
/// Returns the `str` representation of the header.
38+
fn as_str(&self) -> &'static str {
3839
match self {
39-
LambdaHeaders::RequestId => write!(f, "Lambda-Runtime-Aws-Request-Id"),
40-
LambdaHeaders::FunctionArn => write!(f, "Lambda-Runtime-Invoked-Function-Arn"),
41-
LambdaHeaders::TraceId => write!(f, "Lambda-Runtime-Trace-Id"),
42-
LambdaHeaders::Deadline => write!(f, "Lambda-Runtime-Deadline-Ms"),
43-
LambdaHeaders::ClientContext => write!(f, "Lambda-Runtime-Client-Context"),
44-
LambdaHeaders::CognitoIdentity => write!(f, "Lambda-Runtime-Cognito-Identity"),
40+
LambdaHeaders::RequestId => "Lambda-Runtime-Aws-Request-Id",
41+
LambdaHeaders::FunctionArn => "Lambda-Runtime-Invoked-Function-Arn",
42+
LambdaHeaders::TraceId => "Lambda-Runtime-Trace-Id",
43+
LambdaHeaders::Deadline => "Lambda-Runtime-Deadline-Ms",
44+
LambdaHeaders::ClientContext => "Lambda-Runtime-Client-Context",
45+
LambdaHeaders::CognitoIdentity => "Lambda-Runtime-Cognito-Identity",
4546
}
4647
}
4748
}
4849

50+
impl fmt::Display for LambdaHeaders {
51+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52+
f.write_str(self.as_str())
53+
}
54+
}
55+
4956
/// AWS Moble SDK client properties
5057
#[derive(Deserialize, Clone)]
5158
pub struct ClientApplication {
@@ -371,31 +378,31 @@ impl RuntimeClient {
371378
fn get_event_context(&self, headers: &HeaderMap<HeaderValue>) -> Result<EventContext, ApiError> {
372379
// let headers = resp.headers();
373380

374-
let aws_request_id = match headers.get(LambdaHeaders::RequestId.to_string()) {
381+
let aws_request_id = match headers.get(LambdaHeaders::RequestId.as_str()) {
375382
Some(value) => value.to_str()?.to_owned(),
376383
None => {
377384
error!("Response headers do not contain request id header");
378385
return Err(ApiError::new(&format!("Missing {} header", LambdaHeaders::RequestId)));
379386
}
380387
};
381388

382-
let invoked_function_arn = match headers.get(LambdaHeaders::FunctionArn.to_string()) {
389+
let invoked_function_arn = match headers.get(LambdaHeaders::FunctionArn.as_str()) {
383390
Some(value) => value.to_str()?.to_owned(),
384391
None => {
385392
error!("Response headers do not contain function arn header");
386393
return Err(ApiError::new(&format!("Missing {} header", LambdaHeaders::FunctionArn)));
387394
}
388395
};
389396

390-
let xray_trace_id = match headers.get(LambdaHeaders::TraceId.to_string()) {
397+
let xray_trace_id = match headers.get(LambdaHeaders::TraceId.as_str()) {
391398
Some(value) => value.to_str()?.to_owned(),
392399
None => {
393400
error!("Response headers do not contain trace id header");
394401
return Err(ApiError::new(&format!("Missing {} header", LambdaHeaders::TraceId)));
395402
}
396403
};
397404

398-
let deadline: u128 = match headers.get(LambdaHeaders::Deadline.to_string()) {
405+
let deadline: u128 = match headers.get(LambdaHeaders::Deadline.as_str()) {
399406
Some(value) => value.to_str()?.to_owned(),
400407
None => {
401408
error!("Response headers do not contain deadline header");
@@ -413,14 +420,14 @@ impl RuntimeClient {
413420
identity: Option::default(),
414421
};
415422

416-
if let Some(ctx_json) = headers.get(LambdaHeaders::ClientContext.to_string()) {
423+
if let Some(ctx_json) = headers.get(LambdaHeaders::ClientContext.as_str()) {
417424
let ctx_json = ctx_json.to_str()?;
418425
trace!("Found Client Context in response headers: {}", ctx_json);
419426
let ctx_value: ClientContext = serde_json::from_str(&ctx_json)?;
420427
ctx.client_context = Option::from(ctx_value);
421428
};
422429

423-
if let Some(cognito_json) = headers.get(LambdaHeaders::CognitoIdentity.to_string()) {
430+
if let Some(cognito_json) = headers.get(LambdaHeaders::CognitoIdentity.as_str()) {
424431
let cognito_json = cognito_json.to_str()?;
425432
trace!("Found Cognito Identity in response headers: {}", cognito_json);
426433
let identity_value: CognitoIdentity = serde_json::from_str(&cognito_json)?;

0 commit comments

Comments
 (0)