Skip to content

Commit

Permalink
block: Rename BlockBuilder to block::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 f385eec commit 884ff98
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Renamed `behavior::BehaviorHost` to `behavior::Host`.
- Renamed `behavior::BehaviorPersistence` to `behavior::Persistence`.

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

- `all-is-cubes-gpu` library:
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-ui/src/vui/widgets/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use exhaust::Exhaust;
use all_is_cubes::arcstr::ArcStr;
use all_is_cubes::behavior::BehaviorSetTransaction;
use all_is_cubes::block::{
self, Block, BlockBuilder,
self, Block, Builder,
Resolution::{self, *},
};
use all_is_cubes::color_block;
Expand Down Expand Up @@ -650,7 +650,7 @@ impl ButtonBase for ToggleButtonVisualState {
fn button_block(&self, txn: &mut UniverseTransaction) -> Result<Block, InGenError> {
let label_z = self.button_label_z();
let active = self.value;
let illuminate = move |builder: BlockBuilder<block::builder::BlockBuilderAtom, ()>| {
let illuminate = move |builder: Builder<block::builder::Atom, ()>| {
if active {
builder.light_emission(palette::BUTTON_ACTIVATED_GLOW)
} else {
Expand Down
9 changes: 4 additions & 5 deletions all-is-cubes/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ pub use block_def::*;

pub mod builder;
#[doc(inline)]
#[expect(clippy::module_name_repetitions)] // TODO: rename to Builder?
pub use builder::BlockBuilder;
pub use builder::Builder;

mod eval;
pub use eval::*;
Expand Down Expand Up @@ -319,10 +318,10 @@ impl fmt::Debug for Block {
}

impl Block {
/// Returns a new [`BlockBuilder`] which may be used to construct a [`Block`] value
/// Returns a new [`Builder`] which may be used to construct a [`Block`] value
/// from various inputs with convenient syntax.
pub const fn builder() -> BlockBuilder<builder::NeedsPrimitive, ()> {
BlockBuilder::new()
pub const fn builder() -> Builder<builder::NeedsPrimitive, ()> {
Builder::new()
}

/// Construct a [`Block`] from a [`Primitive`] value.
Expand Down
6 changes: 2 additions & 4 deletions all-is-cubes/src/block/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
/// `BlockAttributes::default()` will produce a reasonable set of defaults for “ordinary”
/// blocks.
#[derive(Clone, Eq, Hash, PartialEq)]
#[expect(clippy::exhaustive_structs)] // TODO: Make this non_exhaustive but give users a way to construct it easily, possibly via BlockBuilder.
#[expect(clippy::exhaustive_structs)] // TODO: Make this non_exhaustive but give users a way to construct it easily, possibly via block::Builder.
pub struct BlockAttributes {
/// The name that should be displayed to players.
///
Expand Down Expand Up @@ -66,9 +66,7 @@ pub struct BlockAttributes {
/// contain voxels with animation hints themselves.
pub animation_hint: AnimationHint,
//
// Reminder: When adding new fields, add them to BlockBuilder too.
//
// TODO: add 'behavior' functionality, if we don't come up with something else
// Reminder: When adding new fields, add them to block::Builder too.
}

impl fmt::Debug for BlockAttributes {
Expand Down
90 changes: 48 additions & 42 deletions all-is-cubes/src/block/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Lesser-used helpers for [`BlockBuilder`].
//! Support for [`Builder`].
use alloc::borrow::Cow;
use alloc::sync::Arc;
Expand All @@ -7,7 +7,7 @@ use alloc::vec::Vec;
use arcstr::ArcStr;

use crate::block::{
AnimationHint, Atom, Block, BlockAttributes, BlockCollision, BlockParts, BlockPtr, Modifier,
self, AnimationHint, Block, BlockAttributes, BlockCollision, BlockParts, BlockPtr, Modifier,
Primitive, Resolution, RotationPlacementRule, AIR,
};
use crate::inv;
Expand All @@ -19,7 +19,7 @@ use crate::universe::{Handle, Name, Universe, UniverseTransaction};
/// Tool for constructing [`Block`] values conveniently.
///
/// To create one, call [`Block::builder()`].
/// ([`BlockBuilder::default()`] is also available.)
/// ([`Builder::default()`] is also available.)
///
/// ```
/// use all_is_cubes::block::{Block, EvaluatedBlock};
Expand All @@ -34,10 +34,15 @@ use crate::universe::{Handle, Name, Universe, UniverseTransaction};
/// assert_eq!(evaluated.color(), Rgba::new(0.5, 0.5, 0., 1.));
/// assert_eq!(evaluated.attributes().display_name.as_str(), "BROWN");
/// ```
///
/// # Type parameters
///
/// * `P` is a type corresponding to the type of [`Primitive`] that is being built.
/// * `Txn` is [`UniverseTransaction`] if the block builder is also building a transaction
/// that must be executed, and [`()`] otherwise.
#[derive(Clone, Debug, Eq, PartialEq)]
#[must_use]
#[expect(clippy::module_name_repetitions)] // TODO: rename to Builder?
pub struct BlockBuilder<P, Txn> {
pub struct Builder<P, Txn> {
attributes: BlockAttributes,
primitive_builder: P,
modifiers: Vec<Modifier>,
Expand All @@ -47,16 +52,16 @@ pub struct BlockBuilder<P, Txn> {
transaction: Txn,
}

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

impl BlockBuilder<NeedsPrimitive, ()> {
impl Builder<NeedsPrimitive, ()> {
/// Common implementation of [`Block::builder`] and [`Default::default`]; use one of those to call this.
pub(super) const fn new() -> Self {
BlockBuilder {
Builder {
attributes: BlockAttributes::default(),
primitive_builder: NeedsPrimitive,
modifiers: Vec::new(),
Expand All @@ -65,7 +70,7 @@ impl BlockBuilder<NeedsPrimitive, ()> {
}
}

impl<P, Txn> BlockBuilder<P, Txn> {
impl<P, Txn> Builder<P, Txn> {
// TODO: When #![feature(const_precise_live_drops)] becomes stable, we can make
// this builder mostly usable in const contexts.
// https://github.com/rust-lang/rust/issues/73255
Expand Down Expand Up @@ -132,10 +137,10 @@ impl<P, Txn> BlockBuilder<P, Txn> {
/// Sets the color value for building a [`Primitive::Atom`].
///
/// This will replace any previous color **or voxels.**
pub fn color(self, color: impl Into<Rgba>) -> BlockBuilder<BlockBuilderAtom, ()> {
BlockBuilder {
pub fn color(self, color: impl Into<Rgba>) -> Builder<Atom, ()> {
Builder {
attributes: self.attributes,
primitive_builder: BlockBuilderAtom {
primitive_builder: Atom {
color: color.into(),
emission: Rgb::ZERO,
collision: BlockCollision::Hard,
Expand All @@ -155,10 +160,10 @@ impl<P, Txn> BlockBuilder<P, Txn> {
self,
resolution: Resolution,
space: Handle<Space>,
) -> BlockBuilder<BlockBuilderVoxels, ()> {
BlockBuilder {
) -> Builder<Voxels, ()> {
Builder {
attributes: self.attributes,
primitive_builder: BlockBuilderVoxels {
primitive_builder: Voxels {
space,
resolution,
offset: GridPoint::origin(),
Expand Down Expand Up @@ -186,7 +191,7 @@ impl<P, Txn> BlockBuilder<P, Txn> {
// TODO: Maybe resolution should be a separate method? Check usage patterns later.
resolution: Resolution,
mut function: F,
) -> Result<BlockBuilder<BlockBuilderVoxels, UniverseTransaction>, SetCubeError>
) -> Result<Builder<Voxels, UniverseTransaction>, SetCubeError>
where
F: FnMut(Cube) -> B,
B: Into<Cow<'a, Block>>,
Expand All @@ -200,7 +205,7 @@ impl<P, Txn> BlockBuilder<P, Txn> {
modifiers: Vec<Modifier>,
resolution: Resolution,
function: &mut dyn FnMut(Cube) -> Cow<'a, Block>,
) -> Result<BlockBuilder<BlockBuilderVoxels, UniverseTransaction>, SetCubeError> {
) -> Result<Builder<Voxels, UniverseTransaction>, SetCubeError> {
let mut not_air_bounds: Option<GridAab> = None;

let mut space = Space::for_block(resolution).build();
Expand Down Expand Up @@ -237,9 +242,9 @@ impl<P, Txn> BlockBuilder<P, Txn> {

let space_handle = Handle::new_pending(Name::Pending, space);

Ok(BlockBuilder {
Ok(Builder {
attributes,
primitive_builder: BlockBuilderVoxels {
primitive_builder: Voxels {
space: space_handle.clone(),
resolution,
offset: GridPoint::origin(),
Expand Down Expand Up @@ -284,7 +289,7 @@ impl<P, Txn> BlockBuilder<P, Txn> {
}
}

impl<P: BuildPrimitive> BlockBuilder<P, ()> {
impl<P: BuildPrimitive> Builder<P, ()> {
/// Converts this builder into a block value.
///
/// This method may only be used when the builder has *not* been used with `voxels_fn()`,
Expand All @@ -295,7 +300,7 @@ impl<P: BuildPrimitive> BlockBuilder<P, ()> {
}
}

impl<P: BuildPrimitive> BlockBuilder<P, UniverseTransaction> {
impl<P: BuildPrimitive> Builder<P, UniverseTransaction> {
// TODO: Also allow extracting the transaction for later use

/// Converts this builder into a block value, and inserts its associated [`Space`] into the
Expand All @@ -321,7 +326,7 @@ impl<P: BuildPrimitive> BlockBuilder<P, UniverseTransaction> {
}

/// Atom-specific builder methods.
impl<Txn> BlockBuilder<BlockBuilderAtom, Txn> {
impl<Txn> Builder<Atom, Txn> {
/// Sets the collision behavior of a [`Primitive::Atom`] block.
pub const fn collision(mut self, collision: BlockCollision) -> Self {
self.primitive_builder.collision = collision;
Expand All @@ -344,7 +349,7 @@ impl<Txn> BlockBuilder<BlockBuilderAtom, Txn> {
}

/// Voxel-specific builder methods.
impl<Txn> BlockBuilder<BlockBuilderVoxels, Txn> {
impl<Txn> Builder<Voxels, Txn> {
/// Sets the coordinate offset for building a [`Primitive::Recur`]:
/// the lower-bound corner of the region of the [`Space`]
/// which will be used for block voxels. The default is zero.
Expand All @@ -357,32 +362,32 @@ impl<Txn> BlockBuilder<BlockBuilderVoxels, Txn> {
// and "add offset", but don't add those until use cases are seen.
}

/// Allows implicitly converting `BlockBuilder` to the block it would build.
impl<C: BuildPrimitive> From<BlockBuilder<C, ()>> for Block {
fn from(builder: BlockBuilder<C, ()>) -> Self {
/// Allows implicitly converting [`Builder`] to the block it would build.
impl<C: BuildPrimitive> From<Builder<C, ()>> for Block {
fn from(builder: Builder<C, ()>) -> Self {
builder.build()
}
}
/// Equivalent to `Block::builder().color(color)`.
impl From<Rgba> for BlockBuilder<BlockBuilderAtom, ()> {
impl From<Rgba> for Builder<Atom, ()> {
fn from(color: Rgba) -> Self {
Block::builder().color(color)
}
}
/// Equivalent to `Block::builder().color(color.with_alpha_one())`.
impl From<Rgb> for BlockBuilder<BlockBuilderAtom, ()> {
impl From<Rgb> for Builder<Atom, ()> {
fn from(color: Rgb) -> Self {
Block::builder().color(color.with_alpha_one())
}
}

/// Placeholder type for an incomplete [`BlockBuilder`]'s content. The builder
/// Placeholder type for an incomplete [`Builder`]'s content. The builder
/// cannot create an actual block until this is replaced.
#[expect(clippy::exhaustive_structs)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct NeedsPrimitive;

/// Something that a parameterized [`BlockBuilder`] can use to construct a block's primitive.
/// Something that a parameterized [`Builder`] can use to construct a block's primitive.
///
/// TODO: This is not currently necessary; we can replace the BuildPrimitive types with the
/// primitive itself. (But will that remain true?)
Expand All @@ -391,32 +396,33 @@ pub trait BuildPrimitive {
fn build_primitive(self) -> Primitive;
}

/// Parameter type for [`BlockBuilder::color`], building [`Primitive::Atom`].
/// Parameter type for a [`Builder`] that is building a block with a [`Primitive::Atom`].
///
/// This is not the same as the [`block::Atom`] type.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct BlockBuilderAtom {
pub struct Atom {
color: Rgba,
emission: Rgb,
collision: BlockCollision,
}
impl BuildPrimitive for BlockBuilderAtom {
impl BuildPrimitive for Atom {
fn build_primitive(self) -> Primitive {
Primitive::Atom(Atom {
Primitive::Atom(block::Atom {
color: self.color,
emission: self.emission,
collision: self.collision,
})
}
}

/// Parameter type for a [`BlockBuilder`] that is building a block with voxels
/// ([`Primitive::Recur`]).
/// Parameter type for a [`Builder`] that is building a block with voxels ([`Primitive::Recur`]).
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct BlockBuilderVoxels {
pub struct Voxels {
space: Handle<Space>,
resolution: Resolution,
offset: GridPoint,
}
impl BuildPrimitive for BlockBuilderVoxels {
impl BuildPrimitive for Voxels {
fn build_primitive(self) -> Primitive {
Primitive::Recur {
offset: self.offset,
Expand Down Expand Up @@ -444,7 +450,7 @@ mod tests {
let color = Rgba::new(0.1, 0.2, 0.3, 0.4);
assert_eq!(
Block::builder().color(color).build(),
Block::from(Atom {
Block::from(block::Atom {
color,
emission: Rgb::ZERO,
collision: BlockCollision::Hard,
Expand All @@ -455,8 +461,8 @@ mod tests {
#[test]
fn default_equivalent() {
assert_eq!(
BlockBuilder::new(),
<BlockBuilder<NeedsPrimitive, ()> as Default>::default()
Builder::new(),
<Builder<NeedsPrimitive, ()> as Default>::default()
);
}

Expand All @@ -482,7 +488,7 @@ mod tests {
.animation_hint(AnimationHint::replacement(block::AnimationChange::Shape))
.modifier(Modifier::Rotate(GridRotation::CLOCKWISE))
.build(),
Block::from(Atom {
Block::from(block::Atom {
color,
emission,
collision: BlockCollision::None,
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes/src/block/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ mod eval {
let resolution = R4;
let mut universe = Universe::new();
let mut space = Space::empty(GridAab::for_block(resolution));
// TODO: BlockBuilder should support constructing indirects (by default, even)
// TODO: block::Builder should support constructing indirects (by default, even)
// and we can use the more concise version
space
.fill(space.bounds(), |point| {
Expand Down
3 changes: 1 addition & 2 deletions all-is-cubes/src/space/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,8 @@ impl Bounds for Vol<()> {
}
}

/// Module for sealed trait
/// Module for [`Bounds`] sealed trait
mod sealed {
#![expect(clippy::module_name_repetitions)]
use super::*;
#[doc(hidden)]
#[expect(unnameable_types)]
Expand Down

0 comments on commit 884ff98

Please sign in to comment.