Introducing a new way to review your PRs.

Read the announcement

maplibre / maplibre-tile-spec

Next generation map vector tiles format

View on GitHub

20 documents · Updated Nov 23, 2026

docs / rust-core-codecs

Rust core codecs

The Rust core crate is the shared tile engine for the Rust workspace. It models tile layers, encodes them as MLT, decodes MLT back into feature data, and provides MVT and GeoJSON exchange. It also defines the checks, tests, fuzz targets, synthetic inputs, and measurements that protect format safety and codec speed. See the Rust tile workspace, native bindings, Wasm package, and cross-language validation pages for its wider use.

Core codec role

The core codec separates the logical tile model from the binary MLT form. This lets encoders choose compact storage without changing the feature data used by converters and language-facing packages.

Shared tile model

The shared tile model is the row-oriented form used to build and exchange layers. A layer owns its name, extent, feature order, geometry, IDs, and typed property columns. With the v2 feature enabled, it can also hold nested values, vertex or triangle m-values, mesh triangles, and z coordinates. This model gives all codec paths one meaning for a feature even when their wire layouts differ. rust/…/tile.rs• L51-70

References: rust/…/tile.rs• L51-70 rust/…/tile.rs• L519-579 rust/…/tile.rs• L633-688 rust/…/tile.rs• L725-795 rust/…/lib.rs• L47-75

Wire versions

The crate supports the v1 layer format as the broad compatibility path. The unstable v2 path adds newer layer features and a more compact envelope while decoding into the same shared core view. A v1 encode rejects data that v1 cannot represent instead of silently dropping it. rust/…/encode01.rs• L12-43

Wire versionPurposev2-only capabilities
v1, tag 0x01Compatibility for common geometry, IDs, scalar values, strings, and dictionariesNone
v2, tag 0x02Newer MLT layouts and richer tile dataNested values, m-values, z coordinates, meshes, and newer stream choices

V1 identifies stream roles in its headers. V2 derives roles from stream position and uses context for counts when possible. The two versions therefore share stream meanings while keeping different envelope rules. rust/…/root02.rs• L1-46

References: rust/…/encode01.rs• L12-43 rust/…/encode02.rs• L1-16 rust/…/root02.rs• L1-46 rust/…/model02.rs• L14-30

Decode behavior

Decoding is split into parsing, column decoding, and row materialization. This allows tools to inspect a tile's structure without expanding every value, while feature consumers can request a complete row-oriented layer.

Layer parsing and access

The parser checks the layer envelope and keeps column data close to the input bytes. Columns can remain lazy until a caller needs them. Full decoding produces typed columns and geometry, and into_tile then joins those columns into owned features. V2 adds nested values, m-values, z values, and mesh triangles while it performs this materialization. rust/…/into_tile.rs• L34-81

The main decode flow is:

flowchart LR
    B[MLT bytes] --> P[Parser]
    P --> L[Lazy layer and raw streams]
    L --> D[Typed column and geometry decode]
    D --> R[Row-oriented TileLayer]
    L --> I[Column and stream inspection]

References: rust/…/lazy_state.rs• L51-105 rust/…/root02.rs• L105-254 rust/…/into_tile.rs• L34-81 rust/…/into_tile.rs• L92-132

Geometry reconstruction

The geometry model stores feature types, offset levels, vertices, and optional triangle data. The decoder uses the available topology levels to rebuild points, lines, polygons, and multi-geometries. It closes polygon rings for client-facing geometry because the MLT form stores the closing vertex only once. rust/…/model.rs• L11-75

V2 can store a shared vertex dictionary, tessellation trees, and a mesh layout. Mesh layers use (x, y, z) vertices and triangle indexes. The mesh layout requires z data and represents every feature as a mesh. Triangle-only storage is valid for polygon layers when outline topology is not needed by the consumer. rust/…/geotype.rs• L119-120

References: rust/…/model.rs• L17-75 rust/…/decode.rs• L31-64 rust/…/geotype.rs• L119-120 rust/…/geotype.rs• L391-480 rust/…/tri.rs• L234-268

V2 attributes

