From ff77ecbe9650b3cd46a4ae3a08d71bc98141ea61 Mon Sep 17 00:00:00 2001 From: Xinzhao Xu Date: Fri, 8 Nov 2024 10:17:23 +0800 Subject: [PATCH] Add the #[inline] attr for some methods --- waki/src/body.rs | 2 ++ waki/src/client.rs | 8 ++++++++ waki/src/common/request_and_response.rs | 5 +++++ waki/src/common/scheme.rs | 2 ++ waki/src/request.rs | 9 +++++++++ waki/src/response.rs | 8 ++++++++ 6 files changed, 34 insertions(+) diff --git a/waki/src/body.rs b/waki/src/body.rs index d538ceb..bc68184 100644 --- a/waki/src/body.rs +++ b/waki/src/body.rs @@ -12,6 +12,7 @@ pub struct IncomingBodyStream { } impl From for IncomingBodyStream { + #[inline] fn from(body: IncomingBody) -> Self { Self { // The stream() method can only be called once @@ -37,6 +38,7 @@ pub enum Body { } impl Body { + #[inline] pub fn chunk(&self, len: u64) -> Result>> { match &self { Body::Bytes(_) => Ok(None), diff --git a/waki/src/client.rs b/waki/src/client.rs index ecdc445..b11dd21 100644 --- a/waki/src/client.rs +++ b/waki/src/client.rs @@ -4,34 +4,42 @@ use crate::{Method, RequestBuilder}; pub struct Client {} impl Client { + #[inline] pub fn new() -> Self { Default::default() } + #[inline] pub fn get(&self, url: &str) -> RequestBuilder { self.request(Method::Get, url) } + #[inline] pub fn post(&self, url: &str) -> RequestBuilder { self.request(Method::Post, url) } + #[inline] pub fn put(&self, url: &str) -> RequestBuilder { self.request(Method::Put, url) } + #[inline] pub fn patch(&self, url: &str) -> RequestBuilder { self.request(Method::Patch, url) } + #[inline] pub fn delete(&self, url: &str) -> RequestBuilder { self.request(Method::Delete, url) } + #[inline] pub fn head(&self, url: &str) -> RequestBuilder { self.request(Method::Head, url) } + #[inline] pub fn request(&self, method: Method, url: &str) -> RequestBuilder { RequestBuilder::new(method, url) } diff --git a/waki/src/common/request_and_response.rs b/waki/src/common/request_and_response.rs index 72c6c51..555f9ac 100644 --- a/waki/src/common/request_and_response.rs +++ b/waki/src/common/request_and_response.rs @@ -14,11 +14,13 @@ macro_rules! impl_common_get_methods { ($($t:ty),+ $(,)?) => ($( impl $t { /// Get the header. + #[inline] pub fn header(&self, key: K) -> Option<&HeaderValue> { self.headers.get(key) } /// Get headers. + #[inline] pub fn headers(&self) -> &HeaderMap { &self.headers } @@ -29,6 +31,7 @@ macro_rules! impl_common_get_methods { /// /// NOTE: This method is only for incoming requests/responses, if you call it on an /// outgoing request/response it will always return None. + #[inline] pub fn chunk(&self, len: u64) -> Result>> { self.body.chunk(len) } @@ -36,6 +39,7 @@ macro_rules! impl_common_get_methods { /// Get the full body. /// /// It will block until the stream is closed. + #[inline] pub fn body(self) -> Result> { self.body.bytes() } @@ -181,6 +185,7 @@ macro_rules! impl_common_set_methods { /// r.body("hello"); /// # } /// ``` + #[inline] pub fn body>>(mut self, body: V) -> Self { if let Ok(ref mut inner) = self.inner { inner.body = Body::Bytes(body.into()); diff --git a/waki/src/common/scheme.rs b/waki/src/common/scheme.rs index 1cda07e..d1ac9e7 100644 --- a/waki/src/common/scheme.rs +++ b/waki/src/common/scheme.rs @@ -1,6 +1,7 @@ use crate::bindings::wasi::http::types::Scheme; impl From<&str> for Scheme { + #[inline] fn from(s: &str) -> Self { match s { "http" => Scheme::Http, @@ -13,6 +14,7 @@ impl From<&str> for Scheme { impl TryInto for Scheme { type Error = http::uri::InvalidUri; + #[inline] fn try_into(self) -> Result { match self { Scheme::Http => Ok(http::uri::Scheme::HTTP), diff --git a/waki/src/request.rs b/waki/src/request.rs index 63c8e5c..255244e 100644 --- a/waki/src/request.rs +++ b/waki/src/request.rs @@ -23,6 +23,7 @@ pub struct RequestBuilder { } impl RequestBuilder { + #[inline] pub fn new(method: Method, uri: &str) -> Self { Self { inner: uri.parse::().map_or_else( @@ -86,6 +87,7 @@ impl RequestBuilder { /// # Ok(()) /// # } /// ``` + #[inline] pub fn connect_timeout(mut self, timeout: Duration) -> Self { if let Ok(ref mut req) = self.inner { req.connect_timeout = Some(timeout.as_nanos() as u64); @@ -94,11 +96,13 @@ impl RequestBuilder { } /// Build the Request. + #[inline] pub fn build(self) -> Result { self.inner } /// Send the Request, returning a [`Response`]. + #[inline] pub fn send(self) -> Result { match self.inner { Ok(req) => req.send(), @@ -162,6 +166,7 @@ impl TryFrom for Request { } impl Request { + #[inline] pub fn new(method: Method, uri: Parts) -> Self { Self { method, @@ -172,16 +177,19 @@ impl Request { } } + #[inline] pub fn builder(method: Method, uri: &str) -> RequestBuilder { RequestBuilder::new(method, uri) } /// Get the HTTP method of the request. + #[inline] pub fn method(&self) -> Method { self.method.clone() } /// Get the path of the request. + #[inline] pub fn path(&self) -> &str { match &self.uri.path_and_query { Some(path_and_query) => path_and_query.path(), @@ -202,6 +210,7 @@ impl Request { } /// Get the authority of the request. + #[inline] pub fn authority(&self) -> &Option { &self.uri.authority } diff --git a/waki/src/response.rs b/waki/src/response.rs index 4ce0df6..0be9546 100644 --- a/waki/src/response.rs +++ b/waki/src/response.rs @@ -15,12 +15,14 @@ pub struct ResponseBuilder { } impl Default for ResponseBuilder { + #[inline] fn default() -> Self { Self::new() } } impl ResponseBuilder { + #[inline] pub fn new() -> Self { Self { inner: Ok(Response::new()), @@ -30,6 +32,7 @@ impl ResponseBuilder { /// Set the status code for the response. /// /// Default value: 200. + #[inline] pub fn status_code(mut self, status_code: u16) -> Self { if let Ok(ref mut resp) = self.inner { resp.status_code = status_code; @@ -38,6 +41,7 @@ impl ResponseBuilder { } /// Build the Response. + #[inline] pub fn build(self) -> Result { match self.inner { Ok(inner) => Ok(inner), @@ -53,6 +57,7 @@ pub struct Response { } impl Default for Response { + #[inline] fn default() -> Self { Self::new() } @@ -77,6 +82,7 @@ impl TryFrom for Response { } impl Response { + #[inline] pub fn new() -> Self { Self { headers: HeaderMap::new(), @@ -85,10 +91,12 @@ impl Response { } } + #[inline] pub fn builder() -> ResponseBuilder { ResponseBuilder::new() } + #[inline] /// Get the status code of the response. pub fn status_code(&self) -> u16 { self.status_code