If I want to pass a slice over C FFI, the proper way to do that would be to pass a pointer and a length, right? For example I've got a struct with these fields:
/// The definitions of the methods associated to to the [`method_pointers`] with the same index.
pub method_definitions: Vec<ScriptMethodDefinition>,
/// The methods associated to this component
pub method_pointers: Vec<extern "C" fn(*mut u8 /*ptr*/, usize /*len*/) -> *mut u8>,
The method_definitions
contain metadata about the method calls such supported by this scripted "Type" such as the method name, the type of the arguments and the type of the return value.
The actual pointers to these methods are stored in method_pointers
. I needed a singular definition for the method pionters that can facilitate any number of arguments of any type and one return type.
My idea was to logically take a slice of pointers as the argument value where each pointer in the slice would point to a different argument, and then return a pointer to the return value. The raw pointers, in addition to the ScriptMethodDefinition
data will be enough to know what data those pointers are pointing to so that we know how many bytes to read from each pointer and how to interpret that data.
The question then became how to pass the slice of arguments to C, because rustc warned me that C doesn't have any such thing as slices. I've seen this in some C snippets before where, I think, to pass a string it would pass a pointer and a length, which is essentially the same as a slice.
The question is, does this strategy make sense? To pass a pointer and a length, where that points to len
number of pointers the actual argument data?