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
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
Ffi-safe versions of some `std::io` types.
*/

use std::{
    error::Error as ErrorTrait,
    fmt::{self, Debug, Display},
    io::{Error as ioError, ErrorKind},
};

#[allow(unused_imports)]
use core_extensions::prelude::*;

use crate::{traits::{IntoReprC}, std_types::{RBoxError}};

/// Ffi safe equivalent to ::std::io::ErrorKind.
///
/// Using a struct with associated constants is the 
/// ffi-safe way of doing `#[non_exhaustive]` field-less enums.
#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[repr(C)]
#[derive(StableAbi)]
#[sabi(inside_abi_stable_crate)]
pub struct RIoErrorKind {
    value: u8,
}

/// Every (visible) variant of RIoErrorKind,equivalent to that of ::std::io::ErrorKind.
#[allow(non_upper_case_globals)]
impl RIoErrorKind {
    pub const Other: Self = RIoErrorKind { value: 0 };
    pub const NotFound: Self = RIoErrorKind { value: 1 };
    pub const PermissionDenied: Self = RIoErrorKind { value: 2 };
    pub const ConnectionRefused: Self = RIoErrorKind { value: 3 };
    pub const ConnectionReset: Self = RIoErrorKind { value: 4 };
    pub const ConnectionAborted: Self = RIoErrorKind { value: 5 };
    pub const NotConnected: Self = RIoErrorKind { value: 6 };
    pub const AddrInUse: Self = RIoErrorKind { value: 7 };
    pub const AddrNotAvailable: Self = RIoErrorKind { value: 8 };
    pub const BrokenPipe: Self = RIoErrorKind { value: 9 };
    pub const AlreadyExists: Self = RIoErrorKind { value: 10 };
    pub const WouldBlock: Self = RIoErrorKind { value: 11 };
    pub const InvalidInput: Self = RIoErrorKind { value: 12 };
    pub const InvalidData: Self = RIoErrorKind { value: 13 };
    pub const TimedOut: Self = RIoErrorKind { value: 14 };
    pub const WriteZero: Self = RIoErrorKind { value: 15 };
    pub const Interrupted: Self = RIoErrorKind { value: 16 };
    pub const UnexpectedEof: Self = RIoErrorKind { value: 17 };
}

impl Debug for RIoErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match *self {
            RIoErrorKind::NotFound => "NotFound",
            RIoErrorKind::PermissionDenied => "PermissionDenied",
            RIoErrorKind::ConnectionRefused => "ConnectionRefused",
            RIoErrorKind::ConnectionReset => "ConnectionReset",
            RIoErrorKind::ConnectionAborted => "ConnectionAborted",
            RIoErrorKind::NotConnected => "NotConnected",
            RIoErrorKind::AddrInUse => "AddrInUse",
            RIoErrorKind::AddrNotAvailable => "AddrNotAvailable",
            RIoErrorKind::BrokenPipe => "BrokenPipe",
            RIoErrorKind::AlreadyExists => "AlreadyExists",
            RIoErrorKind::WouldBlock => "WouldBlock",
            RIoErrorKind::InvalidInput => "InvalidInput",
            RIoErrorKind::InvalidData => "InvalidData",
            RIoErrorKind::TimedOut => "TimedOut",
            RIoErrorKind::WriteZero => "WriteZero",
            RIoErrorKind::Interrupted => "Interrupted",
            RIoErrorKind::UnexpectedEof => "UnexpectedEof",
            _ => "Other",
        };
        Display::fmt(s, f)
    }
}

