Skip to content

Commit

Permalink
inline now_or_never to get rid of futures-util dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobhellermann committed Mar 20, 2022
1 parent 9151787 commit 8860540
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
1 change: 0 additions & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
37 changes: 35 additions & 2 deletions crates/bevy_render/src/render_resource/pipeline_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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<F: Future>(future: F) -> Option<F::Output> {
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()) }
}
}

0 comments on commit 8860540

Please sign in to comment.