wgpu: Adjust imports to allow no_std.

Remaining uses of `std`:
* `trace_path` / `trace_dir`
* `std::error::Error` (waiting for Rust 1.81)
* `std::ffi::c_void`
* Various uses in `util`
This commit is contained in:
Kevin Reid authored and Nicolas Silva committed 2025-02-21 08:47:16 +01:00
1 parent 7bcbfe0712
commit b3b54b6941
26 files changed
+115 -75

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
use std::future::Future;
use core::future::Future;
use crate::*;
+4 -1
View File
@@ -1,6 +1,9 @@
use alloc::{boxed::Box, vec::Vec};
use wgt::WasmNotSendSync;
use crate::dispatch;
use crate::{Buffer, Label};
use wgt::WasmNotSendSync;
/// Descriptor for the size defining attributes of a triangle geometry, for a bottom level acceleration structure.
pub type BlasTriangleGeometrySizeDescriptor = wgt::BlasTriangleGeometrySizeDescriptor;
+3 -3
View File
@@ -1,7 +1,7 @@
use std::{
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use core::{
error, fmt,
ops::{Bound, Deref, DerefMut, Range, RangeBounds},
sync::Arc,
};
use parking_lot::Mutex;
@@ -701,7 +701,7 @@ pub struct BufferView<'a> {
inner: dispatch::DispatchBufferMappedRange,
}
impl std::ops::Deref for BufferView<'_> {
impl core::ops::Deref for BufferView<'_> {
type Target = [u8];
#[inline]
+1 -1
View File
@@ -1,4 +1,4 @@
use std::ops::Range;
use core::ops::Range;
use crate::{
api::{
+3 -2
View File
@@ -1,4 +1,5 @@
use std::{error, fmt, future::Future, sync::Arc};
use alloc::{boxed::Box, string::String, sync::Arc};
use core::{error, fmt, future::Future};
use parking_lot::Mutex;
@@ -162,7 +163,7 @@ impl Device {
let encoder = self.inner.create_render_bundle_encoder(desc);
RenderBundleEncoder {
inner: encoder,
_p: std::marker::PhantomData,
_p: core::marker::PhantomData,
}
}
+6 -4
View File
@@ -1,9 +1,11 @@
#[cfg(native)]
use alloc::vec::Vec;
use core::future::Future;
use parking_lot::Mutex;
use crate::{dispatch::InstanceInterface, *};
use std::future::Future;
bitflags::bitflags! {
/// WGSL language extensions.
///
@@ -309,7 +311,7 @@ impl Instance {
handle_source = None;
let value: &wasm_bindgen::JsValue = &canvas;
let obj = std::ptr::NonNull::from(value).cast();
let obj = core::ptr::NonNull::from(value).cast();
let raw_window_handle = raw_window_handle::WebCanvasWindowHandle::new(obj).into();
let raw_display_handle = raw_window_handle::WebDisplayHandle::new().into();
@@ -328,7 +330,7 @@ impl Instance {
handle_source = None;
let value: &wasm_bindgen::JsValue = &canvas;
let obj = std::ptr::NonNull::from(value).cast();
let obj = core::ptr::NonNull::from(value).cast();
let raw_window_handle =
raw_window_handle::WebOffscreenCanvasWindowHandle::new(obj).into();
let raw_display_handle = raw_window_handle::WebDisplayHandle::new().into();
+2 -2
View File
@@ -85,11 +85,11 @@ pub type Label<'a> = Option<&'a str>;
/// with the type to be used until the Drop impl is ran. This prevents
/// lifetimes from being shortened.
#[derive(Debug)]
pub(crate) struct PhantomDrop<T>(std::marker::PhantomData<T>);
pub(crate) struct PhantomDrop<T>(core::marker::PhantomData<T>);
impl<T> Default for PhantomDrop<T> {
fn default() -> Self {
Self(std::marker::PhantomData)
Self(core::marker::PhantomData)
}
}
+3 -1
View File
@@ -1,3 +1,5 @@
use alloc::vec::Vec;
use crate::*;
/// Handle to a pipeline cache, which is used to accelerate
@@ -67,7 +69,7 @@ use crate::*;
/// [renaming]: std::fs::rename
#[derive(Debug, Clone)]
pub struct PipelineCache {
pub(crate) inner: dispatch::DispatchPipelineCache,
pub(crate) inner: crate::dispatch::DispatchPipelineCache,
}
#[cfg(send_sync)]
+2 -1
View File
@@ -1,4 +1,5 @@
use std::ops::{Deref, DerefMut};
use alloc::boxed::Box;
use core::ops::{Deref, DerefMut};
use crate::*;
+1 -1
View File
@@ -1,4 +1,4 @@
use std::{marker::PhantomData, num::NonZeroU32, ops::Range};
use core::{marker::PhantomData, num::NonZeroU32, ops::Range};
use crate::dispatch::RenderBundleEncoderInterface;
use crate::*;
+1 -1
View File
@@ -1,4 +1,4 @@
use std::ops::Range;
use core::ops::Range;
use crate::*;
pub use wgt::{LoadOp, Operations, StoreOp};
+1 -1
View File
@@ -1,4 +1,4 @@
use std::num::NonZeroU32;
use core::num::NonZeroU32;
use crate::*;
+7 -1
View File
@@ -1,4 +1,10 @@
use std::{borrow::Cow, future::Future, marker::PhantomData};
use alloc::{
borrow::Cow,
string::{String, ToString as _},
vec,
vec::Vec,
};
use core::{future::Future, marker::PhantomData};
use crate::*;
+7 -5
View File
@@ -1,4 +1,6 @@
use std::{error, fmt};
use alloc::{boxed::Box, string::String, vec, vec::Vec};
use core::fmt;
use std::error; // TODO(https://github.com/gfx-rs/wgpu/issues/6826): use core::error after MSRV bump
use parking_lot::Mutex;
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
@@ -294,7 +296,7 @@ pub enum SurfaceTargetUnsafe {
///
/// - layer must be a valid object to create a surface upon.
#[cfg(metal)]
CoreAnimationLayer(*mut std::ffi::c_void),
CoreAnimationLayer(*mut core::ffi::c_void),
/// Surface from `IDCompositionVisual`.
///
@@ -302,7 +304,7 @@ pub enum SurfaceTargetUnsafe {
///
/// - visual must be a valid `IDCompositionVisual` to create a surface upon. Its refcount will be incremented internally and kept live as long as the resulting [`Surface`] is live.
#[cfg(dx12)]
CompositionVisual(*mut std::ffi::c_void),
CompositionVisual(*mut core::ffi::c_void),
/// Surface from DX12 `DirectComposition` handle.
///
@@ -313,7 +315,7 @@ pub enum SurfaceTargetUnsafe {
/// - surface_handle must be a valid `DirectComposition` handle to create a surface upon. Its lifetime **will not** be internally managed: this handle **should not** be freed before
/// the resulting [`Surface`] is destroyed.
#[cfg(dx12)]
SurfaceHandle(*mut std::ffi::c_void),
SurfaceHandle(*mut core::ffi::c_void),
/// Surface from DX12 `SwapChainPanel`.
///
@@ -321,7 +323,7 @@ pub enum SurfaceTargetUnsafe {
///
/// - visual must be a valid SwapChainPanel to create a surface upon. Its refcount will be incremented internally and kept live as long as the resulting [`Surface`] is live.
#[cfg(dx12)]
SwapChainPanel(*mut std::ffi::c_void),
SwapChainPanel(*mut core::ffi::c_void),
}
impl SurfaceTargetUnsafe {
+3 -1
View File
@@ -1,4 +1,6 @@
use std::{error, fmt, thread};
use core::fmt;
use std::error; // TODO(https://github.com/gfx-rs/wgpu/issues/6826): use core::error after MSRV bump
use std::thread;
use crate::*;
+3 -2
View File
@@ -1,7 +1,8 @@
use alloc::{sync::Arc, vec, vec::Vec};
use core::ops::{Index, IndexMut, Range};
use crate::{api::blas::TlasInstance, dispatch};
use crate::{BindingResource, Buffer, Label};
use std::ops::{Index, IndexMut, Range};
use std::sync::Arc;
use wgt::WasmNotSendSync;
/// Descriptor to create top level acceleration structures.
+13 -5
View File
@@ -5,16 +5,24 @@ mod ext_bindings;
#[allow(clippy::allow_attributes)]
mod webgpu_sys;
use js_sys::Promise;
use std::{
use alloc::{
boxed::Box,
format,
rc::Rc,
string::{String, ToString as _},
vec,
vec::Vec,
};
use core::{
cell::RefCell,
fmt,
future::Future,
ops::Range,
pin::Pin,
rc::Rc,
task::{self, Poll},
};
use js_sys::Promise;
use wasm_bindgen::{prelude::*, JsCast};
use crate::{dispatch, SurfaceTargetUnsafe};
@@ -54,7 +62,7 @@ impl fmt::Debug for ContextWebGpu {
impl crate::Error {
fn from_js(js_error: js_sys::Object) -> Self {
let source = Box::<dyn std::error::Error + Send + Sync>::from("<WebGPU Error>");
let source = Box::<dyn core::error::Error + Send + Sync>::from("<WebGPU Error>");
if let Some(js_error) = js_error.dyn_ref::<webgpu_sys::GpuValidationError>() {
crate::Error::Validation {
source,
@@ -967,7 +975,7 @@ fn future_compilation_info(
.collect()
}
Err(_v) => base_messages
.chain(std::iter::once(crate::CompilationMessage {
.chain(core::iter::once(crate::CompilationMessage {
message: "Getting compilation info failed".to_string(),
message_type: crate::CompilationMessageType::Error,
location: None,
@@ -1,4 +1,4 @@
use std::ops::{Deref, DerefMut};
use core::ops::{Deref, DerefMut};
use wasm_bindgen::JsValue;
+20 -13
View File
@@ -1,3 +1,20 @@
use alloc::{
borrow::Cow::Borrowed,
boxed::Box,
format,
string::{String, ToString as _},
sync::Arc,
vec,
vec::Vec,
};
use core::{error::Error, fmt, future::ready, ops::Range, pin::Pin, ptr::NonNull, slice};
use arrayvec::ArrayVec;
use parking_lot::Mutex;
use smallvec::SmallVec;
use wgc::{command::bundle_ffi::*, error::ContextErrorSource, pipeline::CreateShaderModuleError};
use wgt::WasmNotSendSync;
use crate::{
api,
dispatch::{self, BufferMappedRangeInterface, InterfaceTypes},
@@ -6,16 +23,6 @@ use crate::{
ShaderSource, SurfaceTargetUnsafe, TextureDescriptor,
};
use arrayvec::ArrayVec;
use parking_lot::Mutex;
use smallvec::SmallVec;
use std::{
borrow::Cow::Borrowed, error::Error, fmt, future::ready, ops::Range, pin::Pin, ptr::NonNull,
slice, sync::Arc,
};
use wgc::{command::bundle_ffi::*, error::ContextErrorSource, pipeline::CreateShaderModuleError};
use wgt::WasmNotSendSync;
#[derive(Clone)]
pub struct ContextWgpuCore(Arc<wgc::global::Global>);
@@ -338,7 +345,7 @@ impl ContextWgpuCore {
fn print_tree(output: &mut String, level: &mut usize, e: &(dyn Error + 'static)) {
let mut print = |e: &(dyn Error + 'static)| {
use std::fmt::Write;
use core::fmt::Write;
writeln!(output, "{}{}", " ".repeat(*level * 2), e).unwrap();
if let Some(e) = e.source() {
@@ -642,7 +649,7 @@ impl ErrorSinkRaw {
}
impl fmt::Debug for ErrorSinkRaw {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "ErrorSink")
}
}
@@ -2304,7 +2311,7 @@ impl dispatch::CommandEncoderInterface for CoreCommandEncoder {
&wgc::command::RenderPassDescriptor {
label: desc.label.map(Borrowed),
timestamp_writes: timestamp_writes.as_ref(),
color_attachments: std::borrow::Cow::Borrowed(&colors),
color_attachments: Borrowed(&colors),
depth_stencil_attachment: depth_stencil.as_ref(),
occlusion_query_set: desc.occlusion_query_set.map(|qs| qs.inner.as_core().id),
},
+14 -14
View File
@@ -4,7 +4,7 @@
//!
//! For types (like WebGPU) that don't have such a property, we generate an identifier and use that.
use std::{
use core::{
num::NonZeroU64,
sync::atomic::{AtomicU64, Ordering},
};
@@ -41,19 +41,19 @@ macro_rules! impl_eq_ord_hash_proxy {
impl Eq for $type {}
impl PartialOrd for $type {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for $type {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self $($access)*.cmp(&other $($access)*)
}
}
impl std::hash::Hash for $type {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
impl core::hash::Hash for $type {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self $($access)*.hash(state)
}
}
@@ -70,8 +70,8 @@ macro_rules! impl_eq_ord_hash_arc_address {
($type:ty => $($access:tt)*) => {
impl PartialEq for $type {
fn eq(&self, other: &Self) -> bool {
let address_left = std::sync::Arc::as_ptr(&self $($access)*);
let address_right = std::sync::Arc::as_ptr(&other $($access)*);
let address_left = alloc::sync::Arc::as_ptr(&self $($access)*);
let address_right = alloc::sync::Arc::as_ptr(&other $($access)*);
address_left == address_right
}
@@ -80,23 +80,23 @@ macro_rules! impl_eq_ord_hash_arc_address {
impl Eq for $type {}
impl PartialOrd for $type {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for $type {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let address_left = std::sync::Arc::as_ptr(&self $($access)*);
let address_right = std::sync::Arc::as_ptr(&other $($access)*);
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
let address_left = alloc::sync::Arc::as_ptr(&self $($access)*);
let address_right = alloc::sync::Arc::as_ptr(&other $($access)*);
address_left.cmp(&address_right)
}
}
impl std::hash::Hash for $type {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let address = std::sync::Arc::as_ptr(&self $($access)*);
impl core::hash::Hash for $type {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
let address = alloc::sync::Arc::as_ptr(&self $($access)*);
address.hash(state)
}
}
+5 -4
View File
@@ -13,7 +13,8 @@
use crate::{WasmNotSend, WasmNotSendSync};
use std::{any::Any, fmt::Debug, future::Future, hash::Hash, ops::Range, pin::Pin, sync::Arc};
use alloc::{boxed::Box, string::String, sync::Arc, vec::Vec};
use core::{any::Any, fmt::Debug, future::Future, hash::Hash, ops::Range, pin::Pin};
use crate::backend;
@@ -632,7 +633,7 @@ macro_rules! dispatch_types_inner {
}
}
impl std::ops::Deref for $name {
impl core::ops::Deref for $name {
type Target = dyn $trait;
#[inline]
@@ -763,7 +764,7 @@ macro_rules! dispatch_types_inner {
}
}
impl std::ops::Deref for $name {
impl core::ops::Deref for $name {
type Target = dyn $trait;
#[inline]
@@ -779,7 +780,7 @@ macro_rules! dispatch_types_inner {
}
}
impl std::ops::DerefMut for $name {
impl core::ops::DerefMut for $name {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
+6 -5
View File
@@ -14,10 +14,14 @@
//! - **`naga`** ---- Enabled when any non-wgsl shader input is enabled.
//!
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![doc(html_logo_url = "https://raw.githubusercontent.com/gfx-rs/wgpu/trunk/logo.png")]
#![warn(
clippy::alloc_instead_of_core,
clippy::allow_attributes,
clippy::std_instead_of_alloc,
clippy::std_instead_of_core,
missing_docs,
rust_2018_idioms,
unsafe_op_in_unsafe_fn
@@ -25,6 +29,8 @@
#![allow(clippy::arc_with_non_send_sync)]
#![cfg_attr(not(any(wgpu_core, webgpu)), allow(unused))]
extern crate alloc;
extern crate std;
#[cfg(wgpu_core)]
pub extern crate wgpu_core as wgc;
#[cfg(wgpu_core)]
@@ -111,8 +117,3 @@ pub use raw_window_handle as rwh;
///
#[cfg(any(webgl, webgpu))]
pub use web_sys;
/// `web-sys` has a `no_std` mode, and instead refers to the `alloc` crate in its generated code.
/// Since we vendor the WebGPU bindings we need to explicitly add the `alloc` crate ourselves.
#[cfg(webgpu)]
extern crate alloc;
+2 -1
View File
@@ -2,7 +2,8 @@ use crate::{
util::align_to, Buffer, BufferAddress, BufferDescriptor, BufferSize, BufferSlice, BufferUsages,
BufferViewMut, CommandEncoder, Device, MapMode,
};
use std::fmt;
use alloc::vec::Vec;
use core::fmt;
use std::sync::mpsc;
/// Efficiently performs many buffer writes by sharing and reusing temporary buffers.
+2
View File
@@ -1,3 +1,5 @@
use alloc::borrow::ToOwned as _;
use wgt::TextureDataOrder;
/// Describes a [Buffer](crate::Buffer) when allocating.
+1 -1
View File
@@ -1,4 +1,4 @@
use std::ops::Range;
use core::ops::Range;
use wgt::{BufferAddress, DynamicOffset, IndexFormat};
+3 -3
View File
@@ -9,8 +9,8 @@ mod encoder;
mod init;
mod texture_blitter;
use std::sync::Arc;
use std::{borrow::Cow, ptr::copy_nonoverlapping};
use alloc::{borrow::Cow, format, string::String, sync::Arc, vec};
use core::ptr::copy_nonoverlapping;
pub use belt::StagingBelt;
pub use device::{BufferInitDescriptor, DeviceExt};
@@ -134,7 +134,7 @@ impl DownloadBuffer {
}
}
impl std::ops::Deref for DownloadBuffer {
impl core::ops::Deref for DownloadBuffer {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.mapped_range.slice()