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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use std::ffi::NulError;
use std::str::Utf8Error;

use thiserror::Error;

macro_rules! impl_c_repr_of_for {
    ($typ:ty) => {
        impl CReprOf<$typ> for $typ {
            fn c_repr_of(input: $typ) -> Result<$typ, CReprOfError> {
                Ok(input)
            }
        }
    };

    ($from_typ:ty, $to_typ:ty) => {
        impl CReprOf<$from_typ> for $to_typ {
            fn c_repr_of(input: $from_typ) -> Result<$to_typ, CReprOfError> {
                Ok(input as $to_typ)
            }
        }
    };
}

/// implements a noop implementation of the CDrop trait for a given type.
macro_rules! impl_c_drop_for {
    ($typ:ty) => {
        impl CDrop for $typ {
            fn do_drop(&mut self) -> Result<(), CDropError> {
                Ok(())
            }
        }
    };
}

macro_rules! impl_as_rust_for {
    ($typ:ty) => {
        impl AsRust<$typ> for $typ {
            fn as_rust(&self) -> Result<$typ, AsRustError> {
                Ok(*self)
            }
        }
    };

    ($from_typ:ty, $to_typ:ty) => {
        impl AsRust<$to_typ> for $from_typ {
            fn as_rust(&self) -> Result<$to_typ, AsRustError> {
                Ok(*self as $to_typ)
            }
        }
    };
}

macro_rules! impl_rawpointerconverter_for {
    ($typ:ty) => {
        impl RawPointerConverter<$typ> for $typ {
            fn into_raw_pointer(self) -> *const $typ {
                convert_into_raw_pointer(self)
            }
            fn into_raw_pointer_mut(self) -> *mut $typ {
                convert_into_raw_pointer_mut(self)
            }
            unsafe fn from_raw_pointer(
                input: *const $typ,
            ) -> Result<Self, UnexpectedNullPointerError> {
                take_back_from_raw_pointer(input)
            }
            unsafe fn from_raw_pointer_mut(
                input: *mut $typ,
            ) -> Result<Self, UnexpectedNullPointerError> {
                take_back_from_raw_pointer_mut(input)
            }
        }
    };
}

