Skip to content

Idiom - Error Handling in FFI - with structured enums in Rust #404

Discussion options

You must be logged in to vote

I used cbindgen. It helped with initial code, which I manually adjusted to be close to the article's Rust code (Idiomatic Errors - Rust Design Patterns).

api.h

#include <inttypes.h>

typedef struct FileCorruptedInfo {
  uint32_t code;
} FileCorruptedInfo;

typedef struct IoErrorInfoDetails {
  uint32_t code;
} IoErrorInfoDetails;

typedef struct IoErrorInfo {
  struct IoErrorInfoDetails details;
} IoErrorInfo;

typedef enum DatabaseError_Tag {
  IsReadOnly,
  IoError,
  FileCorrupted,
} DatabaseError_Tag;

typedef struct DatabaseError {
  DatabaseError_Tag tag;
  union {
    struct IoErrorInfo io_error;
    struct FileCorruptedInfo file_corrupted;
  };
} DatabaseError;

char* db_error_des…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dmitri-mamrukov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant