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

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

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

            
10
/// Extension trait for `Result<T, Arc<io::Error>>`
11
pub(crate) trait ArcIoResultExt<T> {
12
    /// Create a new `io::Result<T>` from this `ArcIoResult<T>`
13
    ///
14
    /// We do this by making a new new io::Error (if necessary)
15
    /// with [`wrap_error`].
16
    fn io_result(&self) -> io::Result<T>;
17
}
18

            
19
impl<T: Clone> ArcIoResultExt<T> for ArcIoResult<T> {
20
60
    fn io_result(&self) -> io::Result<T> {
21
60
        match &self {
22
48
            Ok(r) => Ok(r.clone()),
23
12
            Err(e) => Err(wrap_error(e)),
24
        }
25
60
    }
26
}
27

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