Skip to content

Use const_error! when possible #136844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/std/src/io/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Error {

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

pub(crate) const UNSUPPORTED_PLATFORM: Self =
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3575,7 +3575,7 @@ impl Error for StripPrefixError {
pub fn absolute<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
let path = path.as_ref();
if path.as_os_str().is_empty() {
Err(io::const_error!(io::ErrorKind::InvalidInput, "cannot make an empty path absolute",))
Err(io::const_error!(io::ErrorKind::InvalidInput, "cannot make an empty path absolute"))
} else {
sys::path::absolute(path)
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/net/connection/xous/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,6 @@ impl TryFrom<(&str, u16)> for LookupHost {
type Error = io::Error;

fn try_from(v: (&str, u16)) -> io::Result<LookupHost> {
lookup(v.0, v.1).map_err(|_e| io::const_error!(io::ErrorKind::InvalidInput, &"DNS failure"))
lookup(v.0, v.1).map_err(|_e| io::const_error!(io::ErrorKind::InvalidInput, "DNS failure"))
}
}
32 changes: 15 additions & 17 deletions library/std/src/sys/net/connection/xous/tcplistener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! unimpl {
() => {
return Err(io::const_error!(
io::ErrorKind::Unsupported,
&"This function is not yet implemented",
"This function is not yet implemented",
));
};
}
Expand Down Expand Up @@ -71,7 +71,7 @@ impl TcpListener {
0,
4096,
) else {
return Err(io::const_error!(io::ErrorKind::InvalidInput, &"Invalid response"));
return Err(io::const_error!(io::ErrorKind::InvalidInput, "Invalid response"));
};

// The first four bytes should be zero upon success, and will be nonzero
Expand All @@ -80,15 +80,15 @@ impl TcpListener {
if response[0] != 0 || valid == 0 {
let errcode = response[1];
if errcode == NetError::SocketInUse as u8 {
return Err(io::const_error!(io::ErrorKind::ResourceBusy, &"Socket in use"));
return Err(io::const_error!(io::ErrorKind::ResourceBusy, "Socket in use"));
} else if errcode == NetError::Invalid as u8 {
return Err(io::const_error!(io::ErrorKind::AddrNotAvailable, &"Invalid address"));
return Err(io::const_error!(io::ErrorKind::AddrNotAvailable, "Invalid address"));
} else if errcode == NetError::LibraryError as u8 {
return Err(io::const_error!(io::ErrorKind::Other, &"Library error"));
return Err(io::const_error!(io::ErrorKind::Other, "Library error"));
} else {
return Err(io::const_error!(
io::ErrorKind::Other,
&"Unable to connect or internal error"
"Unable to connect or internal error",
));
}
}
Expand Down Expand Up @@ -127,15 +127,13 @@ impl TcpListener {
if receive_request.raw[0] != 0 {
// error case
if receive_request.raw[1] == NetError::TimedOut as u8 {
return Err(io::const_error!(io::ErrorKind::TimedOut, &"accept timed out",));
return Err(io::const_error!(io::ErrorKind::TimedOut, "accept timed out"));
} else if receive_request.raw[1] == NetError::WouldBlock as u8 {
return Err(
io::const_error!(io::ErrorKind::WouldBlock, &"accept would block",),
);
return Err(io::const_error!(io::ErrorKind::WouldBlock, "accept would block"));
} else if receive_request.raw[1] == NetError::LibraryError as u8 {
return Err(io::const_error!(io::ErrorKind::Other, &"Library error"));
return Err(io::const_error!(io::ErrorKind::Other, "Library error"));
} else {
return Err(io::const_error!(io::ErrorKind::Other, &"library error",));
return Err(io::const_error!(io::ErrorKind::Other, "library error"));
}
} else {
// accept successful
Expand All @@ -159,7 +157,7 @@ impl TcpListener {
port,
)
} else {
return Err(io::const_error!(io::ErrorKind::Other, &"library error",));
return Err(io::const_error!(io::ErrorKind::Other, "library error"));
};

// replenish the listener
Expand All @@ -171,7 +169,7 @@ impl TcpListener {
Ok((TcpStream::from_listener(stream_fd, self.local.port(), port, addr), addr))
}
} else {
Err(io::const_error!(io::ErrorKind::InvalidInput, &"Unable to accept"))
Err(io::const_error!(io::ErrorKind::InvalidInput, "Unable to accept"))
}
}

Expand All @@ -182,13 +180,13 @@ impl TcpListener {

pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
if ttl > 255 {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "TTL must be less than 256"));
return Err(io::const_error!(io::ErrorKind::InvalidInput, "TTL must be less than 256"));
}
crate::os::xous::ffi::blocking_scalar(
services::net_server(),
services::NetBlockingScalar::StdSetTtlTcp(self.fd.load(Ordering::Relaxed), ttl).into(),
)
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, &"Unexpected return value")))
.or(Err(io::const_error!(io::ErrorKind::InvalidInput, "Unexpected return value")))
.map(|_| ())
}

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

