error[E0782]: trait objects must include the dyn
keyword
#82
Chaoses-Ib
started this conversation in
General
Replies: 1 comment 1 reply
-
The error message could be better, but if you compare here, you'll also need your own use interoptopus::{ffi_type, ffi_service, ffi_service_ctor};
use std::ffi::CString;
use std::fmt::{Display, Formatter};
#[ffi_type(patterns(ffi_error))]
#[repr(C)]
pub enum FFIError {
Ok = 0,
Null = 100,
Panic = 200,
Fail = 300,
}
// This is the error type you use in a Rust library. Again, you are almost
// entirely free how you want to implement it.
#[derive(Debug)]
pub enum Error {
Bad,
}
// Provide a mapping how your Rust error enums translate
// to your FFI error enums.
impl From<Error> for FFIError {
fn from(x: Error) -> Self {
match x {
Error::Bad => Self::Fail,
}
}
}
// Implement Default so we know what the "good" case is.
impl Default for FFIError {
fn default() -> Self {
Self::Ok
}
}
// Implement Interoptopus' `FFIError` trait for your FFIError enum.
// Here you must map 3 "well known" variants to your enum.
impl interoptopus::patterns::result::FFIError for FFIError {
const SUCCESS: Self = Self::Ok;
const NULL: Self = Self::Null;
const PANIC: Self = Self::Panic;
}
// Lazy "Display" implementation so your error can be logged.
impl Display for Error {
fn fmt(&self, _: &mut Formatter<'_>) -> std::fmt::Result {
Ok(())
}
}
// Tell Rust your error type is an actual Rust Error.
impl std::error::Error for Error {}
/// Some struct we want to expose as a class.
#[ffi_type(opaque)]
#[derive(Default)]
pub struct SimpleService {
pub some_value: u32,
pub c_string: CString,
pub data: Vec<u32>,
}
// Regular implementation of methods.
#[ffi_service(error = "FFIError", prefix = "simple_service_")]
impl SimpleService {
/// The constructor must return a `Result<Self, Error>`.
#[ffi_service_ctor]
pub fn new_with(some_value: u32) -> Result<Self, Error> {
Ok(Self {
some_value,
c_string: CString::new("Hello new_with").unwrap(),
data: vec![some_value; some_value as usize],
})
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Source code:
Build log:
And if I add
dyn
before#[ffi_service_ctor]
, I will get another error:Did I miss something?
Beta Was this translation helpful? Give feedback.
All reactions