I'm running into a problem creating a generic wrapper around an FFI
interface of the following form:

  fn ffi_process(data: *c_void, count: c_int, type: *c_void);

where `data` points to an array of `count` elements of each of type
`type`. Each `type` is represented by a constant opaque pointer to
some fixed symbol exported by the C library and corresponding to a
particular type (e.g. DT_INT32, DT_FLOAT32, etc...).

My approach so far has been to use a trait to map Rust data types to
data types in the library as follows:

trait MySendable {
  fn datatype() -> *c_void;
}

impl MySendable for int {
  fn datatype() -> *c_void { DT_INT32 }
}

fn process<T: MySendable>(data: &[T]) {
  unsafe {
    ffi_process(data.as_ptr(), data.len(), T::datatype());
  }
}

But, Rust does not like this at all. I have seen many many references
on github issues and SO about methods on the types, but it isn't clear
what the current / future approach is / will be.

Is this (1) a reasonable approach to the problem (even if not
currently possible), and (2) are there other ways to bend the type
system to my needs here?

Thanks!
Noah
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to