1
//! Helper: A cloneable wrapper for io::Result.
2
//!
3
//! This is all necessary because io::Error doesn't implement `Clone`.
4

            
5
use extend::ext;
6
use std::{io, sync::Arc};
7

            
8
/// A helper type for a variation on an `io::Error` that we can clone.
9
pub(crate) type ArcIoResult<R> = Result<R, Arc<io::Error>>;
10

            
11
/// Extension trait for `Result<T, Arc<io::Error>>`
12
#[ext(name = ArcIoResultExt)]
13
pub(crate) impl<T: Clone> Result<T, Arc<io::Error>> {
14
    /// Create a new `io::Result<T>` from this `ArcIoResult<T>`
15
    ///
16
    /// We do this by making a new new io::Error (if necessary)
17
    /// with [`wrap_error`].
18
60
    fn io_result(&self) -> io::Result<T> {
19
60
        match &self {
20
48
            Ok(r) => Ok(r.clone()),
21
12
            Err(e) => Err(wrap_error(e)),
22
        }
23
60
    }
24
}
25

            
26
/// Wrap an Arc<io::Error> as a new io::Error.
27
12
pub(crate) fn wrap_error(e: &Arc<io::Error>) -> io::Error {
28
12
    io::Error::new(e.kind(), Arc::clone(e))
29
12
}