Skip to content

Commit

Permalink
Replace all_is_cubes::util::CustomFormat with manyfmt library.
Browse files Browse the repository at this point in the history
`manyfmt` is the same code split out as a separate library.
  • Loading branch information
kpreid committed Oct 7, 2023
1 parent 3e9945a commit 964eab7
Show file tree
Hide file tree
Showing 31 changed files with 185 additions and 265 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
This avoids confusion between points in space and cube-identifying coordinates.
- Renamed `raytracer::PixelBuf` trait to `Accumulate`.

- `all_is_cubes::util::CustomFormat` has been split into a separate library called `manyfmt`,
reexported at `all_is_cubes::util::manyfmt`.
The `CustomFormat` trait is now split into two traits, `Fmt` (for implementing) and `Refmt` (extension).

- `all-is-cubes-mesh` library:
- Renamed `TextureAllocator` to `texture::Allocator`.
- Renamed `TextureTile` to `texture::Tile`.
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ instant = "0.1.12"
itertools = { version = "0.11.0", default-features = false, features = ["use_alloc"] }
log = { version = "0.4.17", default-features = false }
macro_rules_attribute = "0.2.0"
manyfmt = "0.1.0"
mutants = "0.0.3"
num-traits = { version = "0.2.15", default-features = false }
once_cell = "1.17.1"
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-desktop/src/terminal/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use all_is_cubes::character::{Character, Cursor};
use all_is_cubes::euclid::Vector2D;
use all_is_cubes::inv::Slot;
use all_is_cubes::universe::URef;
use all_is_cubes::util::{CustomFormat, StatusText};
use all_is_cubes::util::{Refmt as _, StatusText};

use crate::terminal::chars::{image_patch_to_character, write_colored_and_measure};
use crate::terminal::TextRayImage;
Expand Down Expand Up @@ -478,7 +478,7 @@ impl TerminalState {
);

f.render_widget(
Paragraph::new(format!("{}", info.custom_format(StatusText))),
Paragraph::new(format!("{}", info.refmt(&StatusText))),
render_info_rect,
);
}
Expand Down
62 changes: 31 additions & 31 deletions all-is-cubes-gpu/src/common/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;
use std::time::Duration;

use all_is_cubes::camera::{Flaws, Layers};
use all_is_cubes::util::{CustomFormat, StatusText};
use all_is_cubes::util::{Fmt, Refmt, StatusText};
use all_is_cubes_mesh::dynamic::CsmUpdateInfo;

/// Performance info about drawing an entire scene.
Expand Down Expand Up @@ -61,8 +61,8 @@ pub struct DrawInfo {
pub(crate) submit_time: Option<Duration>,
}

