Repository navigation
Commit eb71da7
types: add Struct iteration, and Extend for Struct, ListValue and FieldMask (#712)
Fixes #704.
Adds the collection items the issue lists, as one change:
- `Struct`: `len`, `is_empty`, `contains_key`, `iter`, `IntoIterator`
for `&Struct`, `&mut Struct` and `Struct`, and `Extend<(K, V)>` with the
`Into<String>` / `Into<Value>` bounds of `from_fields`. A key that is
already present has its value replaced, as `insert` does. The docs state
that iteration order is unspecified.
- `ListValue`: `Extend<V: Into<Value>>`, matching `from_values`.
- `FieldMask`: `Extend<T: Into<String>>`, appending without normalizing,
as `from_paths` does.
Two additions beyond the plan I posted on the issue:
- **`Struct::iter_mut`.** `IntoIterator for &mut Struct` needs a mutable
iterator anyway, and `ListValue` pairs the two the same way.
- **Three public iterator types, `StructIter`, `StructIterMut` and
`StructIntoIter`,** re-exported at the crate root. `Struct::fields` is
`buffa::__private::HashMap`, whose iterators are `std`'s or
`hashbrown`'s depending on the `std` feature, so the `IntoIterator`
impls need a type of their own to name. `serde_json::Map` takes the same
approach. They forward `fold` and `count` to the map's iterators,
implement `ExactSizeIterator` and `FusedIterator` (checked against the
inner types in a test), and are `#[must_use]`.
`FromIterator` keeps its existing item types (`(String, Value)` and
`Value`). Widening it to match `Extend` could break inference in
existing `collect` calls, so the crate docs describe the difference
instead.
Tests don't depend on hash order. One generic test checks that `C:
Extend<T> + IntoIterator` works for all three types, which is the
issue's motivation.
**Verification**
- `task lint`, `task doc`, `task check-nostd` and `task test-local`
(4211 passed, 0 failed), with the repository-pinned protoc 33.5. `cargo
+1.75 check -p buffa-types --all-features` with an MSRV-resolved
lockfile, as the `msrv-check` job does.
- Reviewed with the repo's `rust-code-reviewer` and
`rust-api-ergonomics-reviewer` per CLAUDE.md: no Critical or High
findings. Addressed the Mediums: the iterators forward `fold`/`count`,
and the docs no longer overstate the `FromIterator`/`Extend` symmetry.
Also addressed the Lows: a static check of the fused/exact-size claims,
`#[must_use]`, `Extend` doc wording, and `HashMap::extend` delegation.
---------
Co-authored-by: Iain McGinniss <iain@anthropic.com>1 parent 47290ba commit eb71da7
4 files changed
Lines changed: 362 additions & 2 deletions
File tree
- .changes/unreleased
- buffa-types/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
270 | 270 | | |
271 | 271 | | |
272 | 272 | | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
273 | 281 | | |
274 | 282 | | |
275 | 283 | | |
| |||
404 | 412 | | |
405 | 413 | | |
406 | 414 | | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
407 | 423 | | |
408 | 424 | | |
409 | 425 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
| |||
237 | 243 | | |
238 | 244 | | |
239 | 245 | | |
240 | | - | |
241 | | - | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
242 | 249 | | |
243 | 250 | | |
| 251 | + | |
244 | 252 | | |
245 | 253 | | |
246 | 254 | | |
| |||
0 commit comments