Skip to content

Commit

Permalink
[feature] #4126: add implementation for Box<str> FFI conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Veršić <[email protected]>
  • Loading branch information
mversic committed Jan 11, 2024
1 parent 300a031 commit ab90379
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 189 deletions.
2 changes: 1 addition & 1 deletion ffi/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ pub fn ffi_type_derive(input: TokenStream) -> TokenStream {
/// /* function implementation */
/// FfiReturn::Ok
/// }
/// extern "C" fn Foo__bar(handle: *const Foo, output: *mut SliceRef<u8>) -> FfiReturn {
/// extern "C" fn Foo__bar(handle: *const Foo, output: *mut RefSlice<u8>) -> FfiReturn {
/// /* function implementation */
/// FfiReturn::Ok
/// }
Expand Down
6 changes: 3 additions & 3 deletions ffi/derive/src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ fn gen_impl_ffi(name: &Ident, generics: &syn2::Generics) -> TokenStream {
type Ref<#lifetime> = &#lifetime iroha_ffi::Extern where #(#lifetime_bounded_where_clause),*;
type RefMut<#lifetime> = &#lifetime mut iroha_ffi::Extern where #(#lifetime_bounded_where_clause),*;
type Box = Box<iroha_ffi::Extern>;
type SliceBox = Box<[iroha_ffi::Extern]>;
type SliceRef<#lifetime> = &#lifetime [iroha_ffi::ir::Transparent] where #(#lifetime_bounded_where_clause),*;
type SliceRefMut<#lifetime> = &#lifetime mut [iroha_ffi::ir::Transparent] where #(#lifetime_bounded_where_clause),*;
type BoxedSlice = Box<[iroha_ffi::Extern]>;
type RefSlice<#lifetime> = &#lifetime [iroha_ffi::ir::Transparent] where #(#lifetime_bounded_where_clause),*;
type RefMutSlice<#lifetime> = &#lifetime mut [iroha_ffi::ir::Transparent] where #(#lifetime_bounded_where_clause),*;
type Vec = Vec<iroha_ffi::ir::Transparent>;
type Arr<const N: usize> = iroha_ffi::ir::Transparent;
}
Expand Down
52 changes: 26 additions & 26 deletions ffi/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ pub trait IrTypeFamily {
type RefMut<'itm>
where
Self: 'itm;
/// [`Ir`] type that [`Box<T>`] is mapped into
/// [`Ir`] type that [`Box<T>`] is mapped into for any `T: Sized`
type Box;
/// [`Ir`] type that `Box<[T]>` is mapped into
type SliceBox;
type BoxedSlice;
/// [`Ir`] type that `&[T]` is mapped into
type SliceRef<'itm>
type RefSlice<'itm>
where
Self: 'itm;
/// [`Ir`] type that `&mut [T]` is mapped into
type SliceRefMut<'itm>
type RefMutSlice<'itm>
where
Self: 'itm;
/// [`Ir`] type that [`Vec<T>`] is mapped into
Expand All @@ -126,60 +126,60 @@ impl<R: Cloned> IrTypeFamily for R {
// NOTE: Unused
type RefMut<'itm> = () where Self: 'itm;
type Box = Box<Self>;
type SliceBox = Box<[Self]>;
type SliceRef<'itm> = &'itm [Self] where Self: 'itm;
type BoxedSlice = Box<[Self]>;
type RefSlice<'itm> = &'itm [Self] where Self: 'itm;
// NOTE: Unused
type SliceRefMut<'itm> = () where Self: 'itm;
type RefMutSlice<'itm> = () where Self: 'itm;
type Vec = Vec<Self>;
type Arr<const N: usize> = [Self; N];
}
impl IrTypeFamily for Robust {
type Ref<'itm> = Transparent;
type RefMut<'itm> = Transparent;
type Box = Box<Self>;
type SliceBox = Box<[Self]>;
type SliceRef<'itm> = &'itm [Self];
type SliceRefMut<'itm> = &'itm mut [Self];
type BoxedSlice = Box<[Self]>;
type RefSlice<'itm> = &'itm [Self];
type RefMutSlice<'itm> = &'itm mut [Self];
type Vec = Vec<Self>;
type Arr<const N: usize> = Self;
}
impl IrTypeFamily for Opaque {
type Ref<'itm> = Transparent;
type RefMut<'itm> = Transparent;
type Box = Box<Self>;
type SliceBox = Box<[Self]>;
type SliceRef<'itm> = &'itm [Self];
type SliceRefMut<'itm> = &'itm mut [Self];
type BoxedSlice = Box<[Self]>;
type RefSlice<'itm> = &'itm [Self];
type RefMutSlice<'itm> = &'itm mut [Self];
type Vec = Vec<Self>;
type Arr<const N: usize> = [Self; N];
}
impl IrTypeFamily for Transparent {
type Ref<'itm> = Self;
type RefMut<'itm> = Self;
type Box = Box<Self>;
type SliceBox = Box<[Self]>;
type SliceRef<'itm> = &'itm [Self];
type SliceRefMut<'itm> = &'itm mut [Self];
type BoxedSlice = Box<[Self]>;
type RefSlice<'itm> = &'itm [Self];
type RefMutSlice<'itm> = &'itm mut [Self];
type Vec = Vec<Self>;
type Arr<const N: usize> = Self;
}
impl IrTypeFamily for &Extern {
type Ref<'itm> = &'itm Self where Self: 'itm;
type RefMut<'itm> = &'itm mut Self where Self: 'itm;
type Box = Box<Self>;
type SliceBox = Box<[Self]>;
type SliceRef<'itm> = &'itm [Self] where Self: 'itm;
type SliceRefMut<'itm> = &'itm mut [Self] where Self: 'itm;
type BoxedSlice = Box<[Self]>;
type RefSlice<'itm> = &'itm [Self] where Self: 'itm;
type RefMutSlice<'itm> = &'itm mut [Self] where Self: 'itm;
type Vec = Vec<Self>;
type Arr<const N: usize> = [Self; N];
}
impl IrTypeFamily for &mut Extern {
type Ref<'itm> = &'itm Self where Self: 'itm;
type RefMut<'itm> = &'itm mut Self where Self: 'itm;
type Box = Box<Self>;
type SliceBox = Box<[Self]>;
type SliceRef<'itm> = &'itm [Self] where Self: 'itm;
type SliceRefMut<'itm> = &'itm mut [Self] where Self: 'itm;
type BoxedSlice = Box<[Self]>;
type RefSlice<'itm> = &'itm [Self] where Self: 'itm;
type RefMutSlice<'itm> = &'itm mut [Self] where Self: 'itm;
type Vec = Vec<Self>;
type Arr<const N: usize> = [Self; N];
}
Expand Down Expand Up @@ -221,27 +221,27 @@ impl<R: Ir> Ir for Box<[R]>
where
R::Type: IrTypeFamily,
{
type Type = <R::Type as IrTypeFamily>::SliceBox;
type Type = <R::Type as IrTypeFamily>::BoxedSlice;
}
impl<'itm, R: Ir> Ir for &'itm [R]
where
R::Type: IrTypeFamily,
{
type Type = <R::Type as IrTypeFamily>::SliceRef<'itm>;
type Type = <R::Type as IrTypeFamily>::RefSlice<'itm>;
}
#[cfg(feature = "non_robust_ref_mut")]
impl<'itm, R: Ir> Ir for &'itm mut [R]
where
R::Type: IrTypeFamily,
{
type Type = <R::Type as IrTypeFamily>::SliceRefMut<'itm>;
type Type = <R::Type as IrTypeFamily>::RefMutSlice<'itm>;
}
#[cfg(not(feature = "non_robust_ref_mut"))]
impl<'itm, R: Ir + InfallibleTransmute> Ir for &'itm mut [R]
where
R::Type: IrTypeFamily,
{
type Type = <R::Type as IrTypeFamily>::SliceRefMut<'itm>;
type Type = <R::Type as IrTypeFamily>::RefMutSlice<'itm>;
}
impl<R: Ir> Ir for Vec<R>
where
Expand Down
2 changes: 1 addition & 1 deletion ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub struct LocalRef<'data, R>(R, core::marker::PhantomData<&'data ()>);
/// ```
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct LocalSlice<'data, R>(alloc::vec::Vec<R>, core::marker::PhantomData<&'data ()>);
pub struct LocalSlice<'data, R>(alloc::boxed::Box<[R]>, core::marker::PhantomData<&'data ()>);

/// Result of execution of an FFI function
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq)]
Expand Down
4 changes: 2 additions & 2 deletions ffi/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ mod wasm {
type Ref<'itm> = Transparent;
type RefMut<'itm> = Transparent;
type Box = Box<Robust>;
type SliceRef<'itm> = &'itm [Robust];
type SliceRefMut<'itm> = &'itm mut [Robust];
type RefSlice<'itm> = &'itm [Robust];
type RefMutSlice<'itm> = &'itm mut [Robust];
type Vec = Vec<Robust>;
type Arr<const N: usize> = Robust;
}
Expand Down
Loading

0 comments on commit ab90379

Please sign in to comment.