From 924454c63836320d88513435494eab5f1da18cbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Tue, 24 Oct 2023 10:52:47 +0200 Subject: [PATCH] Improve lifetime names --- src/request.rs | 58 ++++++++++++++++++++++++------------------------- src/response.rs | 26 +++++++++++----------- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/request.rs b/src/request.rs index 9f6b45e..416211e 100644 --- a/src/request.rs +++ b/src/request.rs @@ -8,18 +8,18 @@ use embedded_io_async::Write; use heapless::String; /// A read only HTTP request type -pub struct Request<'a, B> +pub struct Request<'req, B> where B: RequestBody, { pub(crate) method: Method, - pub(crate) base_path: Option<&'a str>, - pub(crate) path: &'a str, - pub(crate) auth: Option>, - pub(crate) host: Option<&'a str>, + pub(crate) base_path: Option<&'req str>, + pub(crate) path: &'req str, + pub(crate) auth: Option>, + pub(crate) host: Option<&'req str>, pub(crate) body: Option, pub(crate) content_type: Option, - pub(crate) extra_headers: Option<&'a [(&'a str, &'a str)]>, + pub(crate) extra_headers: Option<&'req [(&'req str, &'req str)]>, } impl Default for Request<'_, ()> { @@ -38,26 +38,26 @@ impl Default for Request<'_, ()> { } /// A HTTP request builder. -pub trait RequestBuilder<'a, B> +pub trait RequestBuilder<'req, B> where B: RequestBody, { - type WithBody: RequestBuilder<'a, T>; + type WithBody: RequestBuilder<'req, T>; /// Set optional headers on the request. - fn headers(self, headers: &'a [(&'a str, &'a str)]) -> Self; + fn headers(self, headers: &'req [(&'req str, &'req str)]) -> Self; /// Set the path of the HTTP request. - fn path(self, path: &'a str) -> Self; + fn path(self, path: &'req str) -> Self; /// Set the data to send in the HTTP request body. fn body(self, body: T) -> Self::WithBody; /// Set the host header. - fn host(self, host: &'a str) -> Self; + fn host(self, host: &'req str) -> Self; /// Set the content type header for the request. fn content_type(self, content_type: ContentType) -> Self; /// Set the basic authentication header for the request. - fn basic_auth(self, username: &'a str, password: &'a str) -> Self; + fn basic_auth(self, username: &'req str, password: &'req str) -> Self; /// Return an immutable request. - fn build(self) -> Request<'a, B>; + fn build(self) -> Request<'req, B>; } /// Request authentication scheme. @@ -65,10 +65,10 @@ pub enum Auth<'a> { Basic { username: &'a str, password: &'a str }, } -impl<'a> Request<'a, ()> { +impl<'req> Request<'req, ()> { /// Create a new http request. #[allow(clippy::new_ret_no_self)] - pub fn new(method: Method, path: &'a str) -> DefaultRequestBuilder<'a, ()> { + pub fn new(method: Method, path: &'req str) -> DefaultRequestBuilder<'req, ()> { DefaultRequestBuilder(Request { method, path, @@ -77,32 +77,32 @@ impl<'a> Request<'a, ()> { } /// Create a new GET http request. - pub fn get(path: &'a str) -> DefaultRequestBuilder<'a, ()> { + pub fn get(path: &'req str) -> DefaultRequestBuilder<'req, ()> { Self::new(Method::GET, path) } /// Create a new POST http request. - pub fn post(path: &'a str) -> DefaultRequestBuilder<'a, ()> { + pub fn post(path: &'req str) -> DefaultRequestBuilder<'req, ()> { Self::new(Method::POST, path) } /// Create a new PUT http request. - pub fn put(path: &'a str) -> DefaultRequestBuilder<'a, ()> { + pub fn put(path: &'req str) -> DefaultRequestBuilder<'req, ()> { Self::new(Method::PUT, path) } /// Create a new DELETE http request. - pub fn delete(path: &'a str) -> DefaultRequestBuilder<'a, ()> { + pub fn delete(path: &'req str) -> DefaultRequestBuilder<'req, ()> { Self::new(Method::DELETE, path) } /// Create a new HEAD http request. - pub fn head(path: &'a str) -> DefaultRequestBuilder<'a, ()> { + pub fn head(path: &'req str) -> DefaultRequestBuilder<'req, ()> { Self::new(Method::HEAD, path) } } -impl<'a, B> Request<'a, B> +impl<'req, B> Request<'req, B> where B: RequestBody, { @@ -190,22 +190,22 @@ where } } -pub struct DefaultRequestBuilder<'a, B>(Request<'a, B>) +pub struct DefaultRequestBuilder<'req, B>(Request<'req, B>) where B: RequestBody; -impl<'a, B> RequestBuilder<'a, B> for DefaultRequestBuilder<'a, B> +impl<'req, B> RequestBuilder<'req, B> for DefaultRequestBuilder<'req, B> where B: RequestBody, { - type WithBody = DefaultRequestBuilder<'a, T>; + type WithBody = DefaultRequestBuilder<'req, T>; - fn headers(mut self, headers: &'a [(&'a str, &'a str)]) -> Self { + fn headers(mut self, headers: &'req [(&'req str, &'req str)]) -> Self { self.0.extra_headers.replace(headers); self } - fn path(mut self, path: &'a str) -> Self { + fn path(mut self, path: &'req str) -> Self { self.0.path = path; self } @@ -223,7 +223,7 @@ where }) } - fn host(mut self, host: &'a str) -> Self { + fn host(mut self, host: &'req str) -> Self { self.0.host.replace(host); self } @@ -233,12 +233,12 @@ where self } - fn basic_auth(mut self, username: &'a str, password: &'a str) -> Self { + fn basic_auth(mut self, username: &'req str, password: &'req str) -> Self { self.0.auth.replace(Auth::Basic { username, password }); self } - fn build(self) -> Request<'a, B> { + fn build(self) -> Request<'req, B> { self.0 } } diff --git a/src/response.rs b/src/response.rs index 7986cdb..a7e36f6 100644 --- a/src/response.rs +++ b/src/response.rs @@ -10,11 +10,11 @@ use crate::Error; /// Type representing a parsed HTTP response. #[derive(Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] -pub struct Response<'buf, C> +pub struct Response<'resp, C> where C: Read, { - conn: &'buf mut C, + conn: &'resp mut C, /// The method used to create the response. method: Method, /// The HTTP response status code. @@ -27,17 +27,17 @@ where pub transfer_encoding: heapless::Vec, /// The keep-alive parameters. pub keep_alive: Option, - header_buf: &'buf mut [u8], + header_buf: &'resp mut [u8], header_len: usize, raw_body_read: usize, } -impl<'buf, C> Response<'buf, C> +impl<'resp, C> Response<'resp, C> where C: Read, { // Read at least the headers from the connection. - pub async fn read(conn: &'buf mut C, method: Method, header_buf: &'buf mut [u8]) -> Result { + pub async fn read(conn: &'resp mut C, method: Method, header_buf: &'resp mut [u8]) -> Result { let mut header_len = 0; let mut pos = 0; while pos < header_buf.len() { @@ -135,7 +135,7 @@ where } /// Get the response body - pub fn body(self) -> ResponseBody<'buf, C> { + pub fn body(self) -> ResponseBody<'resp, C> { let reader_hint = if self.method == Method::HEAD { // Head requests does not have a body so we return an empty reader ReaderHint::Empty @@ -179,16 +179,16 @@ impl<'a> Iterator for HeaderIterator<'a> { /// This type contains the original header buffer provided to `read_headers`, /// now renamed to `body_buf`, the number of read body bytes that are available /// in `body_buf`, and a reader to be used for reading the remaining body. -pub struct ResponseBody<'buf, C> +pub struct ResponseBody<'resp, C> where C: Read, { - conn: &'buf mut C, + conn: &'resp mut C, reader_hint: ReaderHint, /// The number of raw bytes read from the body and available in the beginning of `body_buf`. raw_body_read: usize, /// The buffer initially provided to read the header. - pub body_buf: &'buf mut [u8], + pub body_buf: &'resp mut [u8], } enum ReaderHint { @@ -198,11 +198,11 @@ enum ReaderHint { ToEnd, // https://www.rfc-editor.org/rfc/rfc7230#section-3.3.3 pt. 7: Until end of connection } -impl<'buf, C> ResponseBody<'buf, C> +impl<'resp, C> ResponseBody<'resp, C> where C: Read, { - pub fn reader(self) -> BodyReader> { + pub fn reader(self) -> BodyReader> { let raw_body = BufferingReader::new(self.body_buf, self.raw_body_read, self.conn); match self.reader_hint { @@ -220,7 +220,7 @@ where } } -impl<'buf, C> ResponseBody<'buf, C> +impl<'resp, C> ResponseBody<'resp, C> where C: Read, { @@ -231,7 +231,7 @@ where /// while parsing the http response header would be available for the body reader. /// For this case, or if the original buffer is not large enough, use /// [`BodyReader::read_to_end()`] instead from the reader returned by [`ResponseBody::reader()`]. - pub async fn read_to_end(self) -> Result<&'buf mut [u8], Error> { + pub async fn read_to_end(self) -> Result<&'resp mut [u8], Error> { // We can only read responses with Content-Length header to end using the body_buf buffer, // as any other response would require the body reader to know the entire body. match self.reader_hint {