{"record":{"id":"ffb9b6e53ddb4969","repo":"emilk/egui","slug":"failed-to-fit-multisamples-option-of-native-option","errorCode":null,"errorMessage":"failed to fit multisamples option of native_options into u8","messagePattern":"failed to fit multisamples option of native_options into u8","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/eframe/src/native/glow_integration.rs","lineNumber":1098,"sourceCode":"        /*  opengl setup flow goes like this:\n            1. we create a configuration for opengl \"Display\" / \"Config\" creation\n            2. choose between special extensions like glx or egl or wgl and use them to create config/display\n            3. opengl context configuration\n            4. opengl context creation\n        */\n        // start building config for gl display\n        let config_template_builder = glutin::config::ConfigTemplateBuilder::new()\n            .prefer_hardware_accelerated(hardware_acceleration)\n            .with_depth_size(native_options.depth_buffer)\n            .with_stencil_size(native_options.stencil_buffer)\n            .with_transparency(native_options.viewport.transparent.unwrap_or(false));\n        // we don't know if multi sampling option is set. so, check if its more than 0.\n        let config_template_builder = if native_options.multisampling > 0 {\n            config_template_builder.with_multisampling(\n                native_options\n                    .multisampling\n                    .try_into()\n                    .expect(\"failed to fit multisamples option of native_options into u8\"),\n            )\n        } else {\n            config_template_builder\n        };\n\n        log::debug!(\"trying to create glutin Display with config: {config_template_builder:?}\");\n\n        // Create GL display. This may probably create a window too on most platforms. Definitely on `MS windows`. Never on Android.\n        let display_builder = glutin_winit::DisplayBuilder::new()\n            // we might want to expose this option to users in the future. maybe using an env var or using native_options.\n            //\n            // The justification for FallbackEgl over PreferEgl is at https://github.com/emilk/egui/pull/2526#issuecomment-1400229576 .\n            .with_preference(glutin_winit::ApiPreference::FallbackEgl)\n            .with_window_attributes(Some(egui_winit::apply_monitor_to_window_attributes(\n                egui_winit::create_winit_window_attributes(egui_ctx, viewport_builder.clone()),\n                &viewport_builder,\n                event_loop,\n            )));","sourceCodeStart":1080,"sourceCodeEnd":1116,"githubUrl":"https://github.com/emilk/egui/blob/8fb03c9ae9a6bf1ffa602dfba51ed575f2655975/crates/eframe/src/native/glow_integration.rs#L1080-L1116","documentation":"In `GlowIntegration::new`, when `NativeOptions::multisampling` is greater than 0, eframe passes it to glutin's `with_multisampling`, which accepts only a `u8`. The `try_into().expect(...)` panics if the configured multisample count exceeds `u8::MAX` (255). The library converts user-supplied config into a glutin-friendly type and fails loudly on overflow.","triggerScenarios":"Setting `NativeOptions { multisampling: N, .. }` with N > 255 (or N that cannot fit u8) before calling `eframe::run_native` on the glow backend.","commonSituations":"Copy-pasting a large sample count (e.g. 1000) or using `u32`/`i32` typed values without narrowing; confusing MSAA sample counts with percentages; typos like 2560 instead of 16.","solutions":["Set `multisampling` to a realistic value within 0..=255 (typically 0, 2, 4, 8, or 16).","Clamp or validate the value before constructing `NativeOptions`, e.g. `multisampling: requested.min(16) as u8` if you accept user input.","If ultra-high sampling was intended, use a different AA approach (e.g. supersample via higher-resolution rendering) instead of MSAA sample count."],"exampleFix":"// before\nlet options = eframe::NativeOptions {\n    multisampling: 512, // panics: doesn't fit u8\n    ..Default::default()\n};\n// after\nlet requested: i32 = 512;\nlet options = eframe::NativeOptions {\n    multisampling: requested.clamp(0, 16) as u8,\n    ..Default::default()\n};","handlingStrategy":"validation","validationCode":"// validate before constructing NativeOptions\nlet requested: i32 = read_multisampling_from_config();\nassert!((0..=16).contains(&requested), \"multisampling must be 0..=16, got {requested}\");\nlet options = eframe::NativeOptions {\n    multisampling: requested as u8,\n    ..Default::default()\n};","typeGuard":"fn valid_multisampling(v: i32) -> Option<u8> {\n    u8::try_from(v).ok().filter(|n| *n <= 16)\n}","tryCatchPattern":"// panics (expect), so no catch is possible; validate the config value first\n// e.g. in config parsing:\nlet multisampling = match raw.parse::<i32>() {\n    Ok(v) if (0..=255).contains(&v) => v as u8,\n    _ => 4, // safe default\n};","preventionTips":["Keep multisampling to standard MSAA values: 0, 2, 4, 8, 16.","Validate/clamp user- or config-supplied sample counts before building NativeOptions.","Type the config field as u8 in your own settings struct so overflow is caught at parse time."],"tags":["eframe","glutin","config","integer-overflow","panic"],"backgroundTag":"value-out-of-range","analyzedSha":"8fb03c9ae9a6bf1ffa602dfba51ed575f2655975","analyzedAt":"2026-09-12T04:29:21.500Z","contentChangedAt":"2026-09-12T04:29:21.500Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}