impl CustomFormat<StatusText> for RenderInfo {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, _: StatusText) -> fmt::Result {
impl Fmt<StatusText> for RenderInfo {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, _: &StatusText) -> fmt::Result {
let &Self {
waiting_for_gpu,
update:
Expand Down Expand Up @@ -93,42 +93,42 @@ impl CustomFormat<StatusText> for RenderInfo {
fmt,
// TODO: adjust this format to account for more pieces
"Frame time: {} (GPU wait {}, update {}, draw world {}, ui {}",
total_time.custom_format(StatusText),
waiting_for_gpu.custom_format(StatusText),
update_time.custom_format(StatusText),
draw_time.world.custom_format(StatusText),
draw_time.ui.custom_format(StatusText),
total_time.refmt(&StatusText),
waiting_for_gpu.refmt(&StatusText),
update_time.refmt(&StatusText),
draw_time.world.refmt(&StatusText),
draw_time.ui.refmt(&StatusText),
)?;
if let Some(t) = submit_time {
write!(fmt, ", submit {}", t.custom_format(StatusText))?;
write!(fmt, ", submit {}", t.refmt(&StatusText))?;
}
writeln!(fmt, ")")?;

// UpdateInfo details
write!(
fmt,
"Update breakdown: prep {}, world mesh {}, ui mesh {}, lines {}",
update_prep_time.custom_format(StatusText),
update_spaces.world.total_time.custom_format(StatusText),
update_spaces.ui.total_time.custom_format(StatusText),
lines_time.custom_format(StatusText),
update_prep_time.refmt(&StatusText),
update_spaces.world.total_time.refmt(&StatusText),
update_spaces.ui.total_time.refmt(&StatusText),
lines_time.refmt(&StatusText),
)?;
if let Some(t) = update_submit_time {
write!(fmt, ", submit {}", t.custom_format(StatusText))?;
write!(fmt, ", submit {}", t.refmt(&StatusText))?;
}

// Spaces
write!(
fmt,
"\n\nWORLD:\n{}\n{}\n\n",
update_spaces.world.custom_format(StatusText),
draw_spaces.world.custom_format(StatusText)
update_spaces.world.refmt(&StatusText),
draw_spaces.world.refmt(&StatusText)
)?;
write!(
fmt,
"UI:\n{}\n{}",
update_spaces.ui.custom_format(StatusText),
draw_spaces.ui.custom_format(StatusText)
update_spaces.ui.refmt(&StatusText),
draw_spaces.ui.refmt(&StatusText)
)?;

write!(fmt, "\nRender flaws: {flaws}")?;
Expand Down Expand Up @@ -164,8 +164,8 @@ impl SpaceUpdateInfo {
}
}

impl CustomFormat<StatusText> for SpaceUpdateInfo {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, format_type: StatusText) -> fmt::Result {
impl Fmt<StatusText> for SpaceUpdateInfo {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, fopt: &StatusText) -> fmt::Result {
let &SpaceUpdateInfo {
total_time: _, // we print this as summary info from the parent only
ref chunk_info,
Expand All @@ -174,14 +174,14 @@ impl CustomFormat<StatusText> for SpaceUpdateInfo {
light_update_count,
} = self;

let light_update_time = light_update_time.custom_format(format_type);
let light_update_time = light_update_time.refmt(fopt);

writeln!(fmt, "{}", chunk_info.custom_format(format_type))?;
writeln!(fmt, "{}", chunk_info.refmt(fopt))?;
writeln!(
fmt,
"Light: {light_update_count:3} cubes in {light_update_time}"
)?;
write!(fmt, "{:#?}", texture_info.custom_format(StatusText))?;
write!(fmt, "{:#?}", texture_info.refmt(fopt))?;
Ok(())
}
}
Expand Down Expand Up @@ -217,8 +217,8 @@ pub struct SpaceDrawInfo {
pub(crate) flaws: Flaws,
}

impl CustomFormat<StatusText> for SpaceDrawInfo {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, format_type: StatusText) -> fmt::Result {
impl Fmt<StatusText> for SpaceDrawInfo {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, fopt: &StatusText) -> fmt::Result {
let &SpaceDrawInfo {
draw_init_time,
draw_opaque_time,
Expand All @@ -228,9 +228,9 @@ impl CustomFormat<StatusText> for SpaceDrawInfo {
flaws: _, // TODO: include or exclude?
} = self;

let draw_init_time = draw_init_time.custom_format(format_type);
let draw_opaque_time = draw_opaque_time.custom_format(format_type);
let draw_transparent_time = draw_transparent_time.custom_format(format_type);
let draw_init_time = draw_init_time.refmt(fopt);
let draw_opaque_time = draw_opaque_time.refmt(fopt);
let draw_transparent_time = draw_transparent_time.refmt(fopt);

writeln!(
fmt,
Expand Down Expand Up @@ -268,8 +268,8 @@ impl Default for BlockTextureInfo {
}
}

impl CustomFormat<StatusText> for BlockTextureInfo {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, format_type: StatusText) -> fmt::Result {
impl Fmt<StatusText> for BlockTextureInfo {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, fopt: &StatusText) -> fmt::Result {
write!(
fmt,
"Textures: {} tiles, {} texels ({}% of {}) used, {:2} flushed in {}",
Expand All @@ -278,7 +278,7 @@ impl CustomFormat<StatusText> for BlockTextureInfo {
(self.in_use_texels as f32 / self.capacity_texels as f32 * 100.0).ceil() as usize,
self.capacity_texels,
self.flushed,
self.flush_time.custom_format(format_type)
self.flush_time.refmt(fopt)
)
}
}
24 changes: 12 additions & 12 deletions all-is-cubes-gpu/tests/shaders/wgsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::fmt;

use all_is_cubes::util::CustomFormat;
use all_is_cubes::util::{Fmt, Refmt as _};

/// Generate a fragment shader entry point from an expression.
pub fn frag_expr(expr: &str) -> String {
Expand All @@ -22,30 +22,30 @@ trait WgslTypeName {
#[derive(Clone, Copy, Debug)]
pub struct ToWgsl;

pub fn to_wgsl<T: CustomFormat<ToWgsl>>(value: T) -> String {
value.custom_format(ToWgsl).to_string()
pub fn to_wgsl<T: Fmt<ToWgsl>>(value: T) -> String {
value.refmt(&ToWgsl).to_string()
}

impl CustomFormat<ToWgsl> for f32 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, _: ToWgsl) -> fmt::Result {
impl Fmt<ToWgsl> for f32 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, _: &ToWgsl) -> fmt::Result {
write!(fmt, "{:?}", self)
}
}

impl<T> CustomFormat<ToWgsl> for [T; 4]
impl<T> Fmt<ToWgsl> for [T; 4]
where
T: CustomFormat<ToWgsl> + WgslTypeName,
T: Fmt<ToWgsl> + WgslTypeName,
{
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, f: ToWgsl) -> fmt::Result {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, f: &ToWgsl) -> fmt::Result {
let [x, y, z, w] = self;
let t = T::wgsl_type_name();
write!(
fmt,
"vec4<{t}>({x}, {y}, {z}, {w})",
x = x.custom_format(f),
y = y.custom_format(f),
z = z.custom_format(f),
w = w.custom_format(f),
x = x.refmt(f),
y = y.refmt(f),
z = z.refmt(f),
w = w.refmt(f),
)
}
}
8 changes: 4 additions & 4 deletions all-is-cubes-mesh/src/block_vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt;

use all_is_cubes::euclid::Point3D;
use all_is_cubes::math::{Cube, Face6, FreeCoordinate, FreePoint, FreeVector, Rgba};
use all_is_cubes::util::{ConciseDebug, CustomFormat};
use all_is_cubes::util::{ConciseDebug, Fmt, Refmt as _};

/// Basic vertex data type for a [`BlockMesh`].
/// Implement <code>[`From`]&lt;[`BlockVertex`]&gt;</code> (and usually [`GfxVertex`])
Expand Down Expand Up @@ -77,22 +77,22 @@ where
write!(
fmt,
"{{ p: {:?} n: {:?} c: {:?} }}",
self.position.custom_format(ConciseDebug),
self.position.refmt(&ConciseDebug),
self.face,
self.coloring
)
}
}
impl<T> fmt::Debug for Coloring<T>
where
T: CustomFormat<ConciseDebug>, // TODO: inelegant
T: Fmt<ConciseDebug>, // TODO: inelegant
{
// TODO: test formatting of this
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Coloring::Solid(color) => write!(fmt, "Solid({color:?})"),
Coloring::Texture { pos, .. } => {
write!(fmt, "Texture({:?})", pos.custom_format(ConciseDebug))
write!(fmt, "Texture({:?})", pos.refmt(&ConciseDebug))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-mesh/src/dynamic/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use fnv::FnvHashSet;
use all_is_cubes::block::{EvaluatedBlock, Resolution};
use all_is_cubes::space::{BlockIndex, Space};
use all_is_cubes::time;
use all_is_cubes::util::{CustomFormat as _, StatusText, TimeStats};
use all_is_cubes::util::{Refmt as _, StatusText, TimeStats};

use crate::texture;
use crate::{BlockMesh, GetBlockMesh, GfxVertex, MeshOptions, SpaceMesh};
Expand Down Expand Up @@ -196,7 +196,7 @@ where
if duration > time::Duration::from_millis(4) {
log::trace!(
"Block mesh took {}: {:?} {:?}",
duration.custom_format(StatusText),
duration.refmt(&StatusText),
new_evaluated_block.attributes.display_name,
bd.block(),
);
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-mesh/src/dynamic/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ where
// if vertices == 0 {
// log::trace!(
// "meshed {:?}+ in {:.3} ms, 0",
// chunk_origin.custom_format(ConciseDebug),
// chunk_origin.refmt(&ConciseDebug),
// duration_ms,
// );
// } else {
// log::trace!(
// "meshed {:?}+ in {:.3} ms, {} in {:.3} µs/v",
// chunk_origin.custom_format(ConciseDebug),
// chunk_origin.refmt(&ConciseDebug),
// duration_ms,
// vertices,
// duration_ms * (1000.0 / vertices as f32),
Expand Down
16 changes: 7 additions & 9 deletions all-is-cubes-mesh/src/dynamic/chunked_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use all_is_cubes::math::{Cube, Face6, FreeCoordinate, GridCoordinate, LineVertex
use all_is_cubes::space::{BlockIndex, Space, SpaceChange};
use all_is_cubes::time::{self, Duration};
use all_is_cubes::universe::URef;
use all_is_cubes::util::{CustomFormat, StatusText, TimeStats};
use all_is_cubes::util::{Fmt, Refmt, StatusText, TimeStats};

use crate::dynamic::{self, ChunkMesh, ChunkTodo};
use crate::texture;
Expand Down Expand Up @@ -428,7 +428,7 @@ where
space = self.space().name(),
time = end_all_time
.saturating_duration_since(self.zero_time)
.custom_format(StatusText)
.refmt(&StatusText)
);
self.complete_time = Some(end_all_time);
}
Expand Down Expand Up @@ -513,8 +513,8 @@ pub struct CsmUpdateInfo {
pub chunk_total_cpu_byte_size: usize,
}

impl CustomFormat<StatusText> for CsmUpdateInfo {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, _: StatusText) -> fmt::Result {
impl Fmt<StatusText> for CsmUpdateInfo {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>, _: &StatusText) -> fmt::Result {
let CsmUpdateInfo {
flaws,
total_time: _,
Expand All @@ -539,14 +539,12 @@ impl CustomFormat<StatusText> for CsmUpdateInfo {
Mem: {chunk_mib} MiB for {chunk_count} chunks\
"},
flaws = flaws,
prep_time = prep_time.custom_format(StatusText),
prep_time = prep_time.refmt(&StatusText),
block_updates = block_updates,
chunk_scan_time = chunk_scan_time.custom_format(StatusText),
chunk_scan_time = chunk_scan_time.refmt(&StatusText),
chunk_mesh_generation_times = chunk_mesh_generation_times,
chunk_mesh_callback_times = chunk_mesh_callback_times,
depth_sort_time = depth_sort_time
.unwrap_or(Duration::ZERO)
.custom_format(StatusText),
depth_sort_time = depth_sort_time.unwrap_or(Duration::ZERO).refmt(&StatusText),
chunk_mib = chunk_total_cpu_byte_size / (1024 * 1024),
chunk_count = chunk_count,
)
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
Expand Up @@ -7,7 +7,7 @@ use all_is_cubes::block::{Evoxel, Evoxels};
use all_is_cubes::content::palette;
use all_is_cubes::euclid::Point3D;
use all_is_cubes::math::{Cube, GridAab};
use all_is_cubes::util::{ConciseDebug, CustomFormat};
use all_is_cubes::util::{ConciseDebug, Fmt};

#[cfg(doc)]
use all_is_cubes::math::GridArray;
Expand Down Expand Up @@ -252,8 +252,8 @@ impl Plane for NoTexture {
}
}

impl CustomFormat<ConciseDebug> for NoTexture {
fn fmt(&self, _: &mut fmt::Formatter<'_>, _: ConciseDebug) -> fmt::Result {
impl Fmt<ConciseDebug> for NoTexture {
fn fmt(&self, _: &mut fmt::Formatter<'_>, _: &ConciseDebug) -> fmt::Result {
match *self {}
}
}
Expand Down
Loading

0 comments on commit 964eab7

Please sign in to comment.