summaryrefslogtreecommitdiff
path: root/crates/futures-copy/src/arc_io_result.rs
blob: d7c949b52ffb314d34bd70d5cc1fb75c3d5edf03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//! Helper: A cloneable wrapper for io::Result.
//!
//! This is all necessary because io::Error doesn't implement `Clone`.

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

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

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

impl<T: Clone> ArcIoResultExt<T> for ArcIoResult<T> {
    fn io_result(&self) -> io::Result<T> {
        match &self {
            Ok(r) => Ok(r.clone()),
            Err(e) => Err(wrap_error(e)),
        }
    }
}

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