Skip to content

Commit d71a464

Browse files
matthiaskrgrgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#136844 - thaliaarchi:const-io-error, r=ChrisDenton
Use `const_error!` when possible Replace usages of `io::Error::new(io::ErrorKind::Variant, "constant string")` with `io::const_error!(io::ErrorKind::Variant, "constant string")` to avoid allocations when possible. Additionally, fix `&&str` error messages in SGX and missing/misplaced trailing commas in `const_error!`.
2 parents 1b96604 + 58f85de commit d71a464

File tree

23 files changed

+90
-106
lines changed

23 files changed

+90
-106
lines changed

std/src/io/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Error {
8383

8484
pub(crate) const UNKNOWN_THREAD_COUNT: Self = const_error!(
8585
ErrorKind::NotFound,
86-
"The number of hardware threads is not known for the target platform"
86+
"The number of hardware threads is not known for the target platform",
8787
);
8888

8989
pub(crate) const UNSUPPORTED_PLATFORM: Self =

std/src/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3575,7 +3575,7 @@ impl Error for StripPrefixError {
35753575
pub fn absolute<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
35763576
let path = path.as_ref();
35773577
if path.as_os_str().is_empty() {
3578-
Err(io::const_error!(io::ErrorKind::InvalidInput, "cannot make an empty path absolute",))
3578+
Err(io::const_error!(io::ErrorKind::InvalidInput, "cannot make an empty path absolute"))
35793579
} else {
35803580
sys::path::absolute(path)
35813581
}

std/src/sys/net/connection/xous/dns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,6 @@ impl TryFrom<(&str, u16)> for LookupHost {
123123
type Error = io::Error;
124124

125125
fn try_from(v: (&str, u16)) -> io::Result<LookupHost> {
126-
lookup(v.0, v.1).map_err(|_e| io::const_error!(io::ErrorKind::InvalidInput, &"DNS failure"))
126+
lookup(v.0, v.1).map_err(|_e| io::const_error!(io::ErrorKind::InvalidInput, "DNS failure"))
127127
}
128128
}

std/src/sys/net/connection/xous/tcplistener.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ macro_rules! unimpl {
1111
() => {
1212
return Err(io::const_error!(
1313
io::ErrorKind::Unsupported,
14-
&"This function is not yet implemented",
14+
"This function is not yet implemented",
1515
));
1616
};
1717
}
@@ -71,7 +71,7 @@ impl TcpListener {
7171
0,
7272
4096,
7373
) else {
74-
return Err(io::const_error!(io::ErrorKind::InvalidInput, &"Invalid response"));
74+
return Err(io::const_error!(io::ErrorKind::InvalidInput, "Invalid response"));
7575
};
7676

7777
// The first four bytes should be zero upon success, and will be nonzero
@@ -80,15 +80,15 @@ impl TcpListener {
8080
if response[0] != 0 || valid == 0 {
8181
let errcode = response[1];
8282
if errcode == NetError::SocketInUse as u8 {
83-
return Err(io::const_error!(io::ErrorKind::ResourceBusy, &"Socket in use"));
83+
return Err(io::const_error!(io::ErrorKind::ResourceBusy, "Socket in use"));
8484
} else if errcode == NetError::Invalid as u8 {
85-
return Err(io::const_error!(io::ErrorKind::AddrNotAvailable, &"Invalid address"));
85+
return Err(io::const_error!(io::ErrorKind::AddrNotAvailable, "Invalid address"));
8686
} else if errcode == NetError::LibraryError as u8 {
87-
return Err(io::const_error!(io::ErrorKind::Other, &"Library error"));
87+
return Err(io::const_error!(io::ErrorKind::Other, "Library error"));
8888
} else {
8989
return Err(io::const_error!(
9090
io::ErrorKind::Other,
91-
&"Unable to connect or internal error"
91+
"Unable to connect or internal error",
9292
));
9393
}
9494
}
@@ -127,15 +127,13 @@ impl TcpListener {
127127
if receive_request.raw[0] != 0 {
128128
// error case
129129
if receive_request.raw[1] == NetError::TimedOut as u8 {
130-
return Err(io::const_error!(io::ErrorKind::TimedOut, &"accept timed out",));
130+
return Err(io::const_error!(io::ErrorKind::TimedOut, "accept timed out"));
131131
} else if receive_request.raw[1] == NetError::WouldBlock as u8 {
132-
return Err(
133-
io::const_error!(io::ErrorKind::WouldBlock, &"accept would block",),
134-
);
132+
return Err(io::const_error!(io::ErrorKind::WouldBlock, "accept would block"));
135133
} else if receive_request.raw[1] == NetError::LibraryError as u8 {
136-
return Err(io::const_error!(io::ErrorKind::Other, &"Library error"));
134+
return Err(io::const_error!(io::ErrorKind::Other, "Library error"));
137135
} else {
138-
return Err(io::const_error!(io::ErrorKind::Other, &"library error",));
136+
return Err(io::const_error!(io::ErrorKind::Other, "library error"));
139137
}
140138
} else {
141139
// accept successful
@@ -159,7 +157,7 @@ impl TcpListener {
159157
port,
160158
)
161159
} else {
162-
return Err(io::const_error!(io::ErrorKind::Other, &"library error",));
160+
return Err(io::const_error!(io::ErrorKind::Other, "library error"));
163161
};
164162

165163
// replenish the listener
@@ -171,7 +169,7 @@ impl TcpListener {
171169
Ok((TcpStream::from_listener(stream_fd, self.local.port(), port, addr), addr))
172170
}
173171
} else {
174-
Err(io::const_error!(io::ErrorKind::InvalidInput, &"Unable to accept"))
172+
Err(io::const_error!(io::ErrorKind::InvalidInput, "Unable to accept"))
175173
}
176174
}
177175

@@ -182,13 +180,13 @@ impl TcpListener {
182180

183181
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
184182
if ttl > 255 {
185-
return Err(io::Error::new(io::ErrorKind::InvalidInput, "TTL must be less than 256"));
183+
return Err(io::const_error!(io::ErrorKind::InvalidInput, "TTL must be less than 256"));
186184
}
187185
crate::os::xous::ffi::blocking_scalar(
188186
services::net_server(),
189187
services::NetBlockingScalar::StdSetTtlTcp(self.fd.load(Ordering::Relaxed), ttl).into(),
190188
)
191-
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, &"Unexpected return value")))
189+
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, "Unexpected return value")))
192190
.map(|_| ())
193191
}
194192

