# Task: Verify Generated Code Works in `no_std` / `no_alloc`
**Goal:** Ensure all generated protobuf code compiles and runs in `no_std` + `no_alloc` environments. Features requiring `alloc` must be gated behind feature flags in the proto generator's output.
---
## Findings
**Already clean (no_std + no_alloc):**
- All reader structs (`Message<'a>`) — zero-copy accessors over `&[u8]`
- All field accessors, enums, builders (`MessageBuilder<'b>`), typed iterators
- Core runtime: `ProtoAccessor`, `ProtoBuilder`, `RevBuilder`, `read_varint`, `write_varint`, all iterators
7. **Fix `RevBuilder::with()`** — replace `let fields: Vec<_> = ... .collect()` with a forward iteration that doesn't allocate. The reverse order doesn't matter for protobuf encoding (fields are independent):
```rust
// Before: let fields: Vec<_> = msg.accessor.raw_fields().collect();
// for (raw, _) in fields.iter().rev() { ... }
// After: for (raw, _) in msg.accessor.raw_fields() { ... }
```
Alternatively, gate the `with()` method itself behind `#[cfg(feature = "alloc")]`.
### Phase 3: Codegen — Service Code (`codegen/src/generator/mod.rs`)
8. **Gate `SERVICE_IMPORTS`** with `#[cfg(feature = "std")]`
9. **Gate all service traits, server impls, and `*Server` structs** with `#[cfg(feature = "std")]`
### Phase 4: Generated Consumer Setup
10. **Emit a `Cargo.toml`** (or instruct consumers) with the right feature setup:
```toml
[features]
default = ["std"]
std = ["roto-runtime/std", "alloc"]
alloc = ["roto-runtime/alloc", "bytes"]
```
### Phase 5: Validation
11. **Update `examples/no_std_test`** to build with `--no-default-features` and verify compilation
12. **Run existing tests** with default features to ensure nothing breaks
13. **Add a compile-test** that verifies `--no-default-features` builds succeed for generated code
| Breaking existing consumers who use `OwnedMessage` | `OwnedMessage` was already behind a manual `#[cfg]` in the `no_std_test` example — consumers aren't relying on it unconditionally |
| `BufMutBuilder` removal in no-alloc mode | It's only useful with `BytesMut` anyway, which requires alloc |
| `RevBuilder::with()` change | Forward iteration is semantically equivalent for protobuf encoding |