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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

use alloc::{boxed::Box, string::String, vec::Vec};
use safer_ffi::layout::ReprC;

pub use safer_ffi;
pub use safer_ffi_gen_macro::*;

mod async_util;
mod error;
mod opaque;

pub use async_util::*;

#[cfg(feature = "std")]
pub use error::last_error;

/// Private definitions used by macros. These are always considered unstable and can have breaking
/// changes between semver-compatible releases of this crate. Do not use.
#[doc(hidden)]
pub mod private {
    #[cfg(feature = "std")]
    pub use crate::error::{set_last_error, WrapDebug, WrapError, WrapNotDebug, WrapNotError};

    pub use crate::{
        error::{WrapIntoInt, WrapNotIntoInt},
        opaque::OpaqueLayout,
    };
}

pub trait FfiType {
    type Safe;

    fn into_safe(self) -> Self::Safe;
    fn from_safe(x: Self::Safe) -> Self;
}

macro_rules! impl_ffi_type_as_identity {
    ($ty:ty) => {
        impl FfiType for $ty {
            type Safe = $ty;

            fn into_safe(self) -> $ty {
                self
            }

            fn from_safe(x: $ty) -> $ty {
                x
            }
        }
    };
}

impl_ffi_type_as_identity!(i8);
impl_ffi_type_as_identity!(u8);
impl_ffi_type_as_identity!(i16);
impl_ffi_type_as_identity!(u16);
impl_ffi_type_as_identity!(i32);
impl_ffi_type_as_identity!(u32);
impl_ffi_type_as_identity!(i64);
impl_ffi_type_as_identity!(u64);
impl_ffi_type_as_identity!(bool);
impl_ffi_type_as_identity!(());

impl<'a> FfiType for &'a str {
    type Safe = safer_ffi::string::str_ref<'a>;

    fn into_safe(self) -> Self::Safe {
        self.into()
    }

    fn from_safe(x: Self::Safe) -> Self {
        x.as_str()
    }
}

impl FfiType for String {
    type Safe = safer_ffi::String;

    fn into_safe(self) -> Self::Safe {
        self.into()
    }

    fn from_safe(x: Self::Safe) -> Self {
        x.into()
    }
}

impl<'a, T: ReprC + 'a> FfiType for &'a [T] {
    type Safe = safer_ffi::slice::slice_ref<'a, T>;

    fn into_safe(self) -> Self::Safe {
        self.into()
    }

    fn from_safe(x: Self::Safe) -> Self {
        x.as_slice()
    }
}

impl<'a, T: ReprC + 'a> FfiType for &'a mut [T] {
    type Safe = safer_ffi::slice::slice_mut<'a, T>;

    fn into_safe(self) -> Self::Safe {
        self.into()
    }

    fn from_safe(x: Self::Safe) -> Self {
        x.as_slice()
    }
}

impl<T: ReprC> FfiType for Vec<T> {
    type Safe = safer_ffi::Vec<T>;

    fn into_safe(self) -> Self::Safe {
        self.into()
    }

    fn from_safe(x: Self::Safe) -> Self {
        x.into()
    }
}

impl<T: ReprC> FfiType for Box<T> {
    type Safe = safer_ffi::boxed::Box<T>;

    fn into_safe(self) -> Self::Safe {
        self.into()
    }

    fn from_safe(x: Self::Safe) -> Self {
        x.into()
    }
}

impl<T> FfiType for Option<T>
where
    T: FfiType,
    Option<T::Safe>: ReprC,
{
    type Safe = Option<T::Safe>;

    fn into_safe(self) -> Self::Safe {
        self.map(T::into_safe)
    }

    fn from_safe(x: Self::Safe) -> Self {
        x.map(T::from_safe)
    }
}

impl<'a, T: ReprC> FfiType for &'a T {
    type Safe = &'a T;

    fn into_safe(self) -> Self::Safe {
        self
    }

    fn from_safe(x: Self::Safe) -> Self {
        x
    }
}

impl<'a, T: ReprC> FfiType for &'a mut T {
    type Safe = &'a mut T;

    fn into_safe(self) -> Self::Safe {
        self
    }

    fn from_safe(x: Self::Safe) -> Self {
        x
    }
}

impl<T: FfiType, U: FfiType> FfiType for (T, U) {
    type Safe = safer_ffi::Tuple2<T::Safe, U::Safe>;

    fn into_safe(self) -> Self::Safe {
        safer_ffi::Tuple2 {
            _0: self.0.into_safe(),
            _1: self.1.into_safe(),
        }
    }

    fn from_safe(x: Self::Safe) -> Self {
        (T::from_safe(x._0), U::from_safe(x._1))
    }
}

impl<const N: usize, T: ReprC> FfiType for [T; N] {
    type Safe = Self;

    fn into_safe(self) -> Self::Safe {
        self
    }

    fn from_safe(x: Self::Safe) -> Self {
        x
    }
}

#[safer_ffi::ffi_export]
pub fn vec_u8_free(_v: safer_ffi::vec::Vec<u8>) {}