From 8860540e9f274144d03552a0719c6cb5f853418c Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Sun, 20 Mar 2022 11:39:56 +0100 Subject: [PATCH] inline now_or_never to get rid of futures-util dependency --- crates/bevy_render/Cargo.toml | 1 - .../src/render_resource/pipeline_cache.rs | 37 ++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/bevy_render/Cargo.toml b/crates/bevy_render/Cargo.toml index 7b7e40436b978d..14ba909fb3c5b7 100644 --- a/crates/bevy_render/Cargo.toml +++ b/crates/bevy_render/Cargo.toml @@ -66,4 +66,3 @@ flate2 = { version = "1.0.22", optional = true } ruzstd = { version = "0.2.4", optional = true } # For transcoding of UASTC/ETC1S universal formats, and for .basis file support basis-universal = { version = "0.2.0", optional = true } -futures-util = "0.3" diff --git a/crates/bevy_render/src/render_resource/pipeline_cache.rs b/crates/bevy_render/src/render_resource/pipeline_cache.rs index aa1efe87cce52f..cd0dbc9a61c488 100644 --- a/crates/bevy_render/src/render_resource/pipeline_cache.rs +++ b/crates/bevy_render/src/render_resource/pipeline_cache.rs @@ -90,10 +90,9 @@ impl ShaderCache { .wgpu_device() .push_error_scope(wgpu::ErrorFilter::Validation); let shader_module = render_device.create_shader_module(&module_descriptor); - use futures_util::future::FutureExt; let error = render_device.wgpu_device().pop_error_scope(); if let Some(Some(wgpu::Error::Validation { description, .. })) = - error.now_or_never() + futures_helper::now_or_never(error) { return Err(RenderPipelineError::CreateShaderModule(description)); } @@ -541,3 +540,37 @@ impl<'a> Iterator for ErrorSources<'a> { current } } + +mod futures_helper { + use std::{ + future::Future, + task::{Context, Poll, RawWaker, RawWakerVTable, Waker}, + }; + + pub fn now_or_never(future: F) -> Option { + let noop_waker = noop_waker(); + let mut cx = Context::from_waker(&noop_waker); + + futures_lite::pin!(future); + match future.poll(&mut cx) { + Poll::Ready(x) => Some(x), + _ => None, + } + } + + unsafe fn noop_clone(_data: *const ()) -> RawWaker { + noop_raw_waker() + } + + unsafe fn noop(_data: *const ()) {} + + const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop); + + fn noop_raw_waker() -> RawWaker { + RawWaker::new(std::ptr::null(), &NOOP_WAKER_VTABLE) + } + + fn noop_waker() -> Waker { + unsafe { Waker::from_raw(noop_raw_waker()) } + } +}