@@ -197,7 +195,7 @@ impl TcpListener {
197195
services::net_server(),
198196
services::NetBlockingScalar::StdGetTtlTcp(self.fd.load(Ordering::Relaxed)).into(),
199197
)
200-
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, &"Unexpected return value")))
198+
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, "Unexpected return value")))
201199
.map(|res| res[0] as _)?)
202200
}
203201

std/src/sys/net/connection/xous/tcpstream.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ macro_rules! unimpl {
1212
() => {
1313
return Err(io::const_error!(
1414
io::ErrorKind::Unsupported,
15-
&"This function is not yet implemented",
15+
"This function is not yet implemented",
1616
));
1717
};
1818
}
@@ -96,7 +96,7 @@ impl TcpStream {
9696
0,
9797
4096,
9898
) else {
99-
return Err(io::const_error!(io::ErrorKind::InvalidInput, &"Invalid response"));
99+
return Err(io::const_error!(io::ErrorKind::InvalidInput, "Invalid response"));
100100
};
101101

102102
// The first four bytes should be zero upon success, and will be nonzero
@@ -106,13 +106,13 @@ impl TcpStream {
106106
// errcode is a u8 but stuck in a u16 where the upper byte is invalid. Mask & decode accordingly.
107107
let errcode = response[0];
108108
if errcode == NetError::SocketInUse as u8 {
109-
return Err(io::const_error!(io::ErrorKind::ResourceBusy, &"Socket in use",));
109+
return Err(io::const_error!(io::ErrorKind::ResourceBusy, "Socket in use"));
110110
} else if errcode == NetError::Unaddressable as u8 {
111-
return Err(io::const_error!(io::ErrorKind::AddrNotAvailable, &"Invalid address",));
111+
return Err(io::const_error!(io::ErrorKind::AddrNotAvailable, "Invalid address"));
112112
} else {
113113
return Err(io::const_error!(
114114
io::ErrorKind::InvalidInput,
115-
&"Unable to connect or internal error",
115+
"Unable to connect or internal error",
116116
));
117117
}
118118
}
@@ -198,7 +198,7 @@ impl TcpStream {
198198
) else {
199199
return Err(io::const_error!(
200200
io::ErrorKind::InvalidInput,
201-
&"Library failure: wrong message type or messaging error"
201+
"Library failure: wrong message type or messaging error",
202202
));
203203
};
204204

