Skip to content

Allow using Unifex enums and structs in NIF/Cnode state#120

Merged
FelonEkonom merged 12 commits into
masterfrom
unifex-types-in-state
Jun 25, 2026
Merged

Allow using Unifex enums and structs in NIF/Cnode state#120
FelonEkonom merged 12 commits into
masterfrom
unifex-types-in-state

Conversation

@FelonEkonom

@FelonEkonom FelonEkonom commented May 20, 2026

Copy link
Copy Markdown
Member

Comment thread lib/unifex/interface_IO.ex Outdated

@spec_name_sufix ".spec.exs"
@generated_dir_name "_generated"
@types_header_sufix "_types_definitions"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
@types_header_sufix "_types_definitions"
@types_header_suffix "_types"

typo in suffix + I think _types is sufficient

@Noarkhh Noarkhh requested a review from mat-hek May 21, 2026 15:37

@mat-hek mat-hek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

FelonEkonom and others added 4 commits May 25, 2026 12:32
Collapse the two tie headers introduced in c2a9153 / 4bcbdc7 back into a
single `_generated/<name>.h` that includes both `<interface>/<name>.h`
and `<interface>/<name>_types.h`. Document the typedef-include-struct
pattern users follow to put custom types in their state.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@FelonEkonom FelonEkonom requested review from Noarkhh and mat-hek May 25, 2026 14:50

@Noarkhh Noarkhh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I found a bug where you now can't use user-defined enums in user-defined structs. It all stems from the casing - structs use snake case, and enums use camel case (we could discuss unifying that). Changes introduced in this PR make it so the following definitions in .spec.ex file:

type my_enum :: :a | :b 

type my_struct :: %MyStruct{enum_field: my_enum}

get translated to the following C definitions:

enum MyEnum_t { MY_ENUM_A, MY_ENUM_B };
typedef enum MyEnum_t MyEnum;

struct my_struct_t {
  my_enum enum_field;
};
typedef struct my_struct_t my_struct;

which don't work, since there's no type my_enum

Comment thread lib/unifex/code_generators/cnode.ex Outdated
"""
end

defp pargma_and_includes() do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
defp pargma_and_includes() do
defp pragma_and_includes() do

Comment thread lib/unifex/code_generators/nif.ex Outdated
~g"""
#ifdef #{generator.identification_constant()}
#include "#{generator.interface_io_name()}/#{name}.h"
#include "#{generator.interface_io_name()}/#{types_header_filename}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My LSP complains when this include is below the other one, since the types are used inside it. I also think it makes more sense, but it's not necessary

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@FelonEkonom FelonEkonom merged commit 860cbfa into master Jun 25, 2026
4 checks passed
@FelonEkonom FelonEkonom deleted the unifex-types-in-state branch June 25, 2026 10:41
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.

[Unifex] User-defined state can't use user-defined types

3 participants