impl_from_rust_repr! {
    impl From<ErrorKind> for RIoErrorKind {
        fn(this){
            match this {
                ErrorKind::NotFound=>RIoErrorKind::NotFound,
                ErrorKind::PermissionDenied=>RIoErrorKind::PermissionDenied,
                ErrorKind::ConnectionRefused=>RIoErrorKind::ConnectionRefused,
                ErrorKind::ConnectionReset=>RIoErrorKind::ConnectionReset,
                ErrorKind::ConnectionAborted=>RIoErrorKind::ConnectionAborted,
                ErrorKind::NotConnected=>RIoErrorKind::NotConnected,
                ErrorKind::AddrInUse=>RIoErrorKind::AddrInUse,
                ErrorKind::AddrNotAvailable=>RIoErrorKind::AddrNotAvailable,
                ErrorKind::BrokenPipe=>RIoErrorKind::BrokenPipe,
                ErrorKind::AlreadyExists=>RIoErrorKind::AlreadyExists,
                ErrorKind::WouldBlock=>RIoErrorKind::WouldBlock,
                ErrorKind::InvalidInput=>RIoErrorKind::InvalidInput,
                ErrorKind::InvalidData=>RIoErrorKind::InvalidData,
                ErrorKind::TimedOut=>RIoErrorKind::TimedOut,
                ErrorKind::WriteZero=>RIoErrorKind::WriteZero,
                ErrorKind::Interrupted=>RIoErrorKind::Interrupted,
                ErrorKind::UnexpectedEof=>RIoErrorKind::UnexpectedEof,
                _=>RIoErrorKind::Other,
            }
        }
    }
}

impl_into_rust_repr! {
    impl Into<ErrorKind> for RIoErrorKind {
        fn(this){
            match this {
                RIoErrorKind::NotFound=>ErrorKind::NotFound,
                RIoErrorKind::PermissionDenied=>ErrorKind::PermissionDenied,
                RIoErrorKind::ConnectionRefused=>ErrorKind::ConnectionRefused,
                RIoErrorKind::ConnectionReset=>ErrorKind::ConnectionReset,
                RIoErrorKind::ConnectionAborted=>ErrorKind::ConnectionAborted,
                RIoErrorKind::NotConnected=>ErrorKind::NotConnected,
                RIoErrorKind::AddrInUse=>ErrorKind::AddrInUse,
                RIoErrorKind::AddrNotAvailable=>ErrorKind::AddrNotAvailable,
                RIoErrorKind::BrokenPipe=>ErrorKind::BrokenPipe,
                RIoErrorKind::AlreadyExists=>ErrorKind::AlreadyExists,
                RIoErrorKind::WouldBlock=>ErrorKind::WouldBlock,
                RIoErrorKind::InvalidInput=>ErrorKind::InvalidInput,
                RIoErrorKind::InvalidData=>ErrorKind::InvalidData,
                RIoErrorKind::TimedOut=>ErrorKind::TimedOut,
                RIoErrorKind::WriteZero=>ErrorKind::WriteZero,
                RIoErrorKind::Interrupted=>ErrorKind::Interrupted,
                RIoErrorKind::UnexpectedEof=>ErrorKind::UnexpectedEof,
                _=>ErrorKind::Other,
            }
        }
    }
}

///////////////////////////////////////////////////////////////////////////

/// Ffi safe equivalent to ::std::io::Error.
///
/// I can be created from an  io::Error,but it cannot be converted back.
#[repr(C)]
#[derive(Debug, StableAbi)]
#[sabi(inside_abi_stable_crate)]
pub struct RIoError {
    kind: RIoErrorKind,
    error: RBoxError,
}

impl_from_rust_repr! {
    impl From<ioError> for RIoError {
        fn(this){
            RIoError{
                kind:this.kind().into(),
                error:this.piped(RBoxError::new)
            }
        }
    }
}

impl RIoError {
    /// Constructs an RIoError from an error and a io::ErrorKind.
    pub fn new<E>(kind: ErrorKind, error: E) -> Self
    where
        E: ErrorTrait + Send + Sync + 'static,
    {
        RIoError {
            kind: kind.into_c(),
            error: error.piped(RBoxError::new),
        }
    }
}

impl Display for RIoError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

impl ErrorTrait for RIoError {}