Skip to content

Commit

Permalink
Merge pull request #51 from bugadani/lt
Browse files Browse the repository at this point in the history
Improve lifetime names
  • Loading branch information
rmja committed Oct 24, 2023
2 parents 4731572 + 924454c commit a21c15a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
58 changes: 29 additions & 29 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Auth<'a>>,
pub(crate) host: Option<&'a str>,
pub(crate) base_path: Option<&'req str>,
pub(crate) path: &'req str,
pub(crate) auth: Option<Auth<'req>>,
pub(crate) host: Option<&'req str>,
pub(crate) body: Option<B>,
pub(crate) content_type: Option<ContentType>,
pub(crate) extra_headers: Option<&'a [(&'a str, &'a str)]>,
pub(crate) extra_headers: Option<&'req [(&'req str, &'req str)]>,
}

impl Default for Request<'_, ()> {
Expand All @@ -38,37 +38,37 @@ impl Default for Request<'_, ()> {
}

/// A HTTP request builder.
pub trait RequestBuilder<'a, B>
pub trait RequestBuilder<'req, B>
where
B: RequestBody,
{
type WithBody<T: RequestBody>: RequestBuilder<'a, T>;
type WithBody<T: RequestBody>: 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<T: RequestBody>(self, body: T) -> Self::WithBody<T>;
/// 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.
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,
Expand All @@ -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,
{
Expand Down Expand Up @@ -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<T: RequestBody> = DefaultRequestBuilder<'a, T>;
type WithBody<T: RequestBody> = 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
}
Expand All @@ -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
}
Expand All @@ -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
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -27,17 +27,17 @@ where
pub transfer_encoding: heapless::Vec<TransferEncoding, 4>,
/// The keep-alive parameters.
pub keep_alive: Option<KeepAlive>,
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<Self, Error> {
pub async fn read(conn: &'resp mut C, method: Method, header_buf: &'resp mut [u8]) -> Result<Self, Error> {
let mut header_len = 0;
let mut pos = 0;
while pos < header_buf.len() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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<BufferingReader<'buf, C>> {
pub fn reader(self) -> BodyReader<BufferingReader<'resp, C>> {
let raw_body = BufferingReader::new(self.body_buf, self.raw_body_read, self.conn);

match self.reader_hint {
Expand All @@ -220,7 +220,7 @@ where
}
}

impl<'buf, C> ResponseBody<'buf, C>
impl<'resp, C> ResponseBody<'resp, C>
where
C: Read,
{
Expand All @@ -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 {
Expand Down

0 comments on commit a21c15a

Please sign in to comment.