summaryrefslogtreecommitdiff
path: root/src/sys/socket/mod.rs
diff options
context:
space:
mode:
authorAlan Somers <asomers@gmail.com>2021-06-06 10:43:02 -0600
committerAlan Somers <asomers@gmail.com>2021-07-07 20:49:28 -0600
commit6511d02414ec9dd4bcaa237336c5f74869e6c41a (patch)
tree61e6cde2230efb026e9ac9c6a6e786cb6ba77642 /src/sys/socket/mod.rs
parentf90033ed35af02f39d4eaf28eff85f226c10d91b (diff)
downloadnix-6511d02414ec9dd4bcaa237336c5f74869e6c41a.zip
Overhaul Nix's error types
For many of Nix's consumers it be convenient to easily convert a Nix error into a std::io::Error. That's currently not possible because of the InvalidPath, InvalidUtf8, and UnsupportedOperation types that have no equivalent in std::io::Error. However, very few of Nix's public APIs actually return those unusual errors. So a more useful API would be for Nix's standard error type to implement Into<std::io::Error>. This commit makes Error a simple NewType around Errno. For most functions it's a drop-in replacement. There are only three exceptions: * clearenv now returns a bespoke error type. It was the only Nix function whose error couldn't be cleanly mapped onto an Errno. * sys::signal::signal now returns Error(Errno::ENOTSUP) instead of Error::UnsupportedOperation when the user passes an incompatible argument to `handler`. * When a NixPath exceeds PATH_MAX, it will now return Error(Errno::ENAMETOOLONG) instead of Error::InvalidPath. In the latter two cases there is now some abiguity about whether the error code was generated by Nix or by the OS. But I think the ambiguity is worth it for the sake of being able to implement Into<io::Error>. This commit also introduces Error::Sys() as a migration aid. Previously that as an enum variant. Now it's a function, but it will work in many of the same contexts as the original. Fixes #1155
Diffstat (limited to 'src/sys/socket/mod.rs')
-rw-r--r--src/sys/socket/mod.rs4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs
index 69070463..ff64b73f 100644
--- a/src/sys/socket/mod.rs
+++ b/src/sys/socket/mod.rs
@@ -1572,7 +1572,7 @@ pub fn recvfrom(sockfd: RawFd, buf: &mut [u8])
&mut len as *mut socklen_t))? as usize;
match sockaddr_storage_to_addr(&addr, len as usize) {
- Err(Error::Sys(Errno::ENOTCONN)) => Ok((ret, None)),
+ Err(Error(Errno::ENOTCONN)) => Ok((ret, None)),
Ok(addr) => Ok((ret, Some(addr))),
Err(e) => Err(e)
}
@@ -1708,7 +1708,7 @@ pub fn sockaddr_storage_to_addr(
assert!(len <= mem::size_of::<sockaddr_un>());
if len < mem::size_of_val(&addr.ss_family) {
- return Err(Error::Sys(Errno::ENOTCONN));
+ return Err(Error::from(Errno::ENOTCONN));
}
match c_int::from(addr.ss_family) {