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 #2624

Closed
wants to merge 864 commits into from

Conversation

mversic
Copy link
Contributor

@mversic mversic commented Aug 15, 2022

Description of the Change

Add support for converting data-carrying enums to and from FFI types. FFI equivalents for enums are not opaque, rather they are structs generated with FFI representation. Take the following enum as an example:

#[derive(IntoFfi, TryFromReprC)]
pub enum Enumeration {
    A(u32),
    B(PublicKey),
    C,
}

for IntoFfi will generate:

impl IntoFfi for Enumeration {
    type Target = __iroha_ffi__ReprCEnumerationIntoFfiTarget;
    
    fn into_ffi(self) -> Self::Target {
        ///
    }
}

unsafe impl ReprC for __iroha_ffi__ReprCEnumerationIntoFfiTarget {}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct __iroha_ffi__ReprCEnumerationIntoFfiTarget {
    tag: u8,
    payload: __iroha_ffi__ReprCEnumerationIntoFfiTargetPayload,
}

#[repr(C)]
union __iroha_ffi__ReprCEnumerationIntoFfiTargetPayload {
    A: <u32 as IntoFfi>::Target,
    B: <PublicKey as IntoFfi>::Target,
}

for TryFromReprC will generate:

impl<'itm> TryFromReprC<'itm> for Enumeration {
    type Source = __iroha_ffi__ReprCEnumerationTryFromReprCSource<'itm>;
    type Store = (<u32 as TryFromReprC<'itm>>::Source, <PublicKey as TryFromReprC<'itm>>::Source);

    unsafe fn try_from_repr_c(source: Self::Source, store: &'itm mut Self::Store) -> Result<Self> {
        ///
    }
}

unsafe impl ReprC for __iroha_ffi__ReprCEnumerationIntoFfiSource {}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct __iroha_ffi__ReprCEnumerationTryFromReprCSource<'itm> {
    tag: u8,
    payload: __iroha_ffi__ReprCEnumerationTryFromReprCSourcePayload<'itm>,
}

#[repr(C)]
#[derive(Clone, Copy)]
union __iroha_ffi__ReprCEnumerationTryFromReprCSourcePayload<'itm> {
    A: <u32 as TryFromReprC<'itm>>::Source,
    B: <PublicKey as TryFromReprC<'itm>>::Source,
}

Issue

Closes #2622

Benefits

Data-carrying enums can be used in FFI functions

Possible Drawbacks

Usage Examples or Tests [optional]

Alternate Designs [optional]

SamHSmith and others added 30 commits March 31, 2022 07:50
Signed-off-by: BAStos525 <[email protected]>
Signed-off-by: Aleksandr Petrosyan <[email protected]>
Signed-off-by: Aleksandr Petrosyan <[email protected]>
Signed-off-by: Daniil Polyakov <[email protected]>
mversic and others added 28 commits July 22, 2022 18:20
…os for Identifiable, Eq, Hash, Ord (hyperledger-iroha#2476)

* [feature] hyperledger-iroha#1988, hyperledger-iroha#2437: Derive macros for Identifiable, Eq, Hash, Ord

Signed-off-by: Ilia Churin <[email protected]>

* Refactor `OrdEqHash` into `Identifiable`

Signed-off-by: Ilia Churin <[email protected]>

* Refactor `IdOrdEqHash` and `Filter` into separate files

Signed-off-by: Ilia Churin <[email protected]>
…dger-iroha#2516)

* [feature] hyperledger-iroha#1891: Validate permission for event triggers

Signed-off-by: Ales Tsurko <[email protected]>

* [feature] hyperledger-iroha#1891: Update tests

Signed-off-by: Ales Tsurko <[email protected]>
Co-authored-by: Ekaterina Mekhnetsova <[email protected]>
Signed-off-by: Aleksandr Petrosyan <[email protected]>
…ng functions (hyperledger-iroha#2538)

* [feature] hyperledger-iroha#2490: Implement ffi_export attribute for freestanding functions

Signed-off-by: Ales Tsurko <[email protected]>

* [feature] hyperledger-iroha#2490: Update gen_ffi_fn to handle free-standing functions

Signed-off-by: Ales Tsurko <[email protected]>

* [feature] hyperledger-iroha#2490: Update tests

Signed-off-by: Ales Tsurko <[email protected]>
Signed-off-by: Aleksandr Petrosyan <[email protected]>
Signed-off-by: Aleksandr Petrosyan <[email protected]>
Signed-off-by: Aleksandr Petrosyan <[email protected]>
Signed-off-by: BAStos525 [email protected]
Co-authored-by: Ekaterina Mekhnetsova <[email protected]>
Co-authored-by: 6r1d <[email protected]>
Signed-off-by: BAStos525 <[email protected]>
… with definition (hyperledger-iroha#2517)

* [refactor] hyperledger-iroha#2052: implement PermissionTokenDefinition

Signed-off-by: Artemii Gerasimovich <[email protected]>
@mversic
Copy link
Contributor Author

mversic commented Aug 15, 2022

closed in favor of #2625 because it has the wrong target branch

@mversic mversic closed this Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

u128/i128 support in FFI