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

Commit

Permalink
Implement RIType traits for u8 array with an arbitrary size (#13256)
Browse files Browse the repository at this point in the history
  • Loading branch information
conr2d authored Jan 30, 2023
1 parent fdff2b2 commit a9c4334
Showing 1 changed file with 48 additions and 67 deletions.
115 changes: 48 additions & 67 deletions primitives/runtime-interface/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,84 +276,65 @@ impl<T: 'static + Encode> IntoFFIValue for [T] {
}
}

/// Implement the traits for the `[u8; N]` arrays, where `N` is the input to this macro.
macro_rules! impl_traits_for_arrays {
(
$(
$n:expr
),*
$(,)?
) => {
$(
/// The type is passed as `u32`.
///
/// The `u32` is the pointer to the array.
impl RIType for [u8; $n] {
type FFIType = u32;
}

#[cfg(not(feature = "std"))]
impl IntoFFIValue for [u8; $n] {
type Owned = ();
/// The type is passed as `u32`.
///
/// The `u32` is the pointer to the array.
impl<const N: usize> RIType for [u8; N] {
type FFIType = u32;
}

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

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

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

res
}
}
res.copy_from_slice(&data);

#[cfg(feature = "std")]
impl FromFFIValue for [u8; $n] {
type SelfInstance = [u8; $n];
res
}
}

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

#[cfg(feature = "std")]
impl IntoFFIValue for [u8; $n] {
fn into_ffi_value(self, context: &mut dyn FunctionContext) -> Result<u32> {
let addr = context.allocate_memory($n)?;
context.write_memory(addr, &self)?;
Ok(addr.into())
}
}
fn from_ffi_value(context: &mut dyn FunctionContext, arg: u32) -> Result<[u8; N]> {
let mut res = [0u8; N];
context.read_memory_into(Pointer::new(arg), &mut res)?;
Ok(res)
}
}

#[cfg(feature = "std")]
impl IntoPreallocatedFFIValue for [u8; $n] {
type SelfInstance = [u8; $n];

fn into_preallocated_ffi_value(
self_instance: Self::SelfInstance,
context: &mut dyn FunctionContext,
allocated: u32,
) -> Result<()> {
context.write_memory(Pointer::new(allocated), &self_instance)
}
}
)*
#[cfg(feature = "std")]
impl<const N: usize> IntoFFIValue for [u8; N] {
fn into_ffi_value(self, context: &mut dyn FunctionContext) -> Result<u32> {
let addr = context.allocate_memory(N as u32)?;
context.write_memory(addr, &self)?;
Ok(addr.into())
}
}

impl_traits_for_arrays! {
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,
#[cfg(feature = "std")]
impl<const N: usize> IntoPreallocatedFFIValue for [u8; N] {
type SelfInstance = [u8; N];

fn into_preallocated_ffi_value(
self_instance: Self::SelfInstance,
context: &mut dyn FunctionContext,
allocated: u32,
) -> Result<()> {
context.write_memory(Pointer::new(allocated), &self_instance)
}
}

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

0 comments on commit a9c4334

Please sign in to comment.