Skip to content

Commit

Permalink
mesh: Use core and alloc — but not quite hitting no_std.
Browse files Browse the repository at this point in the history
And document why it's not `no_std` for my own future reference.
  • Loading branch information
kpreid committed Dec 15, 2023
1 parent 6eea1ba commit 2981c7a
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 19 deletions.
4 changes: 2 additions & 2 deletions all-is-cubes-mesh/src/block_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! This module is internal and reexported by its parent.
use std::fmt;
use core::fmt;
use std::sync::Arc;

use all_is_cubes::block::{AnimationChange, EvaluatedBlock, Evoxel, Evoxels, Resolution};
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<M: MeshTypes + 'static> BlockMesh<M> {
pub(super) fn all_face_meshes(
&self,
) -> impl Iterator<Item = (Face7, &BlockFaceMesh<M::Vertex>)> {
std::iter::once((Face7::Within, &self.interior_vertices)).chain(
core::iter::once((Face7::Within, &self.interior_vertices)).chain(
self.face_vertices
.iter()
.map(|(f, mesh)| (Face7::from(f), mesh)),
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-mesh/src/block_vertex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Mesh vertices.
use std::fmt;
use core::fmt;

use all_is_cubes::euclid::Point3D;
use all_is_cubes::math::{Cube, Face6, FreeCoordinate, FreePoint, FreeVector, Rgba};
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-mesh/src/dynamic/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::num::NonZeroU32;

use all_is_cubes::math::Cube;
use fnv::FnvHashSet;

use all_is_cubes::block::{EvaluatedBlock, Resolution};
use all_is_cubes::math::Cube;
use all_is_cubes::space::{BlockIndex, Space};
use all_is_cubes::time;
use all_is_cubes::util::{Refmt as _, StatusText, TimeStats};
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-mesh/src/dynamic/chunked_mesh.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::fmt;
use std::collections::{hash_map::Entry::*, HashMap, HashSet};
use std::fmt;
use std::sync::{Arc, Mutex, Weak};

use fnv::{FnvHashMap, FnvHashSet};
Expand Down Expand Up @@ -572,7 +572,7 @@ impl CsmUpdateInfo {
*depth_sort_time = [*depth_sort_time, other.depth_sort_time]
.into_iter()
.flatten()
.reduce(std::ops::Add::add);
.reduce(core::ops::Add::add);
*block_updates += other.block_updates;
*chunk_count = other.chunk_count; // replace!
*chunk_total_cpu_byte_size = other.chunk_total_cpu_byte_size; // replace!
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-mesh/src/dynamic/render_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use core::fmt;

#[cfg(doc)]
use crate::dynamic::ChunkedSpaceMesh;
Expand Down
7 changes: 4 additions & 3 deletions all-is-cubes-mesh/src/index_vec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::mem;
use alloc::vec::Vec;
use core::{mem, ops};

use either::Either;

Expand Down Expand Up @@ -44,8 +45,8 @@ impl IndexVec {
#[inline]
pub fn as_slice<R>(&self, range: R) -> IndexSlice<'_>
where
[u16]: std::ops::Index<R, Output = [u16]>,
[u32]: std::ops::Index<R, Output = [u32]>,
[u16]: ops::Index<R, Output = [u16]>,
[u32]: ops::Index<R, Output = [u32]>,
{
match self {
IndexVec::U16(vec) => IndexSlice::U16(&vec.as_slice()[range]),
Expand Down
8 changes: 8 additions & 0 deletions all-is-cubes-mesh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
// Crate-specific lint settings. (General settings can be found in the workspace manifest.)
#![forbid(unsafe_code)]

// This crate is *almost* `no_std` compatible; the sticking point is the `Mutex` that
// `chunked_mesh::CsmTodo` uses. We could address that by exporting
// `all_is_cubes::util::maybe_sync::Mutex`, but I don't want to do that (and switch hash maps to
// `hashbrown`) until I have some imaginable use case for mesh building on a `no_std` target.
// So for now, the code is just in a state of “reveal how close it is”, hence using `core` and
// `alloc` imports.
extern crate alloc;

use core::fmt;

use all_is_cubes::camera::{GraphicsOptions, TransparencyOption};
Expand Down
3 changes: 2 additions & 1 deletion all-is-cubes-mesh/src/planar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Triangulator's 2D plane operations (sliced voxels to texels and meshes).
use std::ops::Range;
use alloc::vec::Vec;
use core::ops::Range;

use all_is_cubes::block::Resolution;
use all_is_cubes::euclid::{Point2D, Scale, Transform3D, Vector2D};
Expand Down
11 changes: 6 additions & 5 deletions all-is-cubes-mesh/src/space_mesh.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;
use std::ops::Range;
use alloc::vec::Vec;
use core::ops::Range;
use core::{fmt, ops};

use bitvec::vec::BitVec;
use ordered_float::OrderedFloat;
Expand Down Expand Up @@ -126,7 +127,7 @@ impl<M: MeshTypes> SpaceMesh<M> {
/// Returns the total memory (not counting allocator overhead) occupied by this
/// [`SpaceMesh`] value and all its owned objects.
pub fn total_byte_size(&self) -> usize {
use std::mem::size_of;
use core::mem::size_of;

let SpaceMesh {
vertices,
Expand Down Expand Up @@ -418,7 +419,7 @@ impl<M: MeshTypes> SpaceMesh<M> {
}
}

impl<M: MeshTypes> std::ops::Deref for SpaceMesh<M> {
impl<M: MeshTypes> ops::Deref for SpaceMesh<M> {
type Target = MeshMeta<M>;

fn deref(&self) -> &Self::Target {
Expand Down Expand Up @@ -926,7 +927,7 @@ mod tests {
use crate::BlockVertex;
use all_is_cubes::block::Block;
use all_is_cubes::math::Rgba;
use std::mem;
use core::mem;

type TestMesh = SpaceMesh<TextureMt>;

Expand Down
6 changes: 3 additions & 3 deletions all-is-cubes-mesh/src/texture.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Traits for textures used by the meshes this library generates.
use std::fmt;
use core::fmt;

use all_is_cubes::block::{Evoxel, Evoxels};
use all_is_cubes::content::palette;
Expand Down Expand Up @@ -130,15 +130,15 @@ impl<T: Allocator> Allocator for &T {
<T as Allocator>::allocate(self, bounds, channels)
}
}
impl<T: Allocator> Allocator for std::sync::Arc<T> {
impl<T: Allocator> Allocator for alloc::sync::Arc<T> {
type Tile = T::Tile;
type Point = T::Point;
#[mutants::skip] // trivial
fn allocate(&self, bounds: GridAab, channels: Channels) -> Option<Self::Tile> {
<T as Allocator>::allocate(self, bounds, channels)
}
}
impl<T: Allocator> Allocator for std::rc::Rc<T> {
impl<T: Allocator> Allocator for alloc::rc::Rc<T> {
type Tile = T::Tile;
type Point = T::Point;
#[mutants::skip] // trivial
Expand Down

0 comments on commit 2981c7a

Please sign in to comment.