#[derive(Error, Debug)]
pub enum CReprOfError {
    #[error("A string contains a nul bit")]
    StringContainsNullBit(#[from] NulError),
    #[error("An error occurred during conversion to C repr; {}", .0)]
    Other(#[from] Box<dyn std::error::Error + Send + Sync>),
}

/// Trait showing that the struct implementing it is a `repr(C)` compatible view of the parametrized
/// type that can be created from an value of this type.
pub trait CReprOf<T>: Sized + CDrop {
    fn c_repr_of(input: T) -> Result<Self, CReprOfError>;
}

#[derive(Error, Debug)]
pub enum CDropError {
    #[error("unexpected null pointer")]
    NullPointer(#[from] UnexpectedNullPointerError),
    #[error("An error occurred while dropping C struct: {}", .0)]
    Other(#[from] Box<dyn std::error::Error + Send + Sync>),
}

/// Trait showing that the C-like struct implementing it can free up its part of memory that are not
/// managed by Rust.
pub trait CDrop {
    fn do_drop(&mut self) -> Result<(), CDropError>;
}

#[derive(Error, Debug)]
pub enum AsRustError {
    #[error("unexpected null pointer")]
    NullPointer(#[from] UnexpectedNullPointerError),

    #[error("could not convert string as it is not UTF-8: {}", .0)]
    Utf8Error(#[from] Utf8Error),
    #[error("An error occurred during conversion to Rust: {}", .0)]
    Other(#[from] Box<dyn std::error::Error + Send + Sync>),
}

/// Trait showing that the struct implementing it is a `repr(C)` compatible view of the parametrized
/// type and that an instance of the parametrized type can be created form this struct
pub trait AsRust<T> {
    fn as_rust(&self) -> Result<T, AsRustError>;
}

#[derive(Error, Debug)]
#[error("Could not use raw pointer: unexpected null pointer")]
pub struct UnexpectedNullPointerError;

/// Trait representing the creation of a raw pointer from a struct and the recovery of said pointer.
///
/// The `from_raw_pointer` function should be used only on pointers obtained through the
/// `into_raw_pointer` method (and is thus unsafe as we don't have any way to get insurance of that
/// from the compiler).
///
/// The `from_raw_pointer` effectively takes back ownership of the pointer. If you didn't create the
/// pointer yourself, please use the `as_ref` method on the raw pointer to borrow it
pub trait RawPointerConverter<T>: Sized {
    /// Creates a raw pointer from the value and leaks it, you should use [`Self::from_raw_pointer`]
    /// or [`Self::drop_raw_pointer`] to free the value when you're done with it.
    fn into_raw_pointer(self) -> *const T;
    /// Creates a mutable raw pointer from the value and leaks it, you should use
    /// [`Self::from_raw_pointer_mut`] or [`Self::drop_raw_pointer_mut`] to free the value when
    /// you're done with it.
    fn into_raw_pointer_mut(self) -> *mut T;
    /// Takes back control of a raw pointer created by [`Self::into_raw_pointer`].
    /// # Safety
    /// This method is unsafe because passing it a pointer that was not created by
    /// [`Self::into_raw_pointer`] can lead to memory problems. Also note that passing the same pointer
    /// twice to this function will probably result in a double free
    unsafe fn from_raw_pointer(input: *const T) -> Result<Self, UnexpectedNullPointerError>;
    /// Takes back control of a raw pointer created by [`Self::into_raw_pointer_mut`].
    /// # Safety
    /// This method is unsafe because passing it a pointer that was not created by
    /// [`Self::into_raw_pointer_mut`] can lead to memory problems. Also note that passing the same
    /// pointer twice to this function will probably result in a double free
    unsafe fn from_raw_pointer_mut(input: *mut T) -> Result<Self, UnexpectedNullPointerError>;

    /// Takes back control of a raw pointer created by [`Self::into_raw_pointer`] and drop it.
    /// # Safety
    /// This method is unsafe for the same reasons as [`Self::from_raw_pointer`]
    unsafe fn drop_raw_pointer(input: *const T) -> Result<(), UnexpectedNullPointerError> {
        Self::from_raw_pointer(input).map(|_| ())
    }

    /// Takes back control of a raw pointer created by [`Self::into_raw_pointer_mut`] and drops it.
    /// # Safety
    /// This method is unsafe for the same reasons a [`Self::from_raw_pointer_mut`]
    unsafe fn drop_raw_pointer_mut(input: *mut T) -> Result<(), UnexpectedNullPointerError> {
        Self::from_raw_pointer_mut(input).map(|_| ())
    }
}

#[doc(hidden)]
pub fn convert_into_raw_pointer<T>(pointee: T) -> *const T {
    Box::into_raw(Box::new(pointee)) as _
}

#[doc(hidden)]
pub fn convert_into_raw_pointer_mut<T>(pointee: T) -> *mut T {
    Box::into_raw(Box::new(pointee))
}

#[doc(hidden)]
pub unsafe fn take_back_from_raw_pointer<T>(
    input: *const T,
) -> Result<T, UnexpectedNullPointerError> {
    take_back_from_raw_pointer_mut(input as _)
}

#[doc(hidden)]
pub unsafe fn take_back_from_raw_pointer_mut<T>(
    input: *mut T,
) -> Result<T, UnexpectedNullPointerError> {
    if input.is_null() {
        Err(UnexpectedNullPointerError)
    } else {
        Ok(*Box::from_raw(input))
    }
}

/// Trait to create borrowed references to type T, from a raw pointer to a T. Note that this is
/// implemented for all types.
pub trait RawBorrow<T> {
    /// Get a reference on the value behind the pointer or return an error if the pointer is `null`.
    /// # Safety
    /// As this is using `*const T::as_ref()` this is unsafe for exactly the same reasons.
    unsafe fn raw_borrow<'a>(input: *const T) -> Result<&'a Self, UnexpectedNullPointerError>;
}

/// Trait to create mutable borrowed references to type T, from a raw pointer to a T. Note that this
/// is implemented for all types.
pub trait RawBorrowMut<T> {
    /// Get a mutable reference on the value behind the pointer or return an error if the pointer is
    /// `null`.
    /// # Safety
    /// As this is using `*mut T:as_ref()` this is unsafe for exactly the same reasons.
    unsafe fn raw_borrow_mut<'a>(input: *mut T)
        -> Result<&'a mut Self, UnexpectedNullPointerError>;
}

/// Trait that allows obtaining a borrowed reference to a type T from a raw pointer to T
impl<T> RawBorrow<T> for T {
    unsafe fn raw_borrow<'a>(input: *const T) -> Result<&'a Self, UnexpectedNullPointerError> {
        input.as_ref().ok_or(UnexpectedNullPointerError)
    }
}

/// Trait that allows obtaining a mutable borrowed reference to a type T from a raw pointer to T
impl<T> RawBorrowMut<T> for T {
    unsafe fn raw_borrow_mut<'a>(
        input: *mut T,
    ) -> Result<&'a mut Self, UnexpectedNullPointerError> {
        input.as_mut().ok_or(UnexpectedNullPointerError)
    }
}

impl RawPointerConverter<libc::c_void> for std::ffi::CString {
    fn into_raw_pointer(self) -> *const libc::c_void {
        self.into_raw() as _
    }

    fn into_raw_pointer_mut(self) -> *mut libc::c_void {
        self.into_raw() as _
    }

    unsafe fn from_raw_pointer(
        input: *const libc::c_void,
    ) -> Result<Self, UnexpectedNullPointerError> {
        Self::from_raw_pointer_mut(input as *mut libc::c_void)
    }

    unsafe fn from_raw_pointer_mut(
        input: *mut libc::c_void,
    ) -> Result<Self, UnexpectedNullPointerError> {
        if input.is_null() {
            Err(UnexpectedNullPointerError)
        } else {
            Ok(std::ffi::CString::from_raw(input as *mut libc::c_char))
        }
    }
}

impl RawPointerConverter<libc::c_char> for std::ffi::CString {
    fn into_raw_pointer(self) -> *const libc::c_char {
        self.into_raw() as _
    }

    fn into_raw_pointer_mut(self) -> *mut libc::c_char {
        self.into_raw()
    }

    unsafe fn from_raw_pointer(
        input: *const libc::c_char,
    ) -> Result<Self, UnexpectedNullPointerError> {
        Self::from_raw_pointer_mut(input as *mut libc::c_char)
    }

    unsafe fn from_raw_pointer_mut(
        input: *mut libc::c_char,
    ) -> Result<Self, UnexpectedNullPointerError> {
        if input.is_null() {
            Err(UnexpectedNullPointerError)
        } else {
            Ok(std::ffi::CString::from_raw(input as *mut libc::c_char))
        }
    }
}

impl RawBorrow<libc::c_char> for std::ffi::CStr {
    unsafe fn raw_borrow<'a>(
        input: *const libc::c_char,
    ) -> Result<&'a Self, UnexpectedNullPointerError> {
        if input.is_null() {
            Err(UnexpectedNullPointerError)
        } else {
            Ok(Self::from_ptr(input))
        }
    }
}

impl_c_drop_for!(usize);
impl_c_drop_for!(i8);
impl_c_drop_for!(u8);
impl_c_drop_for!(i16);
impl_c_drop_for!(u16);
impl_c_drop_for!(i32);
impl_c_drop_for!(u32);
impl_c_drop_for!(i64);
impl_c_drop_for!(u64);
impl_c_drop_for!(f32);
impl_c_drop_for!(f64);
impl_c_drop_for!(bool);
impl_c_drop_for!(std::ffi::CString);

impl_c_repr_of_for!(usize);
impl_c_repr_of_for!(i8);
impl_c_repr_of_for!(u8);
impl_c_repr_of_for!(i16);
impl_c_repr_of_for!(u16);
impl_c_repr_of_for!(i32);
impl_c_repr_of_for!(u32);
impl_c_repr_of_for!(i64);
impl_c_repr_of_for!(u64);
impl_c_repr_of_for!(f32);
impl_c_repr_of_for!(f64);
impl_c_repr_of_for!(bool);

impl_c_repr_of_for!(usize, i32);

impl CReprOf<String> for std::ffi::CString {
    fn c_repr_of(input: String) -> Result<Self, CReprOfError> {
        Ok(std::ffi::CString::new(input)?)
    }
}

impl_as_rust_for!(usize);
impl_as_rust_for!(i8);
impl_as_rust_for!(u8);
impl_as_rust_for!(i16);
impl_as_rust_for!(u16);
impl_as_rust_for!(i32);
impl_as_rust_for!(u32);
impl_as_rust_for!(i64);
impl_as_rust_for!(u64);
impl_as_rust_for!(f32);
impl_as_rust_for!(f64);
impl_as_rust_for!(bool);

impl_as_rust_for!(i32, usize);

impl AsRust<String> for std::ffi::CStr {
    fn as_rust(&self) -> Result<String, AsRustError> {
        self.to_str().map(|s| s.to_owned()).map_err(|e| e.into())
    }
}

impl_rawpointerconverter_for!(usize);
impl_rawpointerconverter_for!(i16);
impl_rawpointerconverter_for!(u16);
impl_rawpointerconverter_for!(i32);
impl_rawpointerconverter_for!(u32);
impl_rawpointerconverter_for!(i64);
impl_rawpointerconverter_for!(u64);
impl_rawpointerconverter_for!(f32);
impl_rawpointerconverter_for!(f64);
impl_rawpointerconverter_for!(bool);

impl<U, T: CReprOf<U>, const N: usize> CReprOf<[U; N]> for [T; N]
where
    [T; N]: CDrop,
{
    fn c_repr_of(input: [U; N]) -> Result<[T; N], CReprOfError> {
        // TODO passing through a Vec here is a bit ugly, but as the conversion call may fail,
        // TODO we don't want tobe in the case where we're in the middle of the conversion of the
        // TODO array and we encounter an error, hence leaving the array partially uninitialised for
        // TODO rust to try to cleanup. the try_map unstable method on array would be nice here
        let result_vec: Result<Vec<T>, CReprOfError> =
            input.into_iter().map(T::c_repr_of).collect();
        let vec = result_vec?;

        assert_eq!(vec.len(), N);

        let mut result: [T; N] = unsafe { std::mem::zeroed() }; // we'll replace everything so "should" be good

        for (i, t) in vec.into_iter().enumerate() {
            result[i] = t;
        }

        Ok(result)
    }
}

impl<T: CDrop, const N: usize> CDrop for [T; N] {
    fn do_drop(&mut self) -> Result<(), CDropError> {
        let result: Result<Vec<()>, CDropError> = self.iter_mut().map(T::do_drop).collect();
        result?;
        Ok(())
    }
}

impl<U: AsRust<T>, T, const N: usize> AsRust<[T; N]> for [U; N] {
    fn as_rust(&self) -> Result<[T; N], AsRustError> {
        // TODO passing through a Vec here is a bit ugly, but as the conversion call may fail,
        // TODO we don't want tobe in the case where we're in the middle of the conversion of the
        // TODO array and we encounter an error, hence leaving the array partially uninitialised for
        // TODO rust to try to cleanup. the try_map unstable method on array would be nice here
        let result_vec: Result<Vec<T>, AsRustError> = self.iter().map(U::as_rust).collect();
        let vec = result_vec?;

        assert_eq!(vec.len(), N);

        let mut result: [T; N] = unsafe { std::mem::zeroed() }; // we'll replace everything so "should" be good

        for (i, t) in vec.into_iter().enumerate() {
            result[i] = t;
        }

        Ok(result)
    }
}