V2 m-values are columns whose values run over the vertices of present features. Mesh layers may also hold one value per triangle. The decoder uses geometry counts to split flat m-value data into feature runs and rejects a column whose total does not match those counts. rust/…/mvalue.rs• L51-76

Nested values preserve map and list shape as a tree. Z values stay attached to their vertices and use a layer-wide step and height mode. The row materializer adds these v2 fields to the common feature model after it decodes the shared columns. rust/…/into_tile.rs• L92-132

References: rust/…/mvalue.rs• L221-295 rust/…/nested.rs• L25-99 rust/…/into_tile.rs• L92-132 rust/…/tile.rs• L51-228 rust/…/tile.rs• L885-949

Feature iteration

Fully decoded v1 columns expose a lending feature iterator. A lending iterator returns a feature view that borrows reused geometry and value buffers, so callers can scan a layer without allocating a new row for each feature. The owned conversion path remains available when callers need data that outlives the scan. rust/…/iterators.rs• L1-17

References: rust/…/iterators.rs• L29-95 rust/…/iterators.rs• L487-569 rust/…/into_tile.rs• L41-79

Encode behavior

Encoding turns a layer source into a column-oriented staged form and then writes the selected MLT version. The automatic path can change stream layouts, feature order, vertex order, and polygon tessellation because all parallel feature data moves together.

Encoding pipeline

The encoder first gathers facts about values and geometry. It then stages IDs, geometry, properties, nested values, m-values, and z data in one feature order. Candidate layouts are encoded and compared, and the best result is returned as a complete layer record. rust/…/tile.rs• L88-185

flowchart LR
    S[TileLayer or LayerWriter] --> A[Layer analysis]
    A --> O[Order and geometry candidates]
    O --> T[Columnar staging]
    T --> E[Stream encoding]
    E --> C[Size and client cost]
    C --> W[Selected MLT layer]

References: rust/…/source.rs• L17-73 rust/…/tile.rs• L88-185 rust/…/optimizer.rs• L119-204 rust/…/stager.rs• L11-22

Stream and string choices

The encoder selects a stream form that fits the value pattern and active profile. Integer data can use transforms such as delta, run-length, bit packing, or FastPFOR. Strings can use plain, dictionary, FSST, or combined layouts. V2 also supports front-coded dictionaries, compact presence masks, float choices, and additional vertex layouts. rust/…/header02.rs• L75-110

The low-level readers enforce the same contract as the writers. Bit-packed widths and payload sizes must agree. Front-coded dictionaries must have paired lengths and valid prefixes. FSST tables and corpora must stay within their declared bounds. Varints must use their canonical length. rust/…/bitpack.rs• L52-87

The encoder keeps v1 as a strict target. V2-only columns and layouts produce an explicit error when a v1 request cannot preserve their meaning. rust/…/encode01.rs• L18-43

References: rust/…/header02.rs• L144-218 rust/…/model.rs• L85-142 rust/…/bitpack.rs• L13-87 rust/…/front_coding.rs• L37-149 rust/…/fsst.rs• L23-121 rust/…/varint.rs• L6-77 rust/…/errors.rs• L164-215

Feature order and geometry choices

The default order preserves source order. Optional ID, Morton, and Hilbert orders can improve locality and repeated-value patterns. Vertex sorting can also start each feature near the prior feature's final vertex. Sorting changes geometry, IDs, properties, m-values, nested values, and z values as one logical row. rust/…/stager.rs• L110-145

Geometry can keep polygon triangles to reduce client work, or omit them to reduce tile size. V2 mesh encoding may reorder vertices and triangles for compact triangle trees. The encoder returns the order needed to move vertex- and triangle-scoped attributes with the same data. rust/…/encode02.rs• L257-374

References: rust/…/sort.rs• L9-116 rust/…/tile.rs• L99-138 rust/…/stager.rs• L191-260 rust/…/encode02.rs• L312-374 rust/…/mvalue.rs• L9-23

Direct layer writing

LayerWriter supports callers that receive features one at a time. It keeps borrowed values in column buffers and sends the same logical source through the shared encoder choices as TileLayer. It now supports nested v2 columns as well as m-values. An unfinished feature is discarded, and m-value runs are checked against feature geometry before they become part of the layer. rust/…/layer_writer.rs• L24-61

