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

[Question] Replacing enums #1267

Closed
lnicola opened this issue Mar 4, 2018 · 3 comments
Closed

[Question] Replacing enums #1267

lnicola opened this issue Mar 4, 2018 · 3 comments
Assignees

Comments

@lnicola
Copy link
Member

lnicola commented Mar 4, 2018

I'm trying to produce bindings that support multiple versions of a library. It generally went smoothly, but I hit a snag. The library used to have this definition:

typedef int OGRErr;

#define OGRERR_NONE                0
#define OGRERR_NOT_ENOUGH_DATA     1    /* not enough data to deserialize */
#define OGRERR_NOT_ENOUGH_MEMORY   2
#define OGRERR_UNSUPPORTED_GEOMETRY_TYPE 3
#define OGRERR_UNSUPPORTED_OPERATION 4
#define OGRERR_CORRUPT_DATA        5
#define OGRERR_FAILURE             6
#define OGRERR_UNSUPPORTED_SRS     7
#define OGRERR_INVALID_HANDLE      8

which was later replaced by:

#ifdef STRICT_OGRERR_TYPE
/** Type for a OGR error */
typedef enum
{
    OGRERR_NONE,                       /**< Success */
    OGRERR_NOT_ENOUGH_DATA,            /**< Not enough data to deserialize */
    OGRERR_NOT_ENOUGH_MEMORY,          /**< Not enough memory */
    OGRERR_UNSUPPORTED_GEOMETRY_TYPE,  /**< Unsupported geometry type */
    OGRERR_UNSUPPORTED_OPERATION,      /**< Unsupported operation */
    OGRERR_CORRUPT_DATA,               /**< Corrupt data */
    OGRERR_FAILURE,                    /**< Failure */
    OGRERR_UNSUPPORTED_SRS,            /**< Unsupported SRS */
    OGRERR_INVALID_HANDLE,             /**< Invalid handle */
    OGRERR_NON_EXISTING_FEATURE        /**< Non existing feature. Added in GDAL 2.0 */
} OGRErr;
#else
/** Type for a OGR error */
typedef int OGRErr;

#define OGRERR_NONE                0       /**< Success */
#define OGRERR_NOT_ENOUGH_DATA     1       /**< Not enough data to deserialize */
#define OGRERR_NOT_ENOUGH_MEMORY   2       /**< Not enough memory */
#define OGRERR_UNSUPPORTED_GEOMETRY_TYPE 3 /**< Unsupported geometry type */
#define OGRERR_UNSUPPORTED_OPERATION 4     /**< Unsupported operation */
#define OGRERR_CORRUPT_DATA        5       /**< Corrupt data */
#define OGRERR_FAILURE             6       /**< Failure */
#define OGRERR_UNSUPPORTED_SRS     7       /**< Unsupported SRS */
#define OGRERR_INVALID_HANDLE      8       /**< Invalid handle */
#define OGRERR_NON_EXISTING_FEATURE 9      /**< Non existing feature. Added in GDAL 2.0 */

#endif

The other enumerations in the library really are enums. I used the constified_enum_module option for them, and I wanted to also use it for the one above. However, I can't find a way to produce code that works with both versions of the library.

One approach I tried was to provide a replacement for the OGRErr definition:

/**
 * <div rustbindgen replaces="OGRErr"></div>
 */
typedef enum
{
    OGRERR_NONE,                       /**< Success */
    OGRERR_NOT_ENOUGH_DATA,            /**< Not enough data to deserialize */
    OGRERR_NOT_ENOUGH_MEMORY,          /**< Not enough memory */
    OGRERR_UNSUPPORTED_GEOMETRY_TYPE,  /**< Unsupported geometry type */
    OGRERR_UNSUPPORTED_OPERATION,      /**< Unsupported operation */
    OGRERR_CORRUPT_DATA,               /**< Corrupt data */
    OGRERR_FAILURE,                    /**< Failure */
    OGRERR_UNSUPPORTED_SRS,            /**< Unsupported SRS */
    OGRERR_INVALID_HANDLE,             /**< Invalid handle */
    OGRERR_NON_EXISTING_FEATURE        /**< Non existing feature. Added in GDAL 2.0 */
} StrictOGRErr;

But that doesn't work because it redefines the enum values and rustbingen replaces doesn't seem to consider them:

/**
 * <div rustbindgen replaces="OGRErr"></div>
 */
typedef enum
{
    /**
    * <div rustbindgen replaces="OGRERR_NONE"></div>
    */
    STRICT_OGRERR_NONE                       /**< Success */
} StrictOGRErr;

produces:

pub mod OGRErr {
    /// <div rustbindgen replaces="OGRErr"></div>
    pub type Type = u32;
    /// < Success
    pub const STRICT_OGRERR_NONE: Type = 0;
}

Is there a better way to do this?

@emilio
Copy link
Contributor

emilio commented Mar 4, 2018

I guess the issue why you need to use STRICT_OGRERR_NONE instead of just OGRERR_NONE is because otherwise the C code doesn't build due to name clashes? That's indeed annoying :(.

We can add an annotation to rename enum variants, would that work for you?

@lnicola
Copy link
Member Author

lnicola commented Mar 4, 2018

Yes, I think that would work.

@emilio
Copy link
Contributor

emilio commented Mar 4, 2018

#1268 Should fix it. Feel free to give it a try if you want until it gets review :)

bors-servo pushed a commit that referenced this issue Mar 5, 2018
ir: Allow replacing enums and enum variants.

Fixes #1267.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants