Skip to content

[feature] #2491: Enum support in FFi#2625

Merged
mversic merged 1 commit intohyperledger-iroha:iroha2-devfrom
mversic:ffi_enum
Sep 29, 2022
Merged

[feature] #2491: Enum support in FFi#2625
mversic merged 1 commit intohyperledger-iroha:iroha2-devfrom
mversic:ffi_enum

Conversation

@mversic
Copy link
Copy Markdown
Contributor

@mversic mversic commented Aug 15, 2022

Description of the Change

  • removed wasm feature flag for target_type. After some discussion with @Erigara this was decided to be preferable
  • replaced IntoFfi and TryFromReprC with FfiType and FfiConvert traits. Previous separation couldn't be worked with mostly due to conflicting requirements on lifetimes on data carrying enums. Maybe GATs would have been of help here, not sure anymore. If needed FfiConvert can be split into TryFromFfi/IntoFfi
  • removed lifetimes from all ReprC types (namely SliceRef/SliceMut). It was too noisy and difficult to deal with but I think it can (should?) be reintroduced now
  • added non_robust_ref_mut feature flag. Might be useful to disallow mutation for non-robust types
  • improved conversion system so that it can now convert any recursive type
  • added implementation for data-carrying enums
  • prevent double-free in function return via NonLocal

Type challenges

There is no specialization or HKTs in Rust, so it's very difficult to properly delineate type categories. But it can be emulated in the type system. This is what the Ir trait is for. Rust type is first wrapped in the correct Ir type after which static dispatch on IR marker types provides an optimal(zero-copy when possible) implementation of FFIType.

Issue

Closes #2491 #2629

Benefits

optimal conversion, enums support

Possible Drawbacks

I'm not sure if it's more complex than before of just a different perspective

Usage Examples or Tests [optional]

This is what a conversion for data-carrying enum would produce (it is not even required it is repr(C):

pub enum DataCarryingEnum {
    A(String);
    B
}
pub struct ReprCDataCarryingEnum {
    tag: u8,
    payload: DataCarryingEnumPayload,
}
union DataCarryingEnumPayload {
    A: ManuallyDrop<<String as FfiType>::ReprC>,
    B: ()
}

There was a suggestion to use a different enum representation. It can be modified. At the moment I found this easier to implement

Alternate Designs [optional]

I've tried some but all failed in expresivity

@github-actions github-actions Bot added the iroha2-dev The re-implementation of a BFT hyperledger in RUST label Aug 15, 2022
Comment thread ffi/src/primitives.rs Outdated
Comment thread ffi/tests/ffi_export.rs Outdated
@Erigara Erigara self-assigned this Aug 16, 2022
Comment thread ffi/src/primitives.rs Outdated
Comment thread ffi/src/primitives.rs Outdated
Comment thread ffi/src/lib.rs Outdated
Comment thread ffi/tests/ffi_export.rs
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread data_model/src/events/data/filters.rs Outdated
Comment thread data_model/src/events/data/filters.rs Outdated
Comment thread data_model/src/peer.rs Outdated
Comment thread ffi/derive/src/convert.rs
Comment thread ffi/src/ir.rs Outdated
Comment thread ffi/src/ir.rs Outdated
Comment thread ffi/src/repr_c.rs Outdated
Comment thread ffi/src/repr_c.rs Outdated
Comment thread ffi/src/primitives.rs Outdated
Comment thread ffi/src/primitives.rs Outdated
Comment thread ffi/src/primitives.rs Outdated
Comment thread ffi/src/ir.rs Outdated
Comment thread ffi/derive/src/lib.rs
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/src/ir.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread crypto/Cargo.toml
Comment thread data_model/Cargo.toml
Comment thread ffi/src/ir.rs
Comment thread ffi/src/ir.rs Outdated
Comment thread ffi/src/ir.rs Outdated
Comment thread ffi/src/repr_c.rs Outdated
Comment thread ffi/src/repr_c.rs
Comment thread ffi/src/repr_c.rs Outdated
Comment thread ffi/derive/src/convert.rs
Comment thread ffi/derive/src/convert.rs Outdated
Erigara
Erigara previously approved these changes Sep 27, 2022
Copy link
Copy Markdown
Contributor

@Erigara Erigara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Comment thread data_model/src/asset.rs
Comment thread data_model/src/peer.rs Outdated
Comment thread data_model/src/predicate.rs Outdated
Comment thread ffi/Cargo.toml Outdated
Comment thread ffi/Cargo.toml Outdated
Comment thread macro/derive/tests/ui_pass/variant_count.rs
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs
Comment thread ffi/derive/src/convert.rs Outdated
Comment thread ffi/derive/src/convert.rs
Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

add support for data-carrying enums

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

refactor ffi library implementation

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

improve wasm types FFI serialization

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

introduce Opaque and out-pointers in IR

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

introduce Transparent in IR, use it for fieldless enums

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

remove Transmute in favor of Transparent

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

Extend IR, make generic impls for slices and vectors

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

add implementation for data-carrying enum

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

separate FfiType and FfiConvert traits

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

implement support for arrays

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

prevent use-after-free in FFI method return

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>

fix review comments

Signed-off-by: Marin Veršić <marin.versic101@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

iroha2-dev The re-implementation of a BFT hyperledger in RUST

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants