{"record":{"id":"2bdad8c9e2a216dd","repo":"FyroxEngine/Fyrox","slug":"cast-to-failed","errorCode":null,"errorMessage":"Cast to {} failed!","messagePattern":"Cast to (.+?) failed!","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"fyrox-core/src/lib.rs","lineNumber":106,"sourceCode":"\n/// Defines as_(variant), as_mut_(variant) and is_(variant) methods.\n#[macro_export]\nmacro_rules! define_is_as {\n    ($typ:tt : $kind:ident -> ref $result:path => fn $is:ident, fn $as_ref:ident, fn $as_mut:ident) => {\n        /// Returns true if node is instance of given type.\n        pub fn $is(&self) -> bool {\n            match self {\n                $typ::$kind(_) => true,\n                _ => false,\n            }\n        }\n\n        /// Tries to cast shared reference to a node to given type, panics if\n        /// cast is not possible.\n        pub fn $as_ref(&self) -> &$result {\n            match self {\n                $typ::$kind(ref val) => val,\n                _ => panic!(\"Cast to {} failed!\", stringify!($kind)),\n            }\n        }\n\n        /// Tries to cast mutable reference to a node to given type, panics if\n        /// cast is not possible.\n        pub fn $as_mut(&mut self) -> &mut $result {\n            match self {\n                $typ::$kind(ref mut val) => val,\n                _ => panic!(\"Cast to {} failed!\", stringify!($kind)),\n            }\n        }\n    };\n}\n\n/// Utility function that replaces back slashes \\ to forward /. Internally, it converts the input\n/// path to string (lossy - see [`Path::to_string_lossy`]) and replaces the slashes in the string.\n/// Finally, it converts the string to the PathBuf and returns it. This method is intended to be\n/// used only for paths, that does not contain non-unicode characters.","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/FyroxEngine/Fyrox/blob/76c91aad8eca488ce527b1af707be8b3b24ad72d/fyrox-core/src/lib.rs#L88-L124","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the node's actual variant before casting (e.g. matches! or downcast_ref returning Option).","Use the try/Option-returning variant of the cast API instead of the panicking one.","Check where the handle was created — you may be holding the wrong handle."],"exampleFix":"// before\nlet camera = node.as_camera(); // panics if not Camera\n// after\nif let Some(camera) = node.cast::<Camera>() {\n    // use camera\n}","handlingStrategy":"type-guard","validationCode":"// check variant before the panicking cast\nif matches!(node, NodeKind::Camera(_)) { /* safe to call as_ref */ }","typeGuard":"fn as_camera(node: &Node) -> Option<&Camera> {\n    match node {\n        Node::Camera(cam) => Some(cam),\n        _ => None,\n    }\n}","tryCatchPattern":null,"preventionTips":["Prefer Option-returning downcasts (cast::<T>() / try_cast) over panicking accessors.","Type-tag handles at creation time so only correctly-typed handles reach cast sites.","Never assume a handle's kind after scene edits or deserialization."],"tags":["downcast","type-mismatch","panic","macro"],"backgroundTag":"type-mismatch","analyzedSha":"76c91aad8eca488ce527b1af707be8b3b24ad72d","analyzedAt":"2026-09-10T16:04:01.633Z","contentChangedAt":"2026-09-10T16:04:01.633Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}