There's a plenty of similar questins but I could not find a reasonable solution to my problem.
Ideally, I'd like to have a nice API like this:
let data = read_data::<Vec<f32>>()
Where read_data calls to ffi functions of different flavours, e.g
void getFloatAttributeData(..)
void getDoubleAttributeData(..)
void getIntAttributeData(..)
void getInt64AttributeData(..)
All these calls only differ by return type, I was hoping to utilize Rust generics somehow to make it nicer to use. I'm convinced that it's not possible, because it would require something like this:
fn read_data<T>(..) -> Vec<T> {
// branch based on type of T (Any::TypeID?) I think it's impossible.
}
I would be okay with adding an extra argument if it worked, but it doesn't either:
fn read_data<T>(ty: DataType) -> Vec<T> {
match ty {
Float => // call ffi::getFloatAttributeData(),
Int => //call ffi::getIntAttributeData()
}
}
let data = read_data::<f32>(DataType::Float)
The only solution I'm currently using is returning an enum with a payload for each data type. I know it's idiomatic, but I just wanted to hear is this the only way?