FyroxEngine/Fyrox · error

Cast to failed!

Error message

Cast to {} failed!

What it means

Generated try-cast helper (as_ref) for enum wrapper types panics when the variant does not match the requested kind: the caller asked for a reference of one concrete node type but the enum holds a different variant. It exists to give a descriptive message instead of an unwrap failure on a failed downcast.

Solutions

  1. Verify the node's actual variant before casting (e.g. matches! or downcast_ref returning Option).
  2. Use the try/Option-returning variant of the cast API instead of the panicking one.
  3. Check where the handle was created — you may be holding the wrong handle.

Example fix

// before
let camera = node.as_camera(); // panics if not Camera
// after
if let Some(camera) = node.cast::<Camera>() {
    // use camera
}
Defensive patterns

Strategy: type-guard

Validate before calling

// check variant before the panicking cast
if matches!(node, NodeKind::Camera(_)) { /* safe to call as_ref */ }

Type guard

fn as_camera(node: &Node) -> Option<&Camera> {
    match node {
        Node::Camera(cam) => Some(cam),
        _ => None,
    }
}

Prevention

When it happens

Trigger: Calling the generated $as_ref method (e.g. node.as_..., cast helpers on scene node enums) when the underlying enum variant is a different type than requested.

Common situations: Assuming a handle points to a specific node kind (e.g. treating a Mesh as a Camera); lookups by index after the node was replaced; script code casting without checking the node type first.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of FyroxEngine/Fyrox@76c91aad8e (2026-09-10). Data as JSON: /api/errors/2bdad8c9e2a216dd. Report an issue: GitHub.

Appendix: source

Thrown at fyrox-core/src/lib.rs:106

/// Defines as_(variant), as_mut_(variant) and is_(variant) methods.
#[macro_export]
macro_rules! define_is_as {
    ($typ:tt : $kind:ident -> ref $result:path => fn $is:ident, fn $as_ref:ident, fn $as_mut:ident) => {
        /// Returns true if node is instance of given type.
        pub fn $is(&self) -> bool {
            match self {
                $typ::$kind(_) => true,
                _ => false,
            }
        }

        /// Tries to cast shared reference to a node to given type, panics if
        /// cast is not possible.
        pub fn $as_ref(&self) -> &$result {
            match self {
                $typ::$kind(ref val) => val,
                _ => panic!("Cast to {} failed!", stringify!($kind)),
            }
        }

        /// Tries to cast mutable reference to a node to given type, panics if
        /// cast is not possible.
        pub fn $as_mut(&mut self) -> &mut $result {
            match self {
                $typ::$kind(ref mut val) => val,
                _ => panic!("Cast to {} failed!", stringify!($kind)),
            }
        }
    };
}

/// Utility function that replaces back slashes \ to forward /. Internally, it converts the input
/// path to string (lossy - see [`Path::to_string_lossy`]) and replaces the slashes in the string.
/// Finally, it converts the string to the PathBuf and returns it. This method is intended to be
/// used only for paths, that does not contain non-unicode characters.

View on GitHub (pinned to 76c91aad8e)