Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit a9c4334

Browse files
authored
Implement RIType traits for u8 array with an arbitrary size (#13256)
1 parent fdff2b2 commit a9c4334

File tree

1 file changed

+48
-67
lines changed
  • primitives/runtime-interface/src

1 file changed

+48
-67
lines changed

primitives/runtime-interface/src/impls.rs

+48-67
Original file line numberDiff line numberDiff line change
@@ -276,84 +276,65 @@ impl<T: 'static + Encode> IntoFFIValue for [T] {
276276
}
277277
}
278278

279-
/// Implement the traits for the `[u8; N]` arrays, where `N` is the input to this macro.
280-
macro_rules! impl_traits_for_arrays {
281-
(
282-
$(
283-
$n:expr
284-
),*
285-
$(,)?
286-
) => {
287-
$(
288-
/// The type is passed as `u32`.
289-
///
290-
/// The `u32` is the pointer to the array.
291-
impl RIType for [u8; $n] {
292-
type FFIType = u32;
293-
}
294-
295-
#[cfg(not(feature = "std"))]
296-
impl IntoFFIValue for [u8; $n] {
297-
type Owned = ();
279+
/// The type is passed as `u32`.
280+
///
281+
/// The `u32` is the pointer to the array.
282+
impl<const N: usize> RIType for [u8; N] {
283+
type FFIType = u32;
284+
}
298285

299-
fn into_ffi_value(&self) -> WrappedFFIValue<u32> {
300-
(self.as_ptr() as u32).into()
301-
}
302-
}
286+
#[cfg(not(feature = "std"))]
287+
impl<const N: usize> IntoFFIValue for [u8; N] {
288+
type Owned = ();
303289

304-
#[cfg(not(feature = "std"))]
305-
impl FromFFIValue for [u8; $n] {
306-
fn from_ffi_value(arg: u32) -> [u8; $n] {
307-
let mut res = [0u8; $n];
308-
let data = unsafe { Vec::from_raw_parts(arg as *mut u8, $n, $n) };
290+
fn into_ffi_value(&self) -> WrappedFFIValue<u32> {
291+
(self.as_ptr() as u32).into()
292+
}
293+
}
309294

310-
res.copy_from_slice(&data);
295+
#[cfg(not(feature = "std"))]
296+
impl<const N: usize> FromFFIValue for [u8; N] {
297+
fn from_ffi_value(arg: u32) -> [u8; N] {
298+
let mut res = [0u8; N];
299+
let data = unsafe { Vec::from_raw_parts(arg as *mut u8, N, N) };
311300

312-
res
313-
}
314-
}
301+
res.copy_from_slice(&data);
315302

316-
#[cfg(feature = "std")]
317-
impl FromFFIValue for [u8; $n] {
318-
type SelfInstance = [u8; $n];
303+
res
304+
}
305+
}
319306

320-
fn from_ffi_value(context: &mut dyn FunctionContext, arg: u32) -> Result<[u8; $n]> {
321-
let mut res = [0u8; $n];
322-
context.read_memory_into(Pointer::new(arg), &mut res)?;
323-
Ok(res)
324-
}
325-
}
307+
#[cfg(feature = "std")]
308+
impl<const N: usize> FromFFIValue for [u8; N] {
309+
type SelfInstance = [u8; N];
326310

327-
#[cfg(feature = "std")]
328-
impl IntoFFIValue for [u8; $n] {
329-
fn into_ffi_value(self, context: &mut dyn FunctionContext) -> Result<u32> {
330-
let addr = context.allocate_memory($n)?;
331-
context.write_memory(addr, &self)?;
332-
Ok(addr.into())
333-
}
334-
}
311+
fn from_ffi_value(context: &mut dyn FunctionContext, arg: u32) -> Result<[u8; N]> {
312+
let mut res = [0u8; N];
313+
context.read_memory_into(Pointer::new(arg), &mut res)?;
314+
Ok(res)
315+
}
316+
}
335317

336-
#[cfg(feature = "std")]
337-
impl IntoPreallocatedFFIValue for [u8; $n] {
338-
type SelfInstance = [u8; $n];
339-
340-
fn into_preallocated_ffi_value(
341-
self_instance: Self::SelfInstance,
342-
context: &mut dyn FunctionContext,
343-
allocated: u32,
344-
) -> Result<()> {
345-
context.write_memory(Pointer::new(allocated), &self_instance)
346-
}
347-
}
348-
)*
318+
#[cfg(feature = "std")]
319+
impl<const N: usize> IntoFFIValue for [u8; N] {
320+
fn into_ffi_value(self, context: &mut dyn FunctionContext) -> Result<u32> {
321+
let addr = context.allocate_memory(N as u32)?;
322+
context.write_memory(addr, &self)?;
323+
Ok(addr.into())
349324
}
350325
}
351326

352-
impl_traits_for_arrays! {
353-
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,
354-
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
355-
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
356-
75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96,
327+
#[cfg(feature = "std")]
328+
impl<const N: usize> IntoPreallocatedFFIValue for [u8; N] {
329+
type SelfInstance = [u8; N];
330+
331+
fn into_preallocated_ffi_value(
332+
self_instance: Self::SelfInstance,
333+
context: &mut dyn FunctionContext,
334+
allocated: u32,
335+
) -> Result<()> {
336+
context.write_memory(Pointer::new(allocated), &self_instance)
337+
}
357338
}
358339

359340
impl<T: codec::Codec, E: codec::Codec> PassBy for sp_std::result::Result<T, E> {

0 commit comments

Comments
 (0)