I'm trying to create a rust client for swapi.dev API that could be used in Android and IOS. (Kotlin & Swift)
I'm facing difficulties declaring FFI for the Swift. I understand that to send vector I need wrap it in a structure that would have pointer and usize.
What is the best way (without tons of boiler plate code) to transform a structure that contains a lot of nested vectors into "C" supported struct?
- Is there any possibility to set custom translation for vector in the cbindgen? (I have not found it in doc.)
- Any way to do it with code-generation?
For Kotlin I use rust_swig so I don't need to wrap it manually.
This is struct I need to send from Rust to Swift:
#[repr(C)]
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResponsePeople {
pub count: i64,
pub next: String,
pub results: Vec<People>,
}
#[repr(C)]
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct People {
pub name: String,
pub height: String,
pub mass: String,
#[serde(rename = "hair_color")]
pub hair_color: String,
#[serde(rename = "skin_color")]
pub skin_color: String,
#[serde(rename = "eye_color")]
pub eye_color: String,
#[serde(rename = "birth_year")]
pub birth_year: String,
pub gender: String,
pub homeworld: String,
pub films: Vec<String>,
pub species: Vec<String>,
pub vehicles: Vec<String>,
pub starships: Vec<String>,
pub created: String,
pub edited: String,
pub url: String,
}
What I did so far, for each DTO I created a separate native
DTO which I use to send in FFI. for e.g.:
#[repr(C)]
pub struct PeopleNativeWrapper {
array: *mut PeopleNative,
length: usize,
}
#[repr(C)]
pub struct PeopleNative {
pub name: *const c_char,
pub height: *const c_char,
pub mass: *const c_char,
}
But it is a lot of boiler plate code. I need not only wrap vec
but also to translate all types to C
compatible:
String nama
=> pub name: *const c_char,
;