Expand Down
42 changes: 21 additions & 21 deletions library/std/src/sys/net/connection/xous/tcpstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ macro_rules! unimpl {
() => {
return Err(io::const_error!(
io::ErrorKind::Unsupported,
&"This function is not yet implemented",
"This function is not yet implemented",
));
};
}
Expand Down Expand Up @@ -96,7 +96,7 @@ impl TcpStream {
0,
4096,
) else {
return Err(io::const_error!(io::ErrorKind::InvalidInput, &"Invalid response"));
return Err(io::const_error!(io::ErrorKind::InvalidInput, "Invalid response"));
};

// The first four bytes should be zero upon success, and will be nonzero
Expand All @@ -106,13 +106,13 @@ impl TcpStream {
// errcode is a u8 but stuck in a u16 where the upper byte is invalid. Mask & decode accordingly.
let errcode = response[0];
if errcode == NetError::SocketInUse as u8 {
return Err(io::const_error!(io::ErrorKind::ResourceBusy, &"Socket in use",));
return Err(io::const_error!(io::ErrorKind::ResourceBusy, "Socket in use"));
} else if errcode == NetError::Unaddressable as u8 {
return Err(io::const_error!(io::ErrorKind::AddrNotAvailable, &"Invalid address",));
return Err(io::const_error!(io::ErrorKind::AddrNotAvailable, "Invalid address"));
} else {
return Err(io::const_error!(
io::ErrorKind::InvalidInput,
&"Unable to connect or internal error",
"Unable to connect or internal error",
));
}
}
Expand Down Expand Up @@ -198,7 +198,7 @@ impl TcpStream {
) else {
return Err(io::const_error!(
io::ErrorKind::InvalidInput,
&"Library failure: wrong message type or messaging error"
"Library failure: wrong message type or messaging error",
));
};

Expand All @@ -212,14 +212,14 @@ impl TcpStream {
if result[0] != 0 {
if result[1] == 8 {
// timed out
return Err(io::const_error!(io::ErrorKind::TimedOut, &"Timeout",));
return Err(io::const_error!(io::ErrorKind::TimedOut, "Timeout"));
}
if result[1] == 9 {
// would block
return Err(io::const_error!(io::ErrorKind::WouldBlock, &"Would block",));
return Err(io::const_error!(io::ErrorKind::WouldBlock, "Would block"));
}
}
Err(io::const_error!(io::ErrorKind::Other, &"recv_slice failure"))
Err(io::const_error!(io::ErrorKind::Other, "recv_slice failure"))
}
}

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

if send_request.raw[0] != 0 {
if send_request.raw[4] == 8 {
// timed out
return Err(io::const_error!(
io::ErrorKind::BrokenPipe,
&"Timeout or connection closed",
"Timeout or connection closed",
));
} else if send_request.raw[4] == 9 {
// would block
return Err(io::const_error!(io::ErrorKind::WouldBlock, &"Would block",));
return Err(io::const_error!(io::ErrorKind::WouldBlock, "Would block"));
} else {
return Err(io::const_error!(io::ErrorKind::InvalidInput, &"Error when sending",));
return Err(io::const_error!(io::ErrorKind::InvalidInput, "Error when sending"));
}
}
Ok(u32::from_le_bytes([
Expand Down Expand Up @@ -304,7 +304,7 @@ impl TcpStream {
0,
0,
) else {
return Err(io::const_error!(io::ErrorKind::InvalidInput, &"Internal error"));
return Err(io::const_error!(io::ErrorKind::InvalidInput, "Internal error"));
};
let mut i = get_addr.raw.iter();
match *i.next().unwrap() {
Expand All @@ -324,7 +324,7 @@ impl TcpStream {
}
Ok(SocketAddr::V6(SocketAddrV6::new(new_addr.into(), self.local_port, 0, 0)))
}
_ => Err(io::const_error!(io::ErrorKind::InvalidInput, &"Internal error")),
_ => Err(io::const_error!(io::ErrorKind::InvalidInput, "Internal error")),
}
}

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

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

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

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

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

Expand Down
Loading
Loading