For context: I am trying to write a Rust wrapper over a C library.
Like many C libraries, most of its functions return an int
. Positive return values are meaningful (provides information) and negative values are error codes.
To give an example, think of something like int get_items_from_record(const struct record *rec, struct item *items)
. A positive value indicates how many items were returned. -1
could mean ErrorA, -2
ErrorB, and so on.
Since this is Rust, I want to represent this kind of integer as Result<T, E>, e.g.:
enum LibError {
A = -1,
B = -2,
// ....
}
// LibResult is ideally just represented as an integer.
type LibResult = Result<NonNegativeInteger, LibError>;
// Then I can pass LibResult values back to the C code as i32 trivially.
Is there a way/crate to do this?
This is not possible, because Rust still stores a discriminant even when the enum values don't overlap.
As far as I can tell, the only situation where Rust doesn't store a discriminant is when either the
Ok
orErr
variant is zero-sized, and the other variant has a niche. So,Result<(), ErrorEnum>
can be represented as an integer, butResult
can not.You can still use enums, and implement simple conversions like this:
P.S. Sorry that the generics aren't displayed due to Lemmy's bad santiziation.
Discriminant is irrelevant and you're not supposed to fuck with it.
And there is zero reason to use unsafe/transmute for this.
And just to explicitly point out, your code's also better because of the use of the standard traits. It took me a while to get into the habit, but using what's already there is always a good idea.