References: rust/…/layer_writer.rs• L24-61 rust/…/layer_writer.rs• L178-315 rust/…/layer_writer_m_values.rs• L69-84 rust/…/layer_writer_nested.rs• L36-75

Exchange formats

The core crate provides conversion at the row-model boundary. MVT supports common vector-tile content, while GeoJSON gives a readable representation of decoded layers and v2 fields.

MVT exchange

MVT input becomes a row-oriented layer after the decoder infers each property column from the values seen in that layer. MVT output writes layer names, extents, IDs, geometry, and present scalar properties. MVT has no matching form for nested values, m-values, z metadata, or meshes, so those features cannot pass through MVT unchanged. Mesh output is rejected rather than silently flattening the layer. rust/…/encode.rs• L22-43

References: rust/…/decode.rs• L15-21 rust/…/decode.rs• L55-136 rust/…/encode.rs• L11-43 rust/…/errors.rs• L200-205

GeoJSON exchange

GeoJSON presents each decoded feature with its geometry, ID, and properties. V2 m-values and nested values become properties. Z values become the third coordinate in each stored position, and polygon closing positions repeat the first coordinate and its z value. Mesh triangles use a foreign triangles member. The converter validates coordinate shape and preserves special float values through its JSON representation. rust/…/geojson.rs• L30-109

References: rust/…/geojson.rs• L30-115 rust/…/geojson.rs• L126-198 rust/…/geojson.rs• L218-252 rust/…/geojson02.rs• L25-67

Validation boundaries

Validation protects both the logical layer contract and the binary boundary. Errors are returned before invalid counts, names, shapes, or version combinations can create a shifted or ambiguous tile.

Model validation

The row model uses one name space for ordinary properties, m-values, and nested columns. Each feature must match the layer schema. Values must use the declared kind, m-values must match their vertex or triangle scope, nested values must match their shape, and z values must match the layer's vertex count. A mesh layer must contain only mesh features and must define z data. rust/…/tile.rs• L874-949

References: rust/…/tile.rs• L612-630 rust/…/tile.rs• L760-834 rust/…/tile.rs• L874-949 rust/…/errors.rs• L34-65 rust/…/errors.rs• L113-160

Wire validation

Parsing checks lengths, counts, presence data, dictionary indexes, geometry topology, stream context, and trailing bytes. V2 also checks that its header, geometry layout, count context, and m-value scope agree. Unknown layer tags remain visible as unknown data instead of being treated as valid known layers. rust/…/root02.rs• L75-121

Front-coded dictionaries, FSST data, bit-packed streams, presence masks, and triangle trees have their own structural checks. These checks reject malformed prefixes, suffix lengths, symbol tables, run lengths, widths, cycles, and out-of-range vertex indexes before reconstruction returns data. rust/…/front_coding.rs• L63-149

References: rust/…/root02.rs• L131-254 rust/…/root02.rs• L349-465 rust/…/header02.rs• L222-275 rust/…/bitpack.rs• L52-87 rust/…/front_coding.rs• L97-149 rust/…/fsst.rs• L58-121 rust/…/presence_coding.rs• L187-255 rust/…/tri.rs• L237-268 rust/…/varint.rs• L6-28 rust/…/errors.rs• L344-414

Memory budgets

Parser and Decoder enforce a per-tile memory budget before they reserve decoded storage. The default budget is 64 MiB, and callers can set another limit. The decoder reuses physical buffers and charges copied strings and rebuilt rows as well as stream buffers. A budget reset is safe only after earlier decoded values are no longer live. rust/…/limits.rs• L6-24

V2 m-value decoding avoids keeping one span object for every feature and column. It walks each column alongside feature geometry, which keeps sparse or wide m-value layers within the same budget model. rust/…/into_tile.rs• L102-121

References: rust/…/limits.rs• L28-120 rust/…/limits.rs• L124-203 rust/…/mvalue.rs• L204-239 rust/…/into_tile.rs• L102-121 rust/…/string_copy_budget.rs• L18-90

Testing and synthetic data

