From 2e68155bf43253b1f00a366090817d6e1c5e278d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 16 Oct 2021 00:19:58 +0200 Subject: [PATCH] aquatic_http: use Cow<_> for FailureResponse.failure_reason --- aquatic_http/src/lib/network/mod.rs | 4 +--- aquatic_http_protocol/src/response.rs | 7 ++++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/aquatic_http/src/lib/network/mod.rs b/aquatic_http/src/lib/network/mod.rs index 56fba90..8fa0ffd 100644 --- a/aquatic_http/src/lib/network/mod.rs +++ b/aquatic_http/src/lib/network/mod.rs @@ -233,9 +233,7 @@ pub fn handle_connection_read_event( peer_addr: established.peer_addr, }; - let response = FailureResponse { - failure_reason: "invalid request".to_string(), - }; + let response = FailureResponse::new("Invalid request"); local_responses.push((meta, Response::Failure(response))); diff --git a/aquatic_http_protocol/src/response.rs b/aquatic_http_protocol/src/response.rs index a45c93f..e14e661 100644 --- a/aquatic_http_protocol/src/response.rs +++ b/aquatic_http_protocol/src/response.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::io::Write; use std::net::{Ipv4Addr, Ipv6Addr}; @@ -131,11 +132,11 @@ impl ScrapeResponse { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct FailureResponse { #[serde(rename = "failure reason")] - pub failure_reason: String, + pub failure_reason: Cow<'static, str>, } impl FailureResponse { - pub fn new(reason: &str) -> Self { + pub fn new>>(reason: S) -> Self { Self { failure_reason: reason.into(), } @@ -248,7 +249,7 @@ impl quickcheck::Arbitrary for ScrapeResponse { impl quickcheck::Arbitrary for FailureResponse { fn arbitrary(g: &mut quickcheck::Gen) -> Self { Self { - failure_reason: String::arbitrary(g), + failure_reason: String::arbitrary(g).into(), } } }