aboutsummaryrefslogtreecommitdiff
path: root/crates/futures-copy/src/arc_io_result.rs
blob: ce25175d2e7ecf3ef95e724b45372cca25bb6aa6 (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
//! Helper: A cloneable wrapper for io::Result.
//!
//! This is all necessary because io::Error doesn't implement `Clone`.

use extend::ext;
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>>`
#[ext(name = ArcIoResultExt)]
pub(crate) impl<T: Clone> Result<T, Arc<io::Error>> {
    /// 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> {
        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))
}