@@ -212,14 +212,14 @@ impl TcpStream {
212212
if result[0] != 0 {
213213
if result[1] == 8 {
214214
// timed out
215-
return Err(io::const_error!(io::ErrorKind::TimedOut, &"Timeout",));
215+
return Err(io::const_error!(io::ErrorKind::TimedOut, "Timeout"));
216216
}
217217
if result[1] == 9 {
218218
// would block
219-
return Err(io::const_error!(io::ErrorKind::WouldBlock, &"Would block",));
219+
return Err(io::const_error!(io::ErrorKind::WouldBlock, "Would block"));
220220
}
221221
}
222-
Err(io::const_error!(io::ErrorKind::Other, &"recv_slice failure"))
222+
Err(io::const_error!(io::ErrorKind::Other, "recv_slice failure"))
223223
}
224224
}
225225

@@ -258,20 +258,20 @@ impl TcpStream {
258258
self.write_timeout.load(Ordering::Relaxed) as usize,
259259
buf_len,
260260
)
261-
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, &"Internal error")))?;
261+
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, "Internal error")))?;
262262

263263
if send_request.raw[0] != 0 {
264264
if send_request.raw[4] == 8 {
265265
// timed out
266266
return Err(io::const_error!(
267267
io::ErrorKind::BrokenPipe,
268-
&"Timeout or connection closed",
268+
"Timeout or connection closed",
269269
));
270270
} else if send_request.raw[4] == 9 {
271271
// would block
272-
return Err(io::const_error!(io::ErrorKind::WouldBlock, &"Would block",));
272+
return Err(io::const_error!(io::ErrorKind::WouldBlock, "Would block"));
273273
} else {
274-
return Err(io::const_error!(io::ErrorKind::InvalidInput, &"Error when sending",));
274+
return Err(io::const_error!(io::ErrorKind::InvalidInput, "Error when sending"));
275275
}
276276
}
277277
Ok(u32::from_le_bytes([
@@ -304,7 +304,7 @@ impl TcpStream {
304304
0,
305305
0,
306306
) else {
307-
return Err(io::const_error!(io::ErrorKind::InvalidInput, &"Internal error"));
307+
return Err(io::const_error!(io::ErrorKind::InvalidInput, "Internal error"));
308308
};
309309
let mut i = get_addr.raw.iter();
310310
match *i.next().unwrap() {
@@ -324,7 +324,7 @@ impl TcpStream {
324324
}
325325
Ok(SocketAddr::V6(SocketAddrV6::new(new_addr.into(), self.local_port, 0, 0)))
326326
}
327-
_ => Err(io::const_error!(io::ErrorKind::InvalidInput, &"Internal error")),
327+
_ => Err(io::const_error!(io::ErrorKind::InvalidInput, "Internal error")),
328328
}
329329
}
330330

@@ -333,7 +333,7 @@ impl TcpStream {
333333
services::net_server(),
334334
services::NetBlockingScalar::StdTcpStreamShutdown(self.fd, how).into(),
335335
)
336-
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, &"Unexpected return value")))
336+
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, "Unexpected return value")))
337337
.map(|_| ())
338338
}
339339

@@ -355,7 +355,7 @@ impl TcpStream {
355355
services::net_server(),
356356
services::NetBlockingScalar::StdSetNodelay(self.fd, enabled).into(),
357357
)
358-
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, &"Unexpected return value")))
358+
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, "Unexpected return value")))
359359
.map(|_| ())
360360
}
361361

@@ -364,19 +364,19 @@ impl TcpStream {
364364
services::net_server(),
365365
services::NetBlockingScalar::StdGetNodelay(self.fd).into(),
366366
)
367-
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, &"Unexpected return value")))
367+
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, "Unexpected return value")))
368368
.map(|res| res[0] != 0)?)
369369
}
370370

371371
pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
372372
if ttl > 255 {
373-
return Err(io::Error::new(io::ErrorKind::InvalidInput, "TTL must be less than 256"));
373+
return Err(io::const_error!(io::ErrorKind::InvalidInput, "TTL must be less than 256"));
374374
}
375375
crate::os::xous::ffi::blocking_scalar(
376376
services::net_server(),
377377
services::NetBlockingScalar::StdSetTtlTcp(self.fd, ttl).into(),
378378
)
379-
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, &"Unexpected return value")))
379+
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, "Unexpected return value")))
380380
.map(|_| ())
381381
}
382382

@@ -385,7 +385,7 @@ impl TcpStream {
385385
services::net_server(),
386386
services::NetBlockingScalar::StdGetTtlTcp(self.fd).into(),
387387
)
388-
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, &"Unexpected return value")))
388+
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, "Unexpected return value")))
389389
.map(|res| res[0] as _)?)
390390
}
391391

0 commit comments

Comments
 (0)