mirror of
https://git.proxmox.com/git/proxmox
synced 2025-10-04 23:08:26 +00:00

For regular error cases, `std::io::Error` can now also box errors and provides an `Error::other()` function since rust 1.74. For the case where it was used directly with strings (io_err_other("string")) -> that's what `io_format_err!()` and `io_bail!()` are for. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
39 lines
1.1 KiB
Rust
39 lines
1.1 KiB
Rust
//! A set of macros/helpers for I/O handling. These provide for `std::io::Error` what `anyhow` provides
|
|
//! for `anyhow::Error.`
|
|
|
|
use std::io;
|
|
|
|
/// Helper to convert non-system-errors into an `io::Error` or `io::ErrorKind::Other`.
|
|
///
|
|
/// A more convenient way is to use the `io_format_err!` macro.
|
|
#[deprecated = "use std::io::Error::other instead to box the original error"]
|
|
pub fn io_err_other<E: ToString>(e: E) -> io::Error {
|
|
io::Error::new(std::io::ErrorKind::Other, e.to_string())
|
|
}
|
|
|
|
/// Like anyhow's `format_err` but producing a `std::io::Error`.
|
|
#[macro_export]
|
|
macro_rules! io_format_err {
|
|
($($msg:tt)+) => {
|
|
::std::io::Error::new(::std::io::ErrorKind::Other, format!($($msg)+))
|
|
};
|
|
}
|
|
|
|
/// Shortcut to return an `io::Error::last_os_error`.
|
|
///
|
|
/// This is effectively `return Err(::std::io::Error::last_os_error().into());`.
|
|
#[macro_export]
|
|
macro_rules! io_bail_last {
|
|
() => {{
|
|
return Err(::std::io::Error::last_os_error().into());
|
|
}};
|
|
}
|
|
|
|
/// Like anyhow's `bail` but producing a `std::io::Error`.
|
|
#[macro_export]
|
|
macro_rules! io_bail {
|
|
($($msg:tt)+) => {{
|
|
return Err($crate::io_format_err!($($msg)+));
|
|
}};
|
|
}
|