Skip to content

Commit

Permalink
space: Rename SpaceBuilder to space::Builder.
Browse files Browse the repository at this point in the history
I plan to get rid of most or all prefixed names like this, preferring
the whole-module import style.
  • Loading branch information
kpreid committed Sep 30, 2024
1 parent fc806c2 commit f385eec
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 75 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
- Renamed `behavior::BehaviorHost` to `behavior::Host`.
- Renamed `behavior::BehaviorPersistence` to `behavior::Persistence`.

- Renamed `space::SpaceBuilder` to `space::Builder`.

- `all-is-cubes-gpu` library:
- `in_wgpu::SurfaceRenderer::new()` requires `wgpu::Adapter` instead of `&wgpu::Adapter`.

Expand Down
6 changes: 3 additions & 3 deletions all-is-cubes-content/src/city.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use all_is_cubes::math::{
GridSizeCoord, GridVector, Gridgid, VectorOps,
};
use all_is_cubes::op::Operation;
use all_is_cubes::space::{LightPhysics, Space, SpaceBuilder, SpacePhysics};
use all_is_cubes::space::{self, LightPhysics, Space, SpacePhysics};
use all_is_cubes::time::Instant;
use all_is_cubes::transaction::{self, Transaction};
use all_is_cubes::universe::Universe;
Expand Down Expand Up @@ -581,7 +581,7 @@ fn place_one_exhibit<I: Instant>(
// Install the signboard-and-text widgets in a space.
let info_sign_space = info_voxels_widget
.to_space(
SpaceBuilder::default().physics(SpacePhysics::DEFAULT_FOR_BLOCK),
space::Builder::default().physics(SpacePhysics::DEFAULT_FOR_BLOCK),
vui::Gravity::new(vui::Align::Center, vui::Align::Center, vui::Align::Low),
)
.unwrap();
Expand Down Expand Up @@ -832,7 +832,7 @@ fn draw_exhibit_info(exhibit: &Exhibit) -> Result<Space, InGenError> {

// TODO: give it a maximum size, instead of what we currently do which is truncating later
let space = info_widgets.to_space(
SpaceBuilder::default().physics(SpacePhysics::DEFAULT_FOR_BLOCK),
space::Builder::default().physics(SpacePhysics::DEFAULT_FOR_BLOCK),
vui::Gravity::new(vui::Align::Low, vui::Align::Low, vui::Align::Low),
)?;
Ok(space)
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-content/src/city/exhibits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use all_is_cubes::math::{
GridPoint, GridRotation, GridSize, GridVector, Gridgid, NotNan, Rgb, Rgba,
};
use all_is_cubes::op::Operation;
use all_is_cubes::space::{SetCubeError, Space, SpaceBuilder, SpacePhysics, SpaceTransaction};
use all_is_cubes::space::{self, SetCubeError, Space, SpacePhysics, SpaceTransaction};
use all_is_cubes::transaction::{self, Transaction as _};
use all_is_cubes::{color_block, include_image};

Expand Down Expand Up @@ -1503,7 +1503,7 @@ fn UI_PROGRESS_BAR(ctx: Context<'_>) {
});

let space = tree.to_space(
SpaceBuilder::default().physics(SpacePhysics::DEFAULT_FOR_BLOCK),
space::Builder::default().physics(SpacePhysics::DEFAULT_FOR_BLOCK),
vui::Gravity::new(vui::Align::Center, vui::Align::Low, vui::Align::Center),
)?;

Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-content/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn template_menu_widget_tree(

// TODO: the scaled-down logo should exist as a widget itself
let logo_text_space = vui::leaf_widget(logo_text()).to_space(
space::SpaceBuilder::default().physics(space::SpacePhysics::DEFAULT_FOR_BLOCK),
space::Builder::default().physics(space::SpacePhysics::DEFAULT_FOR_BLOCK),
Vector3D::new(Align::Center, Align::Center, Align::Low),
)?;
let logo_text_bounds = logo_text_space.bounds();
Expand Down
6 changes: 3 additions & 3 deletions all-is-cubes-ui/src/vui/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::fmt;

use all_is_cubes::euclid::{self, size3, Size3D, Vector3D};
use all_is_cubes::math::{Axis, Cube, Face6, FaceMap, GridAab, GridPoint, GridSize};
use all_is_cubes::space::{Space, SpaceBuilder, SpaceTransaction};
use all_is_cubes::space::{self, Space, SpaceTransaction};
use all_is_cubes::transaction::{self, Merge as _, Transaction as _};
use all_is_cubes::util::{ConciseDebug, Fmt};

Expand Down Expand Up @@ -432,9 +432,9 @@ impl LayoutTree<Arc<dyn Widget>> {
///
/// Note that the widgets will not actually appear as blocks until the first time the
/// space is stepped.
pub fn to_space<B: all_is_cubes::space::SpaceBuilderBounds>(
pub fn to_space<B: space::builder::Bounds>(
self: &Arc<Self>,
builder: SpaceBuilder<B>,
builder: space::Builder<B>,
gravity: Gravity,
) -> Result<Space, InstallVuiError> {
let mut space = builder
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-ui/src/vui/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use all_is_cubes::euclid::{size2, Size2D};
use all_is_cubes::math::{
Cube, Face6, FreeCoordinate, FreeVector, GridAab, GridCoordinate, GridSize, GridSizeCoord, Rgba,
};
use all_is_cubes::space::{self, Space, SpaceBuilder, SpacePhysics};
use all_is_cubes::space::{self, Space, SpacePhysics};
use all_is_cubes::time;
use all_is_cubes::universe::{Handle, Universe};
use all_is_cubes_render::camera::{self, ViewTransform};
Expand Down Expand Up @@ -305,7 +305,7 @@ pub(crate) mod parts {
large: &WidgetTree,
) -> Result<Arc<dyn Widget>, InstallVuiError> {
let space = large.to_space(
SpaceBuilder::default().physics(SpacePhysics::DEFAULT_FOR_BLOCK),
space::Builder::default().physics(SpacePhysics::DEFAULT_FOR_BLOCK),
Gravity::new(Align::Center, Align::Center, Align::Low),
)?;
Ok(Arc::new(widgets::Voxels::new(
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-ui/src/vui/widgets/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl vui::WidgetController for ProgressBarController {
#[cfg(test)]
mod tests {
use super::*;
use all_is_cubes::space::{SpaceBuilder, SpacePhysics};
use all_is_cubes::space::{self, SpacePhysics};
use all_is_cubes::transaction::Transaction as _;
use all_is_cubes::util::yield_progress_for_testing;
use all_is_cubes::{transaction, universe};
Expand All @@ -231,7 +231,7 @@ mod tests {
ListenableSource::constant(ProgressBarState::new(fraction)),
));

let mut space = SpaceBuilder::default()
let mut space = space::Builder::default()
.physics(SpacePhysics::DEFAULT_FOR_BLOCK)
.bounds(bounds)
.build();
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-ui/src/vui/widgets/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ mod tests {
use all_is_cubes::color_block;
use all_is_cubes::euclid::size3;
use all_is_cubes::math::{GridSizeCoord, Rgba};
use all_is_cubes::space::{SpaceBuilder, SpacePhysics};
use all_is_cubes::space::{self, SpacePhysics};

#[test]
fn large_text_size() {
Expand All @@ -276,7 +276,7 @@ mod tests {

// to_space() serves as a widget building sanity check. TODO: make a proper widget tester
tree.to_space(
SpaceBuilder::default().physics(SpacePhysics::DEFAULT_FOR_BLOCK),
space::Builder::default().physics(SpacePhysics::DEFAULT_FOR_BLOCK),
vui::Gravity::new(vui::Align::Center, vui::Align::Center, vui::Align::Low),
)
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes/src/block/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<P, Txn> BlockBuilder<P, Txn> {
B: Into<Cow<'a, Block>>,
{
// This is a worldgen convenience, not the most efficient possible path (which would be
// `SpaceBuilder::palette_and_contents()`), so save quite a lot of code generation
// `Builder::palette_and_contents()`), so save quite a lot of code generation
// by keeping it monomorphic and not inlined.
#[inline(never)]
fn voxels_fn_impl<'a>(
Expand All @@ -204,7 +204,7 @@ impl<P, Txn> BlockBuilder<P, Txn> {
let mut not_air_bounds: Option<GridAab> = None;

let mut space = Space::for_block(resolution).build();
// TODO: Teach the SpaceBuilder to accept a function in the same way?
// TODO: Teach the Space Builder to accept a function in the same way?
space.fill(space.bounds(), |cube| {
let block = function(cube);

Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes/src/content/load_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl embedded_graphics::geometry::OriginDimensions for &'_ PngAdapter<'_> {
///
/// The `block_function` will be memoized.
///
/// TODO: Allow `SpaceBuilder` controls somehow. Maybe this belongs as a method on SpaceBuilder.
/// TODO: Allow `space::Builder` controls somehow. Maybe this belongs as a method on it.
/// TODO: pixel_function should have a Result return
#[doc(hidden)] // still experimental API
#[inline(never)]
Expand Down
25 changes: 13 additions & 12 deletions all-is-cubes/src/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ use crate::transaction::{self, Merge, Transaction as _};
use crate::universe::{Handle, HandleVisitor, UniverseTransaction, VisitHandles};
use crate::util::{ConciseDebug, Refmt as _, StatusText, TimeStats};

mod builder;
#[allow(clippy::module_name_repetitions)] // TODO: rename to Builder?
pub use builder::{SpaceBuilder, SpaceBuilderBounds};
pub mod builder;
pub use builder::Builder;

mod light;
#[doc(hidden)] // pub only for visualization by all-is-cubes-gpu
Expand Down Expand Up @@ -113,24 +112,26 @@ impl fmt::Debug for Space {
pub type BlockIndex = u16;

impl Space {
/// Returns a [`SpaceBuilder`] configured for a block,
/// Returns a space [`Builder`] configured for a [recursive] block,
/// which may be used to construct a new [`Space`].
///
/// This means that its bounds are as per [`GridAab::for_block()`], and its
/// [`physics`](Self::physics) is [`SpacePhysics::DEFAULT_FOR_BLOCK`].
pub fn for_block(resolution: Resolution) -> SpaceBuilder<Vol<()>> {
SpaceBuilder::new()
///
/// [recursive]: crate::block::Primitive::Recur
pub fn for_block(resolution: Resolution) -> Builder<Vol<()>> {
Builder::new()
.bounds(GridAab::for_block(resolution))
.physics(SpacePhysics::DEFAULT_FOR_BLOCK)
}

/// Returns a [`SpaceBuilder`] with the given bounds and all default values,
/// Returns a [`space::Builder`](Builder) with the given bounds and all default values,
/// which may be used to construct a new [`Space`].
///
/// Panics if `bounds` has a volume exceeding `usize::MAX`.
/// (But there will likely be a memory allocation failure well below that point.)
pub fn builder(bounds: GridAab) -> SpaceBuilder<Vol<()>> {
SpaceBuilder::new().bounds(bounds)
pub fn builder(bounds: GridAab) -> Builder<Vol<()>> {
Builder::new().bounds(bounds)
}

/// Constructs a [`Space`] that is entirely filled with [`AIR`].
Expand All @@ -140,9 +141,9 @@ impl Space {
Space::builder(bounds).build()
}

/// Implementation of [`SpaceBuilder`]'s terminal methods.
fn new_from_builder(builder: SpaceBuilder<Vol<()>>) -> Self {
let SpaceBuilder {
/// Implementation of [`Builder`]'s terminal methods.
fn new_from_builder(builder: Builder<Vol<()>>) -> Self {
let Builder {
bounds,
spawn,
physics,
Expand Down
66 changes: 31 additions & 35 deletions all-is-cubes/src/space/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Lesser-used helpers for [`Builder`].
use alloc::boxed::Box;

use crate::behavior::BehaviorSet;
Expand All @@ -8,19 +10,16 @@ use crate::space::{
BlockIndex, GridAab, LightPhysics, PackedLight, Palette, PaletteError, Sky, Space, SpacePhysics,
};

/// Tool for constructing new [`Space`]s.
///
/// To create one, call [`Space::builder()`](Space::builder).
/// Builder of [`Space`]s.
///
/// TODO: Allow specifying behaviors.
/// To create one, call [`Space::builder()`](Space::builder) or [`Builder::default()`].
///
/// # Type parameters
///
/// * `B` is either `()` or `Vol<()>` according to whether the bounds have been specified.
#[derive(Clone, Debug)]
#[must_use]
#[allow(clippy::module_name_repetitions)]
pub struct SpaceBuilder<B> {
pub struct Builder<B> {
pub(super) bounds: B,
pub(super) spawn: Option<Spawn>,
pub(super) physics: SpacePhysics,
Expand All @@ -39,7 +38,7 @@ pub(super) enum Fill {
},
}

impl<B> SpaceBuilder<B> {
impl<B> Builder<B> {
/// Sets the [`Block`] that the space's volume will be filled with.
///
/// Calling this method will replace any previous specification of the contents,
Expand Down Expand Up @@ -92,16 +91,16 @@ impl<B> SpaceBuilder<B> {
}
}

impl<B: SpaceBuilderBounds> SpaceBuilder<B> {
impl<B: Bounds> Builder<B> {
/// Set the bounds unless they have already been set.
pub fn bounds_if_not_set(self, bounds_fn: impl FnOnce() -> GridAab) -> SpaceBuilder<Vol<()>> {
pub fn bounds_if_not_set(self, bounds_fn: impl FnOnce() -> GridAab) -> Builder<Vol<()>> {
// Delegate to the trait. (This method exists so the trait need not be imported.)
SpaceBuilderBounds::bounds_if_not_set(self, bounds_fn)
Bounds::bounds_if_not_set(self, bounds_fn)
}
}

impl SpaceBuilder<()> {
/// Use [`SpaceBuilder::default()`] as the public way to call this.
impl Builder<()> {
/// Use [`Builder::default()`] as the public way to call this.
pub(super) fn new() -> Self {
Self {
bounds: (),
Expand All @@ -116,8 +115,8 @@ impl SpaceBuilder<()> {
///
/// Panics if `bounds` has a volume exceeding `usize::MAX`.
/// (But there will likely be a memory allocation failure well below that point.)
pub fn bounds(self, bounds: GridAab) -> SpaceBuilder<Vol<()>> {
SpaceBuilder {
pub fn bounds(self, bounds: GridAab) -> Builder<Vol<()>> {
Builder {
bounds: bounds.to_vol().unwrap(),
spawn: self.spawn,
physics: self.physics,
Expand All @@ -127,7 +126,7 @@ impl SpaceBuilder<()> {
}
}

impl SpaceBuilder<Vol<()>> {
impl Builder<Vol<()>> {
/// Sets the default spawn location of new characters.
///
/// Panics if any of the given coordinates is infinite or NaN.
Expand Down Expand Up @@ -238,52 +237,52 @@ impl SpaceBuilder<Vol<()>> {
}
}

impl Default for SpaceBuilder<()> {
impl Default for Builder<()> {
fn default() -> Self {
Self::new()
}
}

/// Helper for [`SpaceBuilder::bounds_if_not_set()`]. Do not call or implement this trait.
pub trait SpaceBuilderBounds: sbb::SbbSealed + Sized {
/// Helper for [`Builder::bounds_if_not_set()`]. Do not call or implement this trait.
pub trait Bounds: sealed::Sealed + Sized {
/// Set the bounds unless they have already been set.
///
/// This function is an implementation detail; call
/// [`SpaceBuilder::bounds_if_not_set()`] instead.
/// [`Builder::bounds_if_not_set()`] instead.
#[doc(hidden)]
fn bounds_if_not_set(
builder: SpaceBuilder<Self>,
builder: Builder<Self>,
bounds_fn: impl FnOnce() -> GridAab,
) -> SpaceBuilder<Vol<()>>;
) -> Builder<Vol<()>>;
}

impl SpaceBuilderBounds for () {
impl Bounds for () {
fn bounds_if_not_set(
builder: SpaceBuilder<Self>,
builder: Builder<Self>,
bounds_fn: impl FnOnce() -> GridAab,
) -> SpaceBuilder<Vol<()>> {
) -> Builder<Vol<()>> {
builder.bounds(bounds_fn())
}
}

impl SpaceBuilderBounds for Vol<()> {
impl Bounds for Vol<()> {
fn bounds_if_not_set(
builder: SpaceBuilder<Self>,
builder: Builder<Self>,
_bounds_fn: impl FnOnce() -> GridAab,
) -> SpaceBuilder<Vol<()>> {
) -> Builder<Vol<()>> {
builder
}
}

/// Module for sealed trait
mod sbb {
mod sealed {
#![expect(clippy::module_name_repetitions)]
use super::*;
#[doc(hidden)]
#[expect(unnameable_types)]
pub trait SbbSealed {}
impl SbbSealed for () {}
impl SbbSealed for Vol<()> {}
pub trait Sealed {}
impl Sealed for () {}
impl Sealed for Vol<()> {}
}

#[cfg(feature = "arbitrary")]
Expand Down Expand Up @@ -363,10 +362,7 @@ mod tests {
fn bounds_if_not_set_when_not_set() {
let bounds = GridAab::from_lower_size([1, 2, 3], [1, 1, 1]);
assert_eq!(
SpaceBuilder::new()
.bounds_if_not_set(|| bounds)
.build()
.bounds(),
Builder::new().bounds_if_not_set(|| bounds).build().bounds(),
bounds
);
}
Expand Down
Loading

0 comments on commit f385eec

Please sign in to comment.