swift_rs/
swift_ret.rs

1use crate::{swift::SwiftObject, *};
2use std::ffi::c_void;
3
4/// Identifies a type as being a valid return type from a Swift function.
5/// For types that are objects which need extra retains,
6/// the [`retain`](SwiftRet::retain) function will be re-implemented.
7pub trait SwiftRet {
8    /// Adds a retain to the value if possible
9    ///
10    /// # Safety
11    /// Just don't use this.
12    /// Let [`swift!`] handle it.
13    unsafe fn retain(&self) {}
14}
15
16macro_rules! primitive_impl {
17    ($($t:ty),+) => {
18        $(impl SwiftRet for $t {
19        })+
20    };
21}
22
23primitive_impl!(
24    Bool,
25    Int,
26    Int8,
27    Int16,
28    Int32,
29    Int64,
30    UInt,
31    UInt8,
32    UInt16,
33    UInt32,
34    UInt64,
35    Float32,
36    Float64,
37    *const c_void,
38    *mut c_void,
39    *const u8,
40    ()
41);
42
43impl<T: SwiftObject> SwiftRet for Option<T> {
44    unsafe fn retain(&self) {
45        if let Some(v) = self {
46            v.retain()
47        }
48    }
49}
50
51impl<T: SwiftObject> SwiftRet for T {
52    unsafe fn retain(&self) {
53        (*self).retain()
54    }
55}