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
use std::ffi::c_void;
use crate::{swift::SwiftObject, *};
/// Identifies a type as being a valid argument in a Swift function.
pub trait SwiftArg<'a> {
type ArgType;
/// Creates a swift-compatible version of the argument.
/// For primitives this just returns `self`,
/// but for [`SwiftObject`] types it wraps them in [`SwiftRef`].
///
/// This function is called within the [`swift!`] macro.
///
/// # Safety
///
/// Creating a [`SwiftRef`] is inherently unsafe,
/// but is reliable if using the [`swift!`] macro,
/// so it is not advised to call this function manually.
unsafe fn as_arg(&'a self) -> Self::ArgType;
}
macro_rules! primitive_impl {
($($t:ty),+) => {
$(impl<'a> SwiftArg<'a> for $t {
type ArgType = $t;
unsafe fn as_arg(&'a self) -> Self::ArgType {
*self
}
})+
};
}
primitive_impl!(
Bool,
Int,
Int8,
Int16,
Int32,
Int64,
UInt,
UInt8,
UInt16,
UInt32,
UInt64,
Float32,
Float64,
*const c_void,
*mut c_void,
*const u8,
()
);
macro_rules! ref_impl {
($($t:ident $(<$($gen:ident),+>)?),+) => {
$(impl<'a $($(, $gen: 'a),+)?> SwiftArg<'a> for $t$(<$($gen),+>)? {
type ArgType = SwiftRef<'a, $t$(<$($gen),+>)?>;
unsafe fn as_arg(&'a self) -> Self::ArgType {
self.swift_ref()
}
})+
};
}
ref_impl!(SRObject<T>, SRArray<T>, SRData, SRString);
impl<'a, T: SwiftArg<'a>> SwiftArg<'a> for &T {
type ArgType = T::ArgType;
unsafe fn as_arg(&'a self) -> Self::ArgType {
(*self).as_arg()
}
}