Skip to content

Commit 4595708

Browse files
authored
Fix panic when creating a surface while no backend is available (#5166)
* Fix panic when creating a surface while no backend is available * changelog entry
1 parent 0888a63 commit 4595708

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Bottom level categories:
103103
- Fix `serde` feature not compiling for `wgpu-types`. By @KirmesBude in [#5149](https://github.com/gfx-rs/wgpu/pull/5149)
104104
- Fix the validation of vertex and index ranges. By @nical in [#5144](https://github.com/gfx-rs/wgpu/pull/5144) and [#5156](https://github.com/gfx-rs/wgpu/pull/5156)
105105
- Device lost callbacks are invoked when replaced and when global is dropped. By @bradwerth in [#5168](https://github.com/gfx-rs/wgpu/pull/5168)
106+
- Fix panic when creating a surface while no backend is available. By @wumpf [#5166](https://github.com/gfx-rs/wgpu/pull/5166)
106107

107108
#### WGL
108109

wgpu-core/src/instance.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,15 @@ pub enum RequestAdapterError {
471471
InvalidSurface(SurfaceId),
472472
}
473473

474+
#[derive(Clone, Debug, Error)]
475+
#[non_exhaustive]
476+
pub enum CreateSurfaceError {
477+
#[error("No backend is available")]
478+
NoSupportedBackend,
479+
#[error(transparent)]
480+
InstanceError(#[from] hal::InstanceError),
481+
}
482+
474483
impl Global {
475484
/// # Safety
476485
///
@@ -483,7 +492,7 @@ impl Global {
483492
display_handle: raw_window_handle::RawDisplayHandle,
484493
window_handle: raw_window_handle::RawWindowHandle,
485494
id_in: Option<SurfaceId>,
486-
) -> Result<SurfaceId, hal::InstanceError> {
495+
) -> Result<SurfaceId, CreateSurfaceError> {
487496
profiling::scope!("Instance::create_surface");
488497

489498
fn init<A: HalApi>(
@@ -521,8 +530,7 @@ impl Global {
521530
hal_surface = init::<hal::api::Gles>(&self.instance.gl, display_handle, window_handle);
522531
}
523532

524-
// This is only None if there's no instance at all.
525-
let hal_surface = hal_surface.unwrap()?;
533+
let hal_surface = hal_surface.ok_or(CreateSurfaceError::NoSupportedBackend)??;
526534

527535
let surface = Surface {
528536
presentation: Mutex::new(None),

wgpu/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,7 +2812,7 @@ pub struct CreateSurfaceError {
28122812
enum CreateSurfaceErrorKind {
28132813
/// Error from [`wgpu_hal`].
28142814
#[cfg(wgpu_core)]
2815-
Hal(hal::InstanceError),
2815+
Hal(wgc::instance::CreateSurfaceError),
28162816

28172817
/// Error from WebGPU surface creation.
28182818
#[allow(dead_code)] // may be unused depending on target and features
@@ -2847,8 +2847,8 @@ impl error::Error for CreateSurfaceError {
28472847
}
28482848

28492849
#[cfg(wgpu_core)]
2850-
impl From<hal::InstanceError> for CreateSurfaceError {
2851-
fn from(e: hal::InstanceError) -> Self {
2850+
impl From<wgc::instance::CreateSurfaceError> for CreateSurfaceError {
2851+
fn from(e: wgc::instance::CreateSurfaceError) -> Self {
28522852
Self {
28532853
inner: CreateSurfaceErrorKind::Hal(e),
28542854
}

0 commit comments

Comments
 (0)