Skip to content

Commit

Permalink
Prefer using core instead of std whenever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Mar 1, 2025
1 parent 74d0d92 commit 0195f1f
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 51 deletions.
2 changes: 1 addition & 1 deletion app-surface/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl HasWindowHandle for NativeWindow {
unsafe {
let a_native_window = self.a_native_window.lock().unwrap();
let handle = AndroidNdkWindowHandle::new(
std::ptr::NonNull::new(*a_native_window as *mut _ as *mut c_void).unwrap(),
core::ptr::NonNull::new(*a_native_window as *mut _ as *mut c_void).unwrap(),
);
Ok(WindowHandle::borrow_raw(RawWindowHandle::AndroidNdk(
handle,
Expand Down
2 changes: 1 addition & 1 deletion app-surface/src/ios.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::marker::Sync;
use core_graphics_types::{base::CGFloat, geometry::CGRect};
use libc::c_void;
use objc::{runtime::Object, *};
use std::marker::Sync;

#[repr(C)]
pub struct IOSViewObj {
Expand Down
2 changes: 1 addition & 1 deletion app-surface/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Deref;
use core::ops::Deref;
use wgpu::{Instance, Surface};

mod touch;
Expand Down
10 changes: 5 additions & 5 deletions app-surface/src/web_rwh/canvas.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::SendSyncWrapper;
use raw_window_handle::{
HasDisplayHandle, HasWindowHandle, RawWindowHandle, WebCanvasWindowHandle, WindowHandle,
};
use std::{
use core::{
ops::{Deref, DerefMut},
ptr::NonNull,
};
use raw_window_handle::{
HasDisplayHandle, HasWindowHandle, RawWindowHandle, WebCanvasWindowHandle, WindowHandle,
};
use wasm_bindgen::{JsCast, JsValue};

#[derive(Debug)]
Expand Down Expand Up @@ -94,7 +94,7 @@ impl HasWindowHandle for CanvasWrapper {
&self,
) -> Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
let value: &JsValue = &self.element;
let obj: NonNull<std::ffi::c_void> = NonNull::from(value).cast();
let obj: NonNull<core::ffi::c_void> = NonNull::from(value).cast();
let handle = WebCanvasWindowHandle::new(obj);
let raw = RawWindowHandle::WebCanvas(handle);
unsafe { Ok(WindowHandle::borrow_raw(raw)) }
Expand Down
6 changes: 3 additions & 3 deletions app-surface/src/web_rwh/offscreen_canvas.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::{Canvas, SendSyncWrapper};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::{
use core::{
ops::{Deref, DerefMut},
ptr::NonNull,
};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use wasm_bindgen::JsValue;

#[derive(Debug)]
Expand Down Expand Up @@ -71,7 +71,7 @@ impl HasWindowHandle for OffscreenCanvasWrapper {
use raw_window_handle::{RawWindowHandle, WebOffscreenCanvasWindowHandle, WindowHandle};

let value: &JsValue = &self.inner;
let obj: NonNull<std::ffi::c_void> = NonNull::from(value).cast();
let obj: NonNull<core::ffi::c_void> = NonNull::from(value).cast();
let handle = WebOffscreenCanvasWindowHandle::new(obj);
let raw = RawWindowHandle::WebOffscreenCanvas(handle);
unsafe { Ok(WindowHandle::borrow_raw(raw)) }
Expand Down
3 changes: 2 additions & 1 deletion wgpu-in-app/src/examples/boids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
use super::Example;
use app_surface::{AppSurface, SurfaceFrame};
use core::mem;
use rand::{
SeedableRng,
distributions::{Distribution, Uniform},
};
use std::{borrow::Cow, mem};
use std::borrow::Cow;
use wgpu::util::DeviceExt;

// number of boid particles to simulate
Expand Down
5 changes: 2 additions & 3 deletions wgpu-in-app/src/examples/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use super::Example;
use app_surface::{AppSurface, SurfaceFrame};
use bytemuck::{Pod, Zeroable};
use std::mem::size_of;
use std::{future::Future, pin::Pin, task};
use core::{future::Future, mem::size_of, pin::Pin, task};
use wgpu::util::DeviceExt;
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
Expand Down Expand Up @@ -323,7 +322,7 @@ impl Cube {

fn generate_matrix(aspect_ratio: f32) -> glam::Mat4 {
let projection =
glam::Mat4::perspective_rh(std::f32::consts::FRAC_PI_4, aspect_ratio, 1.0, 10.0);
glam::Mat4::perspective_rh(core::f32::consts::FRAC_PI_4, aspect_ratio, 1.0, 10.0);
let view = glam::Mat4::look_at_rh(
glam::Vec3::new(1.5f32, -5.0, 3.0),
glam::Vec3::ZERO,
Expand Down
7 changes: 4 additions & 3 deletions wgpu-in-app/src/examples/msaa_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
use super::Example;
use app_surface::{AppSurface, SurfaceFrame};

use std::{borrow::Cow, iter};
use core::iter;
use std::borrow::Cow;

use bytemuck::{Pod, Zeroable};
use wgpu::util::DeviceExt;
Expand Down Expand Up @@ -91,7 +92,7 @@ impl MSAALine {
x += step;
for i in 0..max {
let percent = i as f32 / max as f32;
let (sin, cos) = (percent * 2.0 * std::f32::consts::PI).sin_cos();
let (sin, cos) = (percent * 2.0 * core::f32::consts::PI).sin_cos();
vertex_data.push(Vertex {
_pos: [x, 0.0],
_color: [1.0, -sin, cos, 1.0],
Expand Down Expand Up @@ -155,7 +156,7 @@ impl MSAALine {
entry_point: Some("vs_main"),
compilation_options: Default::default(),
buffers: &[wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
array_stride: core::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x4],
}],
Expand Down
5 changes: 2 additions & 3 deletions wgpu-in-app/src/examples/page_turning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use app_surface::{AppSurface, SurfaceFrame};

use zerocopy::{AsBytes, FromBytes};

use std::cell::RefCell;
use core::{cell::RefCell, f32::consts::FRAC_PI_2};
use std::cmp::PartialEq;
use std::f32::consts::FRAC_PI_2;
use std::rc::Rc;

#[repr(C)]
Expand Down Expand Up @@ -149,7 +148,7 @@ impl PageTurning {
};

let distance = (dx * dx + dy * dy).sqrt();
let half_circle = std::f32::consts::PI * self.turning_uniform.radius;
let half_circle = core::f32::consts::PI * self.turning_uniform.radius;
let pi_2 = FRAC_PI_2;

let a = -dy.atan2(dx);
Expand Down
57 changes: 29 additions & 28 deletions wgpu-in-app/src/examples/point_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::collections::HashMap;
/// X multiplication factor.
/// 1.0 / sqrt(2)
///
const A: f32 = std::f32::consts::FRAC_1_SQRT_2;
const A: f32 = core::f32::consts::FRAC_1_SQRT_2;

///
/// Y multiplication factor.
Expand All @@ -23,7 +23,7 @@ const B: f32 = SQRT_3 * A;
///
/// `sin(45deg)` is used to rotate the points.
///
const S45: f32 = std::f32::consts::FRAC_1_SQRT_2;
const S45: f32 = core::f32::consts::FRAC_1_SQRT_2;
///
/// `cos(45deg)` is used to rotate the points.
///
Expand Down Expand Up @@ -157,32 +157,33 @@ impl HexTerrainMesh {
fn half(p1: &TerrainVertex, p2: &TerrainVertex) -> glam::Vec3 {
(p1.position + p2.position) / 2.0
}
let mut push_triangle = |p1: &TerrainVertex,
p2: &TerrainVertex,
p: &TerrainVertex,
c: [u8; 4]| {
let m = middle(p1, p2, p);
let ap = half(p1, p);
let bp = half(p2, p);
let p = p.position;
let n1 = calculate_normal(ap, m, p);
let n2 = calculate_normal(m, bp, p);
let mut push_triangle =
|p1: &TerrainVertex, p2: &TerrainVertex, p: &TerrainVertex, c: [u8; 4]| {
let m = middle(p1, p2, p);
let ap = half(p1, p);
let bp = half(p2, p);
let p = p.position;
let n1 = calculate_normal(ap, m, p);
let n2 = calculate_normal(m, bp, p);

vertices.extend(
[ap, m, p, m, bp, p]
.iter()
.zip(
std::iter::repeat::<[f32; 3]>(n1.into())
.chain(std::iter::repeat::<[f32; 3]>(n2.into())),
)
.zip(std::iter::repeat(c))
.map(|((pos, normal), colour)| TerrainVertexAttributes {
position: *pos.as_ref(),
normal,
colour,
}),
);
};
vertices.extend(
[ap, m, p, m, bp, p]
.iter()
.zip(
core::iter::repeat::<[f32; 3]>(n1.into()).chain(core::iter::repeat::<
[f32; 3],
>(
n2.into()
)),
)
.zip(core::iter::repeat(c))
.map(|((pos, normal), colour)| TerrainVertexAttributes {
position: *pos.as_ref(),
normal,
colour,
}),
);
};
for i in -self.half_size..=self.half_size {
for j in -self.half_size..=self.half_size {
if let Some(p) = self.vertices.get(&(i, j)) {
Expand Down Expand Up @@ -222,7 +223,7 @@ impl HexWaterMesh {
let z = B * (x_o * S45 + y_o * C45);
if x.hypot(z) < radius {
let x = (x * 2.0).round() as i16;
let z = ((z / B) * std::f32::consts::SQRT_2).round() as i16;
let z = ((z / B) * core::f32::consts::SQRT_2).round() as i16;
map.insert((i, j), [x, z]);
}
}
Expand Down
3 changes: 2 additions & 1 deletion wgpu-in-app/src/examples/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use super::Example;
use app_surface::{AppSurface, SurfaceFrame};

use std::{borrow::Cow, f32::consts, iter, mem, ops::Range, rc::Rc};
use core::{f32::consts, iter, mem, ops::Range};
use std::{borrow::Cow, rc::Rc};

use bytemuck::{Pod, Zeroable};
use wgpu::util::{DeviceExt, align_to};
Expand Down
3 changes: 2 additions & 1 deletion wgpu-in-app/src/examples/water.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use super::{Example, point_gen};
use app_surface::{AppSurface, SurfaceFrame};

use bytemuck::{Pod, Zeroable};
use core::{f32::consts, iter, mem};
use glam::Vec3;
use rand::SeedableRng;
use std::{borrow::Cow, f32::consts, iter, mem};
use std::borrow::Cow;
use wgpu::util::DeviceExt;

///
Expand Down

0 comments on commit 0195f1f

Please sign in to comment.