-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsurrogates.rs
34 lines (28 loc) · 957 Bytes
/
surrogates.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use interoptopus::patterns::surrogates::{CorrectSurrogate, Surrogate};
use interoptopus::{ffi_function, ffi_type};
// Let's assume we can't implement `CTypeInfo` for this.
mod foreign {
#[repr(C)]
pub struct SomeForeignType {
x: u32,
}
}
// Instead, we create a local copy of that type with matching fields.
#[ffi_type]
pub struct Local {
x: u32,
}
// This is really only a marker trait where you need to guarantee that `Local` is a valid surrogate
// for `SomeForeignType`. If you messed this up, you'd get UB.
unsafe impl CorrectSurrogate<foreign::SomeForeignType> for Local {}
// Here we create a nicer alias.
type SomeForeignType = Surrogate<foreign::SomeForeignType, Local>;
#[ffi_type]
pub struct Container {
// We can then use the `Surrogate` type in our interfaces. It wil
pub foreign: SomeForeignType,
}
#[ffi_function]
pub fn pattern_surrogates_1(s: SomeForeignType, c: &mut Container) {
c.foreign = s;
}