a-b-street/abstreet · error

0 dist ahead for slice

Error message

0 dist ahead for slice

What it means

PathV1::exact_slice slices a path step's polyline over a distance window. If dist_ahead is explicitly Some(0), there is no distance to slice, which the library treats as an error rather than returning an empty polyline. It guards callers from constructing degenerate PathV1 slices.

Solutions

  1. Skip the slice entirely when the remaining distance is zero (the step is fully consumed).
  2. Use `if d > Distance::ZERO` guards before calling exact_slice.
  3. Advance to the next PathStep instead of slicing the current one at zero length.
  4. If zero slices should be legal, change the caller to pass None and handle the empty case explicitly.

Example fix

// before
let slice = path.exact_slice(&map, dist_ahead)?; // dist_ahead may be 0
// after
if dist_ahead > Distance::ZERO {
    let slice = path.exact_slice(&map, dist_ahead)?;
}
Defensive patterns

Strategy: type-guard

Type guard

fn sliceable(d: Option<Distance>) -> bool {
    matches!(d, None) || matches!(d, Some(x) if x > Distance::ZERO)
}
// if sliceable(dist_ahead) { path.exact_slice(&map, dist_ahead)?; }

Try / catch

match path.exact_slice(&map, dist_ahead) {
    Err(e) if e.to_string() == "0 dist ahead for slice" => {
        // step fully consumed: advance to next PathStep instead
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling PathV1::exact_slice (or following a path step) with dist_ahead = Some(Distance::ZERO) — i.e. requesting a slice that starts exactly at the step's end with nothing ahead (map_model/src/pathfind/v1.rs:56).

Common situations: Simulation code computing "remaining distance" that hits exactly 0 at a step boundary; callers not filtering zero-length remainders before slicing.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of a-b-street/abstreet@0964f29315 (2026-09-13). Data as JSON: /api/errors/ecd58f8741276994. Report an issue: GitHub.

Appendix: source

Thrown at map_model/src/pathfind/v1.rs:56

    pub fn as_turn(&self) -> TurnID {
        self.as_traversable().as_turn()
    }

    // start is relative to the start of the actual geometry -- so from the lane's real start for
    // ContraflowLane.
    fn exact_slice(
        &self,
        map: &Map,
        start: Distance,
        dist_ahead: Option<Distance>,
    ) -> Result<PolyLine> {
        if let Some(d) = dist_ahead {
            if d < Distance::ZERO {
                panic!("Negative dist_ahead?! {}", d);
            }
            if d == Distance::ZERO {
                bail!("0 dist ahead for slice");
            }
        }

        match self {
            PathStep::Lane(id) => {
                let pts = &map.get_l(*id).lane_center_pts;
                if let Some(d) = dist_ahead {
                    pts.maybe_exact_slice(start, start + d)
                } else {
                    pts.maybe_exact_slice(start, pts.length())
                }
            }
            PathStep::ContraflowLane(id) => {
                let pts = map.get_l(*id).lane_center_pts.reversed();
                let reversed_start = pts.length() - start;
                if let Some(d) = dist_ahead {
                    pts.maybe_exact_slice(reversed_start, reversed_start + d)
                } else {

View on GitHub (pinned to 0964f29315)