Skip to content

Casting an enum to an int inside match #3580

Closed
@brendanzab

Description

@brendanzab

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") }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions