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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::{convert::TryInto, os::raw::c_void};
use array_init::array_init;
use widestring::U16String;
pub trait MessageT: Default + Send + Sync {
type Raw: FFIToRust<Target = Self> + Default + Drop + Send + Sync;
type RawRef: FFIFromRust<From = Self>;
fn type_support() -> *const c_void;
unsafe fn from_raw(from: &Self::Raw) -> Self {
from.to_rust()
}
unsafe fn to_raw_ref(&self) -> Self::RawRef {
Self::RawRef::from_rust(self)
}
}
pub trait ServiceT: Send {
type Request: MessageT;
type Response: MessageT;
fn type_support() -> *const c_void;
}
pub trait ActionT: Send {
type Goal: MessageT;
type Result: MessageT;
type Feedback: MessageT;
type SendGoal: ServiceT;
type GetResult: ServiceT;
type FeedbackMessage: MessageT;
fn type_support() -> *const c_void;
}
pub trait InternalDefault {
fn _default() -> Self;
}
impl<T> InternalDefault for Vec<T> {
fn _default() -> Self {
Self::new()
}
}
impl<T, const N: usize> InternalDefault for [T; N]
where
T: InternalDefault + std::fmt::Debug,
{
fn _default() -> Self {
array_init(|_| InternalDefault::_default())
}
}
macro_rules! impl_trait {
($type: ty) => {
impl InternalDefault for $type {
fn _default() -> Self {
Self::default()
}
}
};
}
impl_trait!(i8);
impl_trait!(i16);
impl_trait!(i32);
impl_trait!(i64);
impl_trait!(u8);
impl_trait!(u16);
impl_trait!(u32);
impl_trait!(u64);
impl_trait!(f32);
impl_trait!(f64);
impl_trait!(bool);
impl_trait!(String);
impl_trait!(U16String);
pub trait FFIToRust {
type Target;
unsafe fn to_rust(&self) -> Self::Target;
}
impl<T, const N: usize> FFIToRust for [T; N]
where
T: FFIToRust,
T::Target: std::fmt::Debug,
{
type Target = [T::Target; N];
unsafe fn to_rust(&self) -> <Self as FFIToRust>::Target {
self.iter()
.map(|v| v.to_rust())
.collect::<Vec<_>>()
.try_into()
.unwrap()
}
}
pub trait FFIFromRust {
type From;
unsafe fn from_rust(from: &Self::From) -> Self;
}
impl<T, const N: usize> FFIFromRust for [T; N]
where
T: FFIFromRust + std::fmt::Debug,
{
type From = [T::From; N];
unsafe fn from_rust(from: &Self::From) -> Self {
from.iter()
.map(|v| FFIFromRust::from_rust(v))
.collect::<Vec<_>>()
.try_into()
.unwrap()
}
}