2.1 KiB
2.1 KiB
Task 3: Grid
Depends on: Task 1, Task 2
Context Files
.dev/2026-05-28-procgen-map-core/spec/design.md— §4.2 (Grid).reikhelm-core/src/geometry.rs—Point,Rect(indexing + region iteration).reikhelm-core/src/lib.rs— append the module declaration here.
Files
- Create:
reikhelm-core/src/grid.rs— genericGrid<T>. - Modify:
reikhelm-core/src/lib.rs— appendpub mod grid;.
What to Build
A generic, bounds-safe 2D grid (spec §4.2) backed by a flat Vec<T> in row-major order. Generic on purpose: it's the map (Grid<Tile>) and also scratch buffers (Grid<u32>, Grid<f32>) during generation. All access is bounds-checked — out-of-bounds reads return None, out-of-bounds writes are no-ops (never panic). Neighbor queries return only in-bounds points. Derive Clone, Debug; derive Serialize, Deserialize gated on T: Serialize/Deserialize.
Interface Dependencies
- Consumes:
Point,Rectfrom Task 2. - Produces:
Grid<T>with:new(width: u32, height: u32, fill: T) -> Self where T: Clonefrom_fn(width, height, f: impl Fn(Point) -> T) -> Selfwidth() -> u32,height() -> u32in_bounds(Point) -> boolget(Point) -> Option<&T>,get_mut(Point) -> Option<&mut T>,set(Point, T)(no-op if OOB)neighbors4(Point) -> impl Iterator<Item = Point>,neighbors8(...)— in-bounds onlyiter() -> impl Iterator<Item = (Point, &T)>iter_rect(Rect) -> impl Iterator<Item = (Point, &T)>(clipped to bounds)
Tests
get/setround-trip for in-bounds points;setOOB is a no-op andgetOOB isNone.neighbors4/neighbors8at a corner return only in-bounds neighbors (3 for corner with 8-conn).itervisits exactlywidth * heightcells once each.iter_rectis clipped when the rect extends past the grid edge.from_fnproduces values matching the closure at sampled points.
Success Criteria
Grid<T>exists with all listed methods; OOB access never panics.pub mod grid;added tolib.rs.- All tests pass:
cargo test -p reikhelm-core grid
Commit
"feat(core): generic bounds-safe Grid<T>"