Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 29, 2022

Conversation

mversic
Copy link
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
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/tests/ffi_export.rs Outdated Show resolved Hide resolved
@Erigara Erigara self-assigned this Aug 16, 2022
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/lib.rs Outdated Show resolved Hide resolved
ffi/tests/ffi_export.rs Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
data_model/src/events/data/filters.rs Outdated Show resolved Hide resolved
data_model/src/events/data/filters.rs Outdated Show resolved Hide resolved
data_model/src/peer.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Show resolved Hide resolved
ffi/src/ir.rs Outdated Show resolved Hide resolved
ffi/src/ir.rs Outdated Show resolved Hide resolved
ffi/src/repr_c.rs Outdated Show resolved Hide resolved
ffi/src/repr_c.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/primitives.rs Outdated Show resolved Hide resolved
ffi/src/ir.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/src/ir.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
Erigara
Erigara previously approved these changes Sep 27, 2022
Copy link
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

data_model/src/asset.rs Show resolved Hide resolved
data_model/src/peer.rs Outdated Show resolved Hide resolved
data_model/src/predicate.rs Outdated Show resolved Hide resolved
ffi/Cargo.toml Outdated Show resolved Hide resolved
ffi/Cargo.toml Outdated Show resolved Hide resolved
macro/derive/tests/ui_pass/variant_count.rs Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Show resolved Hide resolved
ffi/derive/src/convert.rs Outdated Show resolved Hide resolved
ffi/derive/src/convert.rs Show resolved Hide resolved
Signed-off-by: Marin Veršić <[email protected]>

add support for data-carrying enums

Signed-off-by: Marin Veršić <[email protected]>

refactor ffi library implementation

Signed-off-by: Marin Veršić <[email protected]>

improve wasm types FFI serialization

Signed-off-by: Marin Veršić <[email protected]>

introduce Opaque and out-pointers in IR

Signed-off-by: Marin Veršić <[email protected]>

introduce Transparent in IR, use it for fieldless enums

Signed-off-by: Marin Veršić <[email protected]>

remove Transmute in favor of Transparent

Signed-off-by: Marin Veršić <[email protected]>

Extend IR, make generic impls for slices and vectors

Signed-off-by: Marin Veršić <[email protected]>

add implementation for data-carrying enum

Signed-off-by: Marin Veršić <[email protected]>

separate FfiType and FfiConvert traits

Signed-off-by: Marin Veršić <[email protected]>

implement support for arrays

Signed-off-by: Marin Veršić <[email protected]>

prevent use-after-free in FFI method return

Signed-off-by: Marin Veršić <[email protected]>

fix review comments

Signed-off-by: Marin Veršić <[email protected]>
@mversic mversic merged commit a0097e1 into hyperledger-iroha:iroha2-dev Sep 29, 2022
@mversic mversic deleted the ffi_enum branch September 29, 2022 07:52
mversic added a commit that referenced this pull request Sep 29, 2022
appetrosyan pushed a commit that referenced this pull request Oct 7, 2022
appetrosyan pushed a commit that referenced this pull request Oct 7, 2022
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