Skip to content

Commit 18ee8b1

Browse files
authored
fix(volo-http): fix clippy::result_large_err (#568)
The type `Uri` takes up 88 bytes, which causes the entire `ClientError` to take up 144 bytes and clippy will throw `clippy::result_large_err`. Considering that the field will not be used in the hot path, in order to avoid the problem of `ClientError` being too large, we added `Box` to `Uri`. With this change, the size of `ClientError` is reduced from 144 to 64. Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
1 parent a0b37c0 commit 18ee8b1

5 files changed

Lines changed: 9 additions & 15 deletions

File tree

volo-http/src/client/layer/header.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ impl Header {
5050
///
5151
/// [`ClientError`]: crate::error::client::ClientError
5252
/// [`ErrorKind::Builder`]: crate::error::client::ErrorKind::Builder
53-
#[allow(clippy::result_large_err)]
5453
pub fn try_new<K, V>(key: K, val: V) -> Result<Self>
5554
where
5655
K: TryInto<HeaderName>,

volo-http/src/client/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,6 @@ impl<IL, OL, C, LB> ClientBuilder<IL, OL, C, LB> {
666666
/// - Transport through network or unix domain socket.
667667
///
668668
/// [`DnsResolver`]: crate::client::dns::DnsResolver
669-
#[allow(clippy::result_large_err)]
670669
pub fn build(mut self) -> Result<C::Target>
671670
where
672671
IL: Layer<ClientMetaService>,
@@ -701,7 +700,6 @@ impl<IL, OL, C, LB> ClientBuilder<IL, OL, C, LB> {
701700
/// default layers,
702701
///
703702
/// See [`ClientBuilder::build`] for more details.
704-
#[allow(clippy::result_large_err)]
705703
pub fn build_without_extra_layers(self) -> Result<C::Target>
706704
where
707705
IL: Layer<ClientTransport>,
@@ -745,7 +743,6 @@ impl<IL, OL, C, LB> ClientBuilder<IL, OL, C, LB> {
745743
}
746744
}
747745

748-
#[allow(clippy::result_large_err)]
749746
fn insert_header<K, V>(headers: &mut HeaderMap, key: K, value: V) -> Result<()>
750747
where
751748
K: TryInto<HeaderName>,

volo-http/src/client/target.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ pub enum RemoteTargetAddress {
5454
Name(FastStr),
5555
}
5656

57-
#[allow(clippy::result_large_err)]
5857
fn check_scheme(scheme: &Scheme) -> Result<()> {
5958
if scheme == &Scheme::HTTPS {
6059
#[cfg(not(feature = "__tls"))]
@@ -123,7 +122,6 @@ impl Target {
123122
}
124123

125124
/// Create a [`Target`] through a scheme, host name and a port
126-
#[allow(clippy::result_large_err)]
127125
pub fn new_host<S>(scheme: Option<Scheme>, host: S, port: Option<u16>) -> Result<Self>
128126
where
129127
S: Into<Cow<'static, str>>,
@@ -140,7 +138,6 @@ impl Target {
140138
}
141139

142140
/// Create a [`Target`] through a scheme, ip address and a port
143-
#[allow(clippy::result_large_err)]
144141
pub fn new_addr(scheme: Option<Scheme>, ip: IpAddr, port: Option<u16>) -> Result<Self> {
145142
let scheme = scheme.unwrap_or(Scheme::HTTP);
146143
check_scheme(&scheme)?;
@@ -163,7 +160,6 @@ impl Target {
163160
}
164161

165162
/// Create a [`Target`] from [`Uri`]
166-
#[allow(clippy::result_large_err)]
167163
pub fn from_uri(uri: &Uri) -> Result<Self> {
168164
let scheme = uri.scheme().cloned().unwrap_or(Scheme::HTTP);
169165
check_scheme(&scheme)?;
@@ -192,7 +188,6 @@ impl Target {
192188
///
193189
/// Note that if the previous is default port of the previous scheme, the port will be also
194190
/// updated to default port of the new scheme.
195-
#[allow(clippy::result_large_err)]
196191
pub fn set_scheme(&mut self, scheme: Scheme) -> Result<()> {
197192
let rt = match self.remote_mut() {
198193
Some(rt) => rt,
@@ -210,7 +205,6 @@ impl Target {
210205
}
211206

212207
/// Set a new port to the [`Target`]
213-
#[allow(clippy::result_large_err)]
214208
pub fn set_port(&mut self, port: u16) -> Result<()> {
215209
let rt = match self.remote_mut() {
216210
Some(rt) => rt,

volo-http/src/client/test_helpers.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ impl Service<ClientContext, Request> for MockTransport {
109109

110110
impl<IL, OL, C, LB> ClientBuilder<IL, OL, C, LB> {
111111
/// Build a mock HTTP client with a [`MockTransport`] service.
112-
#[allow(clippy::result_large_err)]
113112
pub fn mock(self, transport: MockTransport) -> Result<C::Target>
114113
where
115114
IL: Layer<ClientMockService>,

volo-http/src/error/client.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ pub type Result<T, E = ClientError> = std::result::Result<T, E>;
1717
pub struct ClientError {
1818
kind: ErrorKind,
1919
source: Option<BoxError>,
20-
uri: Option<Uri>,
20+
// The type `Uri` takes up 88 bytes, which causes the entire `ClientError` to take up 144 bytes
21+
// and clippy will throw `clippy::result_large_err`.
22+
//
23+
// Considering that the field will not be used in the hot path, in order to avoid the problem
24+
// of `ClientError` being too large, we added `Box` to `Uri`.
25+
uri: Option<Box<Uri>>,
2126
addr: Option<SocketAddr>,
2227
}
2328

@@ -38,7 +43,7 @@ impl ClientError {
3843
/// Set a [`Uri`] to the [`ClientError`].
3944
#[inline]
4045
pub fn set_url(&mut self, uri: Uri) {
41-
self.uri = Some(uri);
46+
self.uri = Some(Box::new(uri));
4247
}
4348

4449
/// Set a [`SocketAddr`] to the [`ClientError`].
@@ -50,7 +55,7 @@ impl ClientError {
5055
/// Consume current [`ClientError`] and return a new one with given [`Uri`].
5156
#[inline]
5257
pub fn with_url(mut self, uri: Uri) -> Self {
53-
self.uri = Some(uri);
58+
self.uri = Some(Box::new(uri));
5459
self
5560
}
5661

@@ -99,7 +104,7 @@ impl ClientError {
99104
/// Get a reference to the [`Uri`] if it exists
100105
#[inline]
101106
pub fn uri(&self) -> Option<&Uri> {
102-
self.uri.as_ref()
107+
self.uri.as_deref()
103108
}
104109

105110
/// Get a reference to the [`SocketAddr`] if it exists

0 commit comments

Comments
 (0)