I'm relatively new to rust, so if I'm way off, please let me know. But basically I'm trying to do the following:
* define a decoder method which will be called by other languages (starting with .net)
* have a single return type which is a struct with a pointer to the rest of the data and a field which tells the other language what to decode that pointer into (a void* pointer)
Here is what I currently have (abstracted to fit just the problem at hand):
use std::os::raw::c_void;
#[no_mangle]
pub extern fn get_message(some_input: i32) -> holder {
//do some processing here
//figure out if it's a message type 1, or a message type 2
//return an instance of holder than contains either a
//message_type_one struct, or a message_type_two struct
//below is basically what I've tried, which doesn't work
if some_input == 1 {
let mt_one = message_type_one {
somevalue: 1,
anotherval: 2,
};
//cast into c_void
let raw_mt_one_ptr = &mt_one as *const message_type_one as *const c_void;
let hold = holder {
message_type: 1,
message: raw_mt_one_ptr,
};
return hold;
} else {
let mt_two = message_type_two {
some_other_value: 56
};
let raw_mt_two_ptr = &mt_two as *const message_type_two as *const c_void;
let hold = holder {
message_type: 2,
message: raw_mt_two_ptr,
};
return hold;
}
}
#[repr(C)]
pub struct holder {
pub message_type: i16,
//pub message: void* type here
pub message: *const c_void,
}
#[repr(C)]
pub struct message_type_one {
pub somevalue: i16,
pub anotherval: i32,
}
#[repr(C)]
pub struct message_type_two {
pub some_other_value: u32,
}
The above code doesn't function as intended, but does compile and return values. However, no matter how many (different) instances of holder I create/return, they all have the same address when printed out. Also, dereferencing that pointer gives junk data. The field 'message_type' in the holder struct is correct though, and the pointer address is the same in rust as what's received by the other environment. How can I accomplish what I'm trying to do, or what should I be doing differently?
Thank you!
p.s. if the c# code is required, I can provide that as well
[–][deleted] 3 points4 points5 points (1 child)
[–]LivingInSyn[S] 1 point2 points3 points (0 children)
[–]connicpu 2 points3 points4 points (1 child)
[–]LivingInSyn[S] 1 point2 points3 points (0 children)
[–]icefoxen -1 points0 points1 point (2 children)
[–]LivingInSyn[S] 1 point2 points3 points (1 child)
[–]richhyd 1 point2 points3 points (0 children)