subsoil/runtime_interface/
impls.rs1#[cfg(not(substrate_runtime))]
11use super::host::*;
12#[cfg(substrate_runtime)]
13use super::wasm::*;
14use super::{Pointer, RIType};
15
16#[cfg(not(substrate_runtime))]
17use crate::wasm_interface::{FunctionContext, Result};
18
19#[cfg(all(substrate_runtime, not(feature = "disable_target_static_assertions")))]
21const _: () = {
22 assert!(core::mem::size_of::<usize>() == core::mem::size_of::<u32>());
23 assert!(core::mem::size_of::<*const u8>() == core::mem::size_of::<u32>());
24};
25
26macro_rules! impl_traits_for_primitives {
28 (
29 $(
30 $rty:ty, $fty:ty,
31 )*
32 ) => {
33 $(
34 impl RIType for $rty {
36 type FFIType = $fty;
37 type Inner = Self;
38 }
39
40 #[cfg(substrate_runtime)]
41 impl IntoFFIValue for $rty {
42 type Destructor = ();
43
44 fn into_ffi_value(value: &mut $rty) -> (Self::FFIType, Self::Destructor) {
45 (*value as $fty, ())
46 }
47 }
48
49 #[cfg(substrate_runtime)]
50 impl FromFFIValue for $rty {
51 fn from_ffi_value(arg: $fty) -> $rty {
52 arg as $rty
53 }
54 }
55
56 #[cfg(not(substrate_runtime))]
57 impl<'a> FromFFIValue<'a> for $rty {
58 type Owned = Self;
59
60 fn from_ffi_value(_: &mut dyn FunctionContext, arg: $fty) -> Result<$rty> {
61 Ok(arg as $rty)
62 }
63
64 fn take_from_owned(owned: &'a mut Self::Owned) -> Self::Inner {
65 *owned
66 }
67 }
68
69 #[cfg(not(substrate_runtime))]
70 impl IntoFFIValue for $rty {
71 fn into_ffi_value(value: Self::Inner, _: &mut dyn FunctionContext) -> Result<$fty> {
72 Ok(value as $fty)
73 }
74 }
75 )*
76 }
77}
78
79impl_traits_for_primitives! {
80 u8, u32,
81 u16, u32,
82 u32, u32,
83 u64, u64,
84 i8, i32,
85 i16, i32,
86 i32, i32,
87 i64, i64,
88}
89
90impl RIType for bool {
95 type FFIType = u32;
96 type Inner = Self;
97}
98
99#[cfg(substrate_runtime)]
100impl IntoFFIValue for bool {
101 type Destructor = ();
102
103 fn into_ffi_value(value: &mut bool) -> (Self::FFIType, Self::Destructor) {
104 (if *value { 1 } else { 0 }, ())
105 }
106}
107
108#[cfg(substrate_runtime)]
109impl FromFFIValue for bool {
110 fn from_ffi_value(arg: u32) -> bool {
111 arg == 1
112 }
113}
114
115#[cfg(not(substrate_runtime))]
116impl<'a> FromFFIValue<'a> for bool {
117 type Owned = Self;
118
119 fn from_ffi_value(_: &mut dyn FunctionContext, arg: u32) -> Result<bool> {
120 Ok(arg == 1)
121 }
122
123 fn take_from_owned(owned: &'a mut Self::Owned) -> Self::Inner {
124 *owned
125 }
126}
127
128#[cfg(not(substrate_runtime))]
129impl IntoFFIValue for bool {
130 fn into_ffi_value(value: Self, _: &mut dyn FunctionContext) -> Result<u32> {
131 Ok(if value { 1 } else { 0 })
132 }
133}
134
135#[cfg(not(substrate_runtime))]
136impl<T: crate::wasm_interface::PointerType> RIType for Pointer<T> {
137 type FFIType = u32;
138 type Inner = Self;
139}
140
141#[cfg(substrate_runtime)]
143impl<T> RIType for Pointer<T> {
144 type FFIType = u32;
145 type Inner = Self;
146}
147
148#[cfg(substrate_runtime)]
149impl<T> IntoFFIValue for Pointer<T> {
150 type Destructor = ();
151
152 fn into_ffi_value(value: &mut Pointer<T>) -> (Self::FFIType, Self::Destructor) {
153 (*value as u32, ())
154 }
155}
156
157#[cfg(substrate_runtime)]
158impl<T> FromFFIValue for Pointer<T> {
159 fn from_ffi_value(arg: u32) -> Self {
160 arg as _
161 }
162}
163
164#[cfg(not(substrate_runtime))]
165impl<'a, T: crate::wasm_interface::PointerType> FromFFIValue<'a> for Pointer<T> {
166 type Owned = Self;
167
168 fn from_ffi_value(_: &mut dyn FunctionContext, arg: u32) -> Result<Self> {
169 Ok(Pointer::new(arg))
170 }
171
172 fn take_from_owned(owned: &'a mut Self::Owned) -> Self::Inner {
173 *owned
174 }
175}
176
177#[cfg(not(substrate_runtime))]
178impl<T: crate::wasm_interface::PointerType> IntoFFIValue for Pointer<T> {
179 fn into_ffi_value(value: Self, _: &mut dyn FunctionContext) -> Result<u32> {
180 Ok(value.into())
181 }
182}