The core crate uses unit, integration, property, snapshot, differential, and fuzz tests because format correctness has several separate dimensions. Tests compare logical content as well as wire structure, so a smaller tile is not accepted if it changes feature meaning.

Compatibility tests

Round-trip tests encode and decode the same logical layer through both wire versions where possible. V2 tests also check meshes, triangle-scoped and vertex-scoped m-values, nested shapes, z coordinates, vertex sorting, dump coverage, and GeoJSON output. Fixture tests compare v1 and v2 decoded layers and skip only source layouts that v2 cannot represent. rust/…/tag02_roundtrip.rs• L49-67

Property tests extend this coverage across presence patterns, string encodings, shared dictionaries, packed streams, and generated property columns. The test manifest gates these assets by feature, so v2, private inspection, and benchmark checks remain explicit instead of becoming part of the default public build. rust/…/Cargo.toml• L103-186

References: rust/…/tag02_roundtrip.rs• L49-120 rust/…/tag02_fixtures.rs• L63-100 rust/…/tag02_meshes.rs• L236-274 rust/…/tag02_mvalues.rs• L150-194 rust/…/tag02_nested.rs• L142-249 rust/…/hegel_property_columns.rs• L63-190 rust/…/Cargo.toml• L103-186

Synthetic fixtures

The rust/mlt-synthetics tool creates small named layers that isolate codec features. It covers geometry layouts, stream encodings, presence patterns, strings, dictionaries, m-values, nested values, z values, and version limits. Each tile is decoded to reference JSON, and byte differences are recorded as Rust-specific output only when the logical result still matches. rust/…/main.rs• L1-25

References: rust/…/main.rs• L1-25 rust/…/main.rs• L111-129 rust/…/writer.rs• L75-81 rust/…/writer.rs• L146-232

Fuzz targets

Fuzz inputs exercise parser and decoder safety, encoder round trips, MVT normalization, configured fields, z data, and v2 data. Generated layers are bounded so the targets search malformed and unusual structures without turning input size into the main test. Round-trip fuzzing treats unsupported version combinations as expected results and requires every supported result to reach a stable decoded form. rust/…/roundtrip.rs• L4-67

The decoded-layer target checks idempotence after re-staging. Mesh comparisons allow harmless triangle, corner, and vertex order changes but still require IDs, geometry, properties, z values, nested values, and scoped m-values to remain aligned. Differential fuzzing compares v1 Rust output with the C++ decoder, while field-config fuzzing checks that conversions detect changes to decoded values. rust/…/decoded_layer.rs• L164-230

References: rust/…/roundtrip.rs• L4-67 rust/…/decoded_layer.rs• L164-230 rust/…/differential.rs• L12-69 rust/…/field_config.rs• L19-57 rust/…/z.rs• L13-17 rust/…/z.rs• L113-175 rust/…/mvt_roundtrip.rs• L8-38 rust/…/fuzzing.rs• L17-121 rust/…/fuzzing.rs• L38-95

Performance assets

Performance work measures both tile size and runtime cost. The optimizer compares stream and geometry candidates, while reusable codec state and decoder buffers reduce repeated allocation. Hot-path markers and Criterion benches measure parsing, decoding, staging, encoding, strings, utilities, and v2 vertex paths. rust/…/optimizer.rs• L43-91

The codec also uses data-dependent choices. Front coding reduces repeated dictionary prefixes, presence coding chooses between bitmap, runs, and sparse masks, and vertex dictionaries or triangle trees are used when repeated coordinates or topology make them useful. These choices can change bytes without changing the decoded layer. rust/…/front_coding.rs• L37-60

References: rust/…/optimizer.rs• L43-91 rust/…/optimizer.rs• L131-204 rust/…/limits.rs• L15-25 rust/…/front_coding.rs• L37-60 rust/…/presence_coding.rs• L156-185 rust/…/tri.rs• L24-50 rust/…/Cargo.toml• L14-47

Change checklist

When these files or sections are changed, remember to consider these:

  • A new layer field or column role must stay aligned across the row model, staging path, wire envelope, decoder, row materialization, and exchange converters.
  • A stream change must keep its header context, count rule, presence rule, payload decoder, dump walker, and opposite-version behavior consistent.
  • A geometry change must keep feature types, offset levels, triangle indexes, vertex order, z values, and m-value runs aligned.
  • A feature-order or vertex-order change must move geometry, IDs, properties, nested values, m-values, and z values as one logical row.
  • A v2-only capability must have an explicit v1 rejection and a fixture that proves the version boundary.
  • A mesh reorder or triangle-tree change must preserve triangle-scoped m-values and the mapping from indexes to feature vertices.
  • A string or presence layout change must keep its encoder, header, size choice, budget charge, and reconstruction path aligned.
  • A synthetic fixture change must account for both its MLT bytes and its reference JSON, including allowed Rust-specific byte output.
  • A memory-path change must keep parser reservations, decoder allocations, copied-string charges, scratch-buffer reuse, and reset preconditions consistent.
  • A change to MVT or GeoJSON conversion must state which v2 fields the target format can preserve and which it must reject or omit.

Important failure modes

TriggerConsequenceGuard
A v1 encode receives m-values, nested values, z data, meshes, or another v2-only layoutEncoding stops instead of losing dataVersion checks return a specific MltError
A feature value has the wrong kind, count, shape, or vertex alignmentThe layer cannot be staged safelyTileLayer, StagedLayer, and LayerWriter validate the layer schema
Feature sorting or ring winding moves geometry without its vertex-scoped dataz or m-values attach to the wrong vertexStaging records vertex moves and applies them to all parallel columns
A v2 header, geometry layout, or stream context disagreesParsing stops before data becomes trustedV2 header and root parsers cross-check layout and count rules
A dictionary, FSST table, front-coded entry, presence mask, triangle tree, or index is malformedDecoding returns an error instead of partial outputDedicated codec and geometry bounds checks reject it
A decoded tile or copied string data exceeds its memory budgetParsing, materialization, or conversion fails with a memory-limit errorParser and Decoder charge allocations and copies before reserving them
An MVT round trip contains meshes or v2-only columnsThe target format cannot preserve the layerMVT conversion rejects unsupported geometry and exposes only supported fields
A synthetic, property, or v1/v2 differential result changes logical contentCompatibility checks fail even if bytes are smallerRound-trip tests compare decoded layers and reference JSON

Key files

FileWhy to read it
rust/…/lib.rsPublic codec surface, feature gates, and shared exports
rust/…/tile.rsLogical layer, feature, property, m-value, nested, mesh, and z contract
rust/…/optimizer.rsAutomatic encode choices and size or client-cost selection
rust/…/tile.rsMovement from row features to staged column data
rust/…/stager.rsGeometry winding, vertex sorting, and attribute move tracking
rust/…/encode01.rsV1 envelope and compatibility boundary
rust/…/encode02.rsV2 envelope, shared presence, nested columns, and m-value section
rust/…/encode02.rsV2 topology, tessellation, meshes, vertex order, and z vertices
rust/…/root02.rsV2 layer parsing and context-based stream reconstruction
rust/…/geotype.rsFeature geometry, vertex ranges, triangles, and z mapping
rust/…/into_tile.rsConversion from decoded columns to owned feature rows
rust/…/mvalue.rsVertex- and triangle-scoped value reconstruction
rust/…/nested.rsNested map and list shape reconstruction
rust/…/limits.rsParser and decoder memory budgets
rust/…/layer_writer.rsStreaming feature-by-feature encode path
rust/…/encode.rsMVT output boundary and supported fields
rust/…/geojson.rsGeoJSON representation of common and v2 data
rust/…/tag02_roundtrip.rsV1/v2 logical equivalence and wire coverage
rust/…/tag02_meshes.rsMesh, triangle, z, and triangle-scoped m-value behavior
rust/…/tag02_nested.rsNested map, list, and row-shape behavior
rust/…/tag02_vertex_sort.rsVertex order and movement of z and m-values
rust/…/hegel_property_columns.rsGenerated presence, string, dictionary, and property-column coverage
rust/…/string_copy_budget.rsDecoder budget accounting for copied strings
rust/…/decoded_layer.rsStable decoded results after repeated re-staging
rust/…/differential.rsRust and C++ decoder agreement for v1
rust/…/z.rsZ preservation across geometry and tessellation choices