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

Casting an enum to an int inside match #3580

Closed
brendanzab opened this issue Sep 25, 2012 · 6 comments
Closed

Casting an enum to an int inside match #3580

brendanzab opened this issue Sep 25, 2012 · 6 comments

Comments

@brendanzab
Copy link
Member

I'm trying to convert an int to an enum, so I was attempting to use a match. Unfortunately I can't cast the enum values.

mod error {
    enum Token {
        // unable to use constants in to define an enum at compile time :(
        NoError                 = 0,                // GLFW_NO_ERROR;
        NotInitialized          = 0x00070001,       // GLFW_NOT_INITIALIZED;
        NoCurrentContext        = 0x00070002,       // GLFW_NO_CURRENT_CONTEXT;
        InvalidEnum             = 0x00070003,       // GLFW_INVALID_ENUM;
        InvalidValue            = 0x00070004,       // GLFW_INVALID_VALUE;
        OutOfMemory             = 0x00070005,       // GLFW_OUT_OF_MEMORY;
        OpenglUnavailable       = 0x00070006,       // GLFW_OPENGL_UNAVAILABLE;
        VersionUnavailable      = 0x00070007,       // GLFW_VERSION_UNAVAILABLE;
        PlatformError           = 0x00070008,       // GLFW_PLATFORM_ERROR;
        WindowNotActive         = 0x00070009,       // GLFW_WINDOW_NOT_ACTIVE;
        FormatUnavailable       = 0x0007000A,       // GLFW_FORMAT_UNAVAILABLE;
    }
}

fn get_error() -> error::Token {
    let e = glfwGetError(); // e is an int

    match e {
        error::NoError            as int => { error::NoError            }
        error::NotInitialized     as int => { error::NotInitialized     }
        error::NoCurrentContext   as int => { error::NoCurrentContext   }
        error::InvalidEnum        as int => { error::InvalidEnum        }
        error::InvalidValue       as int => { error::InvalidValue       }
        error::OutOfMemory        as int => { error::OutOfMemory        }
        error::OpenglUnavailable  as int => { error::OpenglUnavailable  }
        error::VersionUnavailable as int => { error::VersionUnavailable }
        error::PlatformError      as int => { error::PlatformError      }
        error::WindowNotActive    as int => { error::WindowNotActive    }
        error::FormatUnavailable  as int => { error::FormatUnavailable  }
        _ => { fail(~"Unknown GLFW error") }
    }
}

Error:

./src/owindow/glfw.rs:206:8: 206:36 error: mismatched types: expected enum but found `int`
./src/owindow/glfw.rs:206         error::NoError            => { error::NoError            }

This is the ugly work-around I've been using:

fn get_error() -> error::Token {
    let e = glfwGetError();

    if      e == GLFW_NO_ERROR              { error::NoError            }
    else if e == GLFW_NOT_INITIALIZED       { error::NotInitialized     }
    else if e == GLFW_NO_CURRENT_CONTEXT    { error::NoCurrentContext   }
    else if e == GLFW_INVALID_ENUM          { error::InvalidEnum        }
    else if e == GLFW_INVALID_VALUE         { error::InvalidValue       }
    else if e == GLFW_OUT_OF_MEMORY         { error::OutOfMemory        }
    else if e == GLFW_OPENGL_UNAVAILABLE    { error::OpenglUnavailable  }
    else if e == GLFW_VERSION_UNAVAILABLE   { error::VersionUnavailable }
    else if e == GLFW_PLATFORM_ERROR        { error::PlatformError      }
    else if e == GLFW_WINDOW_NOT_ACTIVE     { error::WindowNotActive    }
    else if e == GLFW_FORMAT_UNAVAILABLE    { error::FormatUnavailable  }
    else { fail(~"Unknown GLFW error") }
}
@brson
Copy link
Contributor

brson commented Sep 25, 2012

I believe this is a simple type mismatch and if you write match e as int (vs match e) then it will work.

@brson
Copy link
Contributor

brson commented Sep 25, 2012

My previous comment is obviously wrong since e is not an enum.

@brendanzab
Copy link
Member Author

Before you ask, if I try to cast the int as an enum directly I get this:

./src/owindow/glfw.rs:241:4: 241:21 error: non-scalar cast: int as glfw::error::Token
./src/owindow/glfw.rs:241     e as error::Token
                              ^~~~~~~~~~~~~~~~~

That was the whole reason why I was doing the massive match – it actually comes up a few times in my code.

@jruderman
Copy link
Contributor

See #2132

@nikomatsakis
Copy link
Contributor

I would go so far as to say that this is a dup of #2132.

@nikomatsakis
Copy link
Contributor

Incidentally, it also suggests an easy workaround. You can use reinterpret_cast, perhaps with some comparisons to make sure the value is in the proper range.

bors pushed a commit to rust-lang-ci/rust that referenced this issue May 15, 2021
RalfJung pushed a commit to RalfJung/rust that referenced this issue May 11, 2024
…RalfJung

Implement non-null pointer for malloc(0)

Use non-null pointer for malloc(0) as mentioned in  rust-lang#3576 to detect leaks and double free of ``malloc(0)`` addresses.
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

No branches or